4 ms·
This is not an ignorant question. Any half-experienced Erlang developer can tell you that you use `list_to_existing_atom' rather than `list_to_atom' if you are
by oinksoft 14y ago
This is not an ignorant question. Any half-experienced Erlang developer can tell you that you use `list_to_existing_atom' rather than `list_to_atom' if you are ever doing dynamic things with atoms. So, if you're trying to accomplish dynamic module lookup and foo_baz.beam is in your code path, default startup will create the `foo_baz' atom, and you know that this atom will exist at runtime.
Symbols in Ruby are atoms (the term "atom" spans languages), and GC/space issues plague any persistent term like an atom, in any language.
And thus I arrive at a key question: Does Ruby have something like `list_to_existing_atom', or some mechanism for telling if a symbol exists already? I see no analog to this, only the `ID2SYM' macro in the extensions API, and similar calls like String#to_sym.
Perhaps there is some way to clean up symbols after they are created. This to me would seem like the ideal route. It's good they've got a stop-gap fix by changing defaults, but it feels to me like they're punting here. Perhaps users who do [ab]use this feature also would not like DOS attacks?
I hope others who know more about Ruby extension development, and symbol management capabilities, can chime in on these questions.
- benmmurphy 14y agothere are methods in the ruby reflection api that take strings and silently convert them to symbols so it is really easy to accidentally leak memory. however, on ruby trunk they added a method: rb_check_id which can be used to check if a string has been already symbolized (https://github.com/ruby/ruby/blob/trunk/parse.y#L10465 https://github.com/ruby/ruby/blob/trunk/parse.y#L10465). this means when these reflection methods get passed a string and it hasn't been symbolized they can bail out and not symbolize the string. (https://github.com/ruby/ruby/blob/trunk/object.c#L2073 https://github.com/ruby/ruby/blob/trunk/object.c#L2073)
- oinksoft 14y agoThis is an excellent development. Indeed, when researching just now, I saw examples of people throwing strings at Module#const_defined?, which no doubt get converted to symbols straightaway.