logoalt Hacker News

drob518yesterday at 7:58 PM1 replyview on HN

Yep, and sometimes just a small code change can flip on boxing.


Replies

cogman10yesterday at 8:05 PM

One of my least proud (most proud?) hacks when working with very large data sets is something like this

    Map<Integer, Integer> intCache = new HashMap<>();
    
    while (loading) {
      Integer feild1 = intCache.computeIfAbsent(getField1(), (i)->i);
    }
This is a terrible thing that shouldn't be as useful as it is to us... but it is really useful. We have a bunch of objects that can optionally have Integer values (hence a null is valid) but those int values are frequently the same.

This saves a bunch of memory and ultimately GC pressure as a result.

Valhalla can't come soon enough for us.

show 2 replies