4 ms·
> At the expense of O(n) index access. Ah yes, that feature which means we must have 2x memory overhead and/or dynamic dispatch, yet is also totally useless in
by morelisp 3y ago
> At the expense of O(n) index access.
Ah yes, that feature which means we must have 2x memory overhead and/or dynamic dispatch, yet is also totally useless in practice.
- PhilipRoman 3y agoThe fact of the matter is that there are millions of places with loops like for(...) {charAt(i)} so it is by definition useful in practice. If Java had pushed iterators more strongly, perhaps it would be possible to make the switch.
- morelisp 3y agoIn all seriousness, what's the point of having the best JIT in the world if it can't reliably transform this antipattern into an iterator?
- PhilipRoman 3y agoOpenJDK seems pretty averse to including special case optimizations, otherwise I'm sure we would have intrinsics at least for ArrayList/HashMap containing boxed primitives. AFAIK they are still noticeably slower than plain arrays or manually inlined implementations. For the most basic case you wouldn't even need JIT support - just keep a cache field of the last index accessed and where it ended and decode based on that.