4 ms·
Why not just do the copying conditionally? If it's at least half the size of the parent, reference the parent. Otherwise just make a copy...
by wfunction 13y ago
Why not just do the copying conditionally?
If it's at least half the size of the parent, reference the parent. Otherwise just make a copy...
- mafuyu 13y agoIn that case, it would be difficult for the programmer to reason about the memory usage of their use of substring(). This change lets programmers not worry about the implementation of substring() and memory leaks in return for linear runtime complexity.
- wfunction 13y agoHow would it be difficult to reason? You know you're never off by more than a factor of 2 (or whatever factor is used), just like with an ArrayList.
- emn13 13y agoPart of the reason for the change was likely to allow better optimizations of the strings. The new strings are basically equivalent to arrays and might be optimized as such; even without that they certainly don't need an offset nor length field anymore since the underlying array can provide that. If you conditionally used one implementation or the other you'd lose the optimization opportunity. You could alternatively make string non-final; but then your method resolution is more expensive and that would really be a pretty big change in any case. I think java made the right choice here since its trivial to wrap string in another structure whenever you need a fast substring, it's just that that custom string won't be passable to lots of other API's which makes it a potentially nasty surprise for existing codebases.
- cpeterso 13y agoCouldn't the substring copying be deferred until the parent string is ready to be GC'd? In the common case of a short-lived substring, no string copies are necessary. Only if the substring outlives the parent string would a copy be necessary.