[JAVA-4816] Improve performance of ObjectId hex string parsing Created: 22/Nov/22  Updated: 28/Oct/23  Resolved: 13/Dec/22

Status: Closed
Project: Java Driver
Component/s: BSON
Affects Version/s: None
Fix Version/s: 4.9.0

Type: Improvement Priority: Minor - P4
Reporter: Ian White Assignee: Jeffrey Yemin
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Fully Compatible
Documentation Changes: Not Needed

 Description   

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++)

{             new ObjectId("1234567890abcdef" + i);         }

        long endTime = System.currentTimeMillis();

        System.out.println("Time elapsed: " + (endTime - startTime));
    }

On master:

Time elapsed: 19460

On branch:

Time elapsed:  3617



 Comments   
Comment by Githook User [ 13/Dec/22 ]

Author:

{'name': 'Ian White', 'email': 'ian@charthop.com', 'username': 'eonwhite'}

Message: Improve performance of ObjectId#parseHexString (#1034)

JAVA-4816

Co-authored-by: Jeff Yemin <jeff.yemin@mongodb.com>
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/cbb8d04bcc6b14d724f654f1264d481124522338

Generated at Thu Feb 08 09:03:02 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.