3 ms·
Well, if strings are immutable then you can point into an array in the subsrring and the worst that can happen is a space leak. If your strings are mutable then
by aardvark179 9y ago
Well, if strings are immutable then you can point into an array in the subsrring and the worst that can happen is a space leak. If your strings are mutable then you need some extra book keeping so that attempts to copy the parent or child won’t affect the other. Whether you get a gain from sharing is going to depend on the length of your strings and what operations you perform on them.
If you are trying to reimplement an existing VM/standard library then you may also need to honour the expectations of developers. If they expect creating a substring to be an O(1) operation and code against that expectation then you probably need to honour it.