3 ms·
Sure, but Optional<T> is what we were already discussing, and List<T> is the collection type that most obviously requires no indirection. Feel free to replace O
by pdpi 4y ago
Sure, but Optional<T> is what we were already discussing, and List<T> is the collection type that most obviously requires no indirection. Feel free to replace Optional<T> with any other sort of sum type and the point still stands — you always have one extra object between the list and the "actual value".
- yakubin 4y ago> List<T> is the collection type that most obviously requires no indirection. Collections in Java as of now (before project Valhalla) are actually collections of pointers to objects, so at least one level of indirection. Another level of indirection is List itself, which is an interface, so the compiler can’t ahead of time devirtualise method calls, only after JIT detects which List implementation it’s dealing with at runtime.
- kaba0 4y agoVirtual calls are trivial optimizations and doesn’t really matter here either way as it only has to happen once, no matter the list size.
- pdpi 4y agoSorry, I wasn't clear: Tree maps have built-in indirection, hash maps can use open addressing but they can also use chaining, which has indirection. An array-backed list is the only collection type that obviously requires no indirection in concept. In a world where Java has value types, you could build ArrayList<T> with no indirection (other than polymorphism/dispatch).