Video in progress
Should the company codebase adopt a primitive collection library like fastutil or Eclipse Collections? How do you justify the decision?
They cut memory and allocation sharply, but they bring a new dependency, a new API and a learning cost for teams. I would propose using them only at hot spots that a profile and benchmark have shown, hidden inside a module.
📖 Answer
The benefit shows in concrete numbers: Int2IntOpenHashMap instead of Map<Integer, Integer> has no boxing and no Node, uses 4 to 5 times less memory, and allocates nothing during iteration. In places like real-time calculation, ranking and graph algorithms, this noticeably shortens GC pauses.
But at the Staff level, the question is not "is it faster?" but "is it worth it for the whole organization?":
- Dependency size. The full fastutil jar is 20+ MB (there is a minimal variant too). Eclipse Collections isn't small either. This affects the container image and startup.
- API spread. If an
IntListleaks into the interfaces between services, every module becomes tied to this library, and removing it later is very hard. - Two sets of rules. Some primitive maps return a "default return value" (0) for a missing key, not null. A developer used to the JDK collections will introduce bugs here.
- Updates and security: one more library to keep an eye on.
My working rules:
- Measure first: a heap histogram or allocation profile (async-profiler
-e alloc) must actually show boxing andNodes. - Pick one library and pin its version in the platform BOM, so that two or three of them don't get mixed.
- Primitive collections only inside a module; the external interface uses JDK types or arrays.
- If a JDK solution is enough (for example
int[]and plain indexing), you don't need the library.
I'll mention the future too: if Project Valhalla value classes arrive, the need for these libraries may shrink. But they are not in any released JDK yet (only early-access builds), and you can't rely on it when making a plan.
Sign up to mark studied questions and save your progress.