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

Performance improvement for DefaultConverter.convertToJSONPrimitive() #43

Merged
merged 1 commit into from Jun 9, 2022

Conversation

ind1go
Copy link
Contributor

@ind1go ind1go commented May 20, 2022

It turns out it's much more efficient to avoid exceptions when trying alternatives types in DefaultConverter.convertToJSONPrimitive(). Instead we can do a bit of character parsing to more efficiently find the type before attempting to do the exception-throwing X.parseX().

The results on a 2019 Intel Macbook Pro for the following test code (which loops through a variety of test data 1000000 times) are:
Before: 27.31s
After: 0.76s
... which seems worth doing!

Test code:

public static void main(String[] args) {
    String[] testValues = new String[] { "test1", null, "true", "True", "", " ", " fdskhfdsf ", "0", "1", "1.1",
            "-1", "NaN", "-7.0-4.2", " 0 ", " true ", "+23", ".33", "-3e7", "-3.4", "-3.0E7", "false", "-3-",
            "-3.0EE7" };
    Class[] testTypes = new Class[] { String.class, null, Boolean.class, String.class, String.class, String.class,
            String.class, Long.class, Long.class, Double.class, Long.class, String.class, String.class,
            String.class, String.class, String.class, String.class, String.class, Double.class, Double.class,
            Boolean.class, String.class, String.class };
    Object[] values = new Object[] { "test1", null, Boolean.TRUE, "True", "", " ", " fdskhfdsf ", Long.valueOf(0),
            Long.valueOf(1), Double.valueOf(1.1), Long.valueOf(-1), "NaN", "-7.0-4.2", " 0 ", " true ", "+23",
            ".33", "-3e7", Double.valueOf(-3.4), Double.valueOf(-3.0E7), Boolean.FALSE, "-3-", "-3.0EE7" };
    DefaultConverter dc = new DefaultConverter();
    long time;
    long time2;

    int loopTotal = 1000000;
    time = System.nanoTime();
    for (int loop = 0; loop < loopTotal; loop++) {
        for (int i = 0; i < testValues.length; i++) {
            Object result = dc.convertToJSONPrimitive(testValues[i]);
            if (result != null) {
                if (!result.equals(values[i]))
                    throw new RuntimeException(
                            "Wrong value for " + i + ", got: " + result + "(" + result.getClass().getName()
                                    + ") expected: " + values[i] + "(" + values[i].getClass().getName() + ")");
                if (result.getClass() != testTypes[i]) {
                    throw new RuntimeException("Wrong type for " + i + ", got: " + result.getClass().getName()
                            + " expected " + testTypes[i].getName());
                }
            } else {
                if (values[i] != null)
                    throw new RuntimeException("Unexpected null");
            }
        }
    }
    time2 = System.nanoTime();
    System.err.println("Result time: " + String.format("%.2f", (time2 - time) * Math.pow(10, -9)));
}

@dkulp dkulp merged commit cfc7382 into jettison-json:master Jun 9, 2022
@ind1go ind1go deleted the faster-parse branch June 14, 2022 12:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants