1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative 'memory'
string = nil
measure_memory("Initial allocation") do
string = "a" * 5*1024*1024
string.freeze
end # => 5.0 MB
measure_memory("Byteslice from start to middle") do
# Why does this need to allocate memory? Surely it can share the original allocation?
x = string.byteslice(0, string.bytesize / 2)
end # => 2.5 MB
measure_memory("Byteslice from middle to end") do
string.byteslice(string.bytesize / 2, string.bytesize)
end # => 0.0 MB
measure_memory("Slice! from start to middle") do
string.dup.slice!(0, string.bytesize / 2)
end # => 7.5 MB
measure_memory("Byte slice into two halves") do
head = string.byteslice(0, string.bytesize / 2) # 2.5 MB
remainder = string.byteslice(string.bytesize / 2, string.bytesize) # Shared
end # 2.5 MB
|