Skip to content

Comment on Speeding Up Ruby with Shared Strings

Comments

So essentially string interning for Ruby?

There are significant differences. Depending on the case, it can be more or less effective.

1) If you intern all strings, then you're guaranteed to have one copy of "Hello World", which this doesn't (you could concatenate "Hello" and "World" twice).

2) String interning doesn't let similar strings share the same array. The Ruby trick lets you have

    "Hello World"
    "World"
    "orld"
    "rld"
    "ld"
    "d"
all share the same underlying array, whereas string interning gives you a separate array for each.

I think Substring in Swift is something similar?

Yeah it’s pretty common optimization. Some languages do this with array slicing.

Why couldn't you do both?

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.