Nov. 29th, 2017

mtbc: maze C (black-yellow)
At work I program in Java 7 mostly. I was asked to add some statistics-gathering to some code that gets called a lot so performance is key. After some initial test runs, back-of-the-envelope calculations about for how long our servers may run, etc., I did not feel safe in assuming that even a eight-byte long counter would never wrap around. In practice, I do not think a production server facing a continuously heavy workload during many years' uptime would come close to overflowing my long but I am used to having so much margin to work with that I do not even have to think about the issue.

A surprising discovery for me is that Java 7 does not offer efficient incrementing of arbitrarily large integers. Coming from a functional programming background I appreciate immutable values but I also appreciate compilers that can optimize much of the heap allocation out of the native code they generate. I have far less faith in what Java does, or indeed may do, with BigInteger, and people on the Internet already seem to have done some performance testing.

Fortunately, not all surprising discoveries are bad. It also turns out to be easy to increment an arbitrarily large integer with few heap allocations by storing it as a byte array, here named count: Java code. ) To log the statistic readably it suffices to return new BigInteger(1, count).

Profile

mtbc: photograph of me (Default)
Mark T. B. Carroll

December 2025

S M T W T F S
 12 3456
78910111213
14 15161718 19 20
21222324252627
2829 3031   

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jan. 3rd, 2026 06:20 am
Powered by Dreamwidth Studios