38. 1 1 0 1 0 1 0 1
1 0 1 1 0 0 1 0
1 0 1 0 0 1 1 0
1 1 0 0 1 1 1 0
1 1 0 0 1 1 1 0
1 0 0 0 0 1 1 0
1 1 1 0 0 1 1 0
1 0 1 0 0 1 1 0
0 0 0 0 1 0 1 0
1 0 0 0 0 1 1 0
Zero-copy read optimization
MessagePack::Buffer
next
mapped_string
mem
Object
{
“count” => 1,
“page” => 0,
“body” => [
“LONG TEXT”
]
]
rb_str_substr()
Fast copy-on-write
x83 A5 c o u n t
t x01 xA4 p a g
e xA4 b o d y
x91 A9 L O N G
T E X T ”
“
※ if SHARABLE_SUBSTRING_P() returns true
x83 A5 c o u n t
t x01 xA4 p a g
e xA4 b o d y
x91 A9 L O N G
T E X T ”
Source String
“
rb_str_dup()
Fast copy-on-write
45. 1 1 0 1 0 1 0 1
1 0 1 1 0 0 1 0
1 0 1 0 0 1 1 0
1 1 0 0 1 1 1 0
1 1 0 0 1 1 1 0
1 0 0 0 0 1 1 0
1 1 1 0 0 1 1 0
1 0 1 0 0 1 1 0
0 0 0 0 1 0 1 0
1 0 0 0 0 1 1 0
Zero-copy read optimization
MessagePack::Buffer
next
mapped_string
mem
Object
{
“count” => 1,
“page” => 0,
“body” => [
“LONG TEXT”
]
]
x83 A5 c o u n t
t x01 xA4 p a g
e xA4 b o d y
x91 L O N G
T E X T ”
“x83 A5 c o u n t
t x01 xA4 p a g
e xA4 b o d y
x91 L O N G
T E X T ”
Source String
“
rb_str_dup()
Fast copy-on-write
rb_str_substr()
Fast copy-on-write
※ if SHARABLE_SUBSTRING_P() returns true
46. 1 1 0 1 0 1 0 1
1 0 1 1 0 0 1 0
1 0 1 0 0 1 1 0
1 1 0 0 1 1 1 0
1 1 0 0 1 1 1 0
1 0 0 0 0 1 1 0
1 1 1 0 0 1 1 0
1 0 1 0 0 1 1 0
0 0 0 0 1 0 1 0
1 0 0 0 0 1 1 0
Copy-on-write substring
SHARABLE_SUBSTRING_P() returns true only when substring shares the last
0 termination with the original string
s = "a" * 1_000_100
10_000.times do
s.slice(100, 1_000_000)
end
#=> 0.002 sec
s = "a" * 1_000_100
10_000.times do
s.slice(100, 1_000_000 - 1)
end
#=> 2.7 sec
Not including the last character
disables Copy-on-Write
47. 1 1 0 1 0 1 0 1
1 0 1 1 0 0 1 0
1 0 1 0 0 1 1 0
1 1 0 0 1 1 1 0
1 1 0 0 1 1 1 0
1 0 0 0 0 1 1 0
1 1 1 0 0 1 1 0
1 0 1 0 0 1 1 0
0 0 0 0 1 0 1 0
1 0 0 0 0 1 1 0
Copy-on-write substring
SHARABLE_SUBSTRING_P() returns true only when substring shares the last
0 termination with the original string OR Ruby is compiled with
SHARABLE_MIDDLE_SUBSTRING flag (not enabled by default)
s = "a" * 1_000_100
10_000.times do
s.slice(100, 1_000_000)
end
#=> 0.002 sec
s = "a" * 1_000_100
10_000.times do
s.slice(100, 1_000_000 - 1)
end
#=> 2.7 sec 0.002 sec
Using ruby binary compiled with
SHARABLE_MIDDLE_SUBSTRING