3 ms·
If you'd be sharing slices naively you might run into cases like a = "large" * 10000000 self.b = a[1:10] this tiny slice would then "magically" keep t
by rndgermandude 8y ago
If you'd be sharing slices naively you might run into cases like
a = "large" * 10000000
self.b = a[1:10]
this tiny slice would then "magically" keep the entire huge large string alive. It would be easy to defacto leak memory with that.
Even if you are less naive and introduce some kind of heuristic to determine when to share and when to copy, you might still be leaking "lots" of memory if you have a lot of small slices (into small-ish strings), which might hurt on low speced/embedded systems with tiny memory available.
- jerf 8y agoI would point out that by "it would be easy to leak memory with that", in reality almost every run time that has ever implemented this "optimization" to automatically apply has had to either pull it back out, or make it explicit whether or not you're taking a copy of the underlying string. I've seen it at least three times. The probability of a given program having this problem is lowish (not that low, but reasonably low), but the probability of one of a language community's flagship projects having this problem shoots to 100% almost instantly for any non-trivial language or community.
- srean 8y agoI like it how Guile does it. There it is very explicit whether you are getting a copy or a view.