Last active
October 19, 2020 00:29
-
-
Save drbrain/0ec0f04b8b9575df2838983c320dd72f to your computer and use it in GitHub Desktop.
Ruby frozen string interning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ruby not_frozen.rb | |
400600 | |
# The string "a" is embedded inside the String object which is 40 bytes | |
# | |
# So 400600/40 = ~10,015 string objects | |
# | |
# (IIRC, up to 24 bytes of string may be embedded in the String) | |
$ ruby frozen.rb | |
1280 | |
# Strings may be created elsewhere so it is hard to be exact | |
# | |
# Still, this is much less than 400KB of String objects |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require "objspace" | |
GC.start | |
before = ObjectSpace.count_objects_size | |
array = Array.new(10_000) { "a" } | |
after = ObjectSpace.count_objects_size | |
p after[:T_STRING] - before[:T_STRING] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: false | |
require "objspace" | |
GC.start | |
before = ObjectSpace.count_objects_size | |
array = Array.new(10_000) { "a" } | |
after = ObjectSpace.count_objects_size | |
p after[:T_STRING] - before[:T_STRING] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment