Re:Microfeatures I'd like to see in more languages
by revskill on Thursday January 05, 2023 @04:23PM
(#34266805)
Is it for performant reason ?
[ Parent
| on HN ]
Re:Microfeatures I'd like to see in more languages
by onei on Thursday January 05, 2023 @04:27PM
(#34266882)
Symbols in Ruby are meant to be more performant that strings iirc. If I have symbol :a, then it's allocated once regardless of how many time it appears. As opposed to "a" which is reallocated every time.
I guess it's similar to Python having a single instance of small integers. PlayStation also experimented with caching small floats which gave them some perf improvements too, but I think wasn't as performant in all cases.
Re:Microfeatures I'd like to see in more languages
by justincormack on Thursday January 05, 2023 @04:32PM
(#34266973)
Lua (and some other languages) intern strings, so all strings that are the same point to the same string instance. This gives the same benefits (plus string equality is just pointer equality) without a different type.
Re:Microfeatures I'd like to see in more languages
by pawelduda on Thursday January 05, 2023 @04:33PM
(#34266986)
There is a caveat in older Ruby versions that they aren't garbage collected, so they shouldn't be used for things like user input. Not a problem since 2.2 though.
Re:Microfeatures I'd like to see in more languages
by progval on Thursday January 05, 2023 @04:34PM
(#34267001)
Symbols can even improve performance. Replace them with integers at compile time like a global enum, and so the runtime only needs to compare integers instead of potentially lengthy (especially if UTF-16) strings.
Re:Microfeatures I'd like to see in more languages
by masklinn on Thursday January 05, 2023 @04:43PM
(#34267146)
> Replace them with integers at compile time like a global enum, and so the runtime only needs to compare integers instead of potentially lengthy (especially if UTF-16) strings.
All of those strings will be interned, and can thus be compared by identity. Which is an integer comparison.