The built-in ObjectId parseHexString() implementation is somewhat slow, because it calls Integer.parseInt() 12 times. Replacing with hand-coded hex parsing achieves something like a 5x improvement.
Performance test (ran on master and on branch on my M1 Mac):
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
for (int i = 10000000; i < 100000000; i++)
long endTime = System.currentTimeMillis();
System.out.println("Time elapsed: " + (endTime - startTime));
}
On master:
Time elapsed: 19460
On branch:
Time elapsed: 3617