Hash#dup should not duplicate keys
hachi8833 opened this issue · comments
Shozo Hatta commented
ref: #797
Currently, Hash#dup
duplicates the hash keys while values are not duplicated:
h = { key: "value" }
b = h.dup
h.object_id #» 824635375680
b.object_id #» 824634333888
h.each do |k, v|
puts k.object_id
puts v.object_id
end
#» 824634332320
#» 824633786336
b.each do |k, v|
puts k.object_id
puts v.object_id
end
#» 824635379488 # duplicated
#» 824633786336 # not duplicated
Stan Lo commented
@hachi8833 I've done some research and unfortunately, this can't be properly fixed at the moment. because of the way we implement Hash objects.
type HashObject struct {
*BaseObj
Pairs map[string]Object
// See `[]` and `[]=` for the operational explanation of the default value.
Default Object
}
As you can see, we use a string
map to store objects internally, which means each Hash only holds its keys values instead of individual key (String) object.
We can solve the issue by:
- Introducing String interning to Goby. This will behave like Ruby's frozen string. Since one string value will only map to one String object, all identical hash keys will point to the same object. But this affects how String works in Goby largely, it's not worth the effort only for making hash keys identical.
- Storing all key's String objects in Hash objects. This feels like a workaround for this particular issue to me. And it will make large Hash objects take much more memory, which is not worth it either.
So my conclusion is to close this issue for now and we can merge #797 once other comments are addressed.
Shozo Hatta commented
Thank you for the investigation!