Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JAVA-4816] perf: improving performance of ObjectId parseHexString() #1034

Merged
merged 5 commits into from
Dec 13, 2022

Conversation

eonwhite
Copy link
Contributor

@eonwhite eonwhite commented Nov 3, 2022

JIRA Ticket: https://jira.mongodb.org/browse/JAVA-4816

The current implementation of ObjectId parseHexString is somewhat slow, because it makes a total of 12 calls to Integer.parseInt, which is not the fastest to begin with. This imposes some overhead to all ObjectId(string) calls, which can really add up at scale, for example when deserializing a large amount of ObjectId data via Jackson.

Replacing this implementation with a hand-rolled version which converts each part of the hex string to a byte. This in my rough testing is about 5x faster than the current implementation of parseHexString.

Tests (on my M1 MacBook Pro):

    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

Copy link
Contributor

@jyemin jyemin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @eonwhite thanks for the contribution. Can you do a couple of things to move this forward:

  1. Open an issue in the JAVA project of MongoDB's Jira, and then reference the issue number in the PR description (see the CONTRIBUTING guide for details). Please attach your performance tests as well.
  2. Run ./gradlew bson:check to make sure all static and runtime checks are passing

bson/src/main/org/bson/types/ObjectId.java Outdated Show resolved Hide resolved
@eonwhite eonwhite changed the title perf: improving performance of ObjectId parseHexString() [JAVA-4816] perf: improving performance of ObjectId parseHexString() Nov 22, 2022
@eonwhite
Copy link
Contributor Author

Hi @eonwhite thanks for the contribution. Can you do a couple of things to move this forward:

  1. Open an issue in the JAVA project of MongoDB's Jira, and then reference the issue number in the PR description (see the CONTRIBUTING guide for details). Please attach your performance tests as well.
  2. Run ./gradlew bson:check to make sure all static and runtime checks are passing

Thanks @jyemin -- have resolved issues and verified that tests pass.

@jyemin
Copy link
Contributor

jyemin commented Dec 7, 2022

Hi @eonwhite I pushed a commit with a few small improvements to your PR. PTAL and then I can merge it.

@jyemin jyemin merged commit cbb8d04 into mongodb:master Dec 13, 2022
@jyemin
Copy link
Contributor

jyemin commented Dec 13, 2022

Thanks again for the contribution @eonwhite

ispringer pushed a commit to evergage/mongo-java-driver that referenced this pull request May 16, 2023
JAVA-4816

Co-authored-by: Jeff Yemin <jeff.yemin@mongodb.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants