goby-lang / goby

Goby - Yet another programming language written in Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hash#dup should not duplicate keys

hachi8833 opened this issue · comments

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

@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:

  1. 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.
  2. 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.

Thank you for the investigation!