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

ResponseFormatter might cause serious performance issue! #18

Open
jerome89 opened this issue Aug 15, 2019 · 0 comments
Open

ResponseFormatter might cause serious performance issue! #18

jerome89 opened this issue Aug 15, 2019 · 0 comments
Assignees
Labels
performance If you need to refactor existing logic to get better performance
Milestone

Comments

@jerome89
Copy link
Collaborator

jerome89 commented Aug 15, 2019

Currently only JSON response is supported, but it can cause serious performance issue.
The following is code block from ResponseFormatter.java.

        for(TimeSeries timeSeries : timeSeriesList) {
            List<String> datapoints = new ArrayList<>();
            for(int i = 0; i < timeSeries.getValues().length; i++) {
                String stringValue;
                if (timeSeries.getValues()[i] == null) {
                    stringValue = "null";
                } else {
                    stringValue = GraphiteUtils.formatDoubleSpecialPlain(timeSeries.getValues()[i]);
                }

                datapoints.add("[" + stringValue + ", " + (timeSeries.getFrom() + timeSeries.getStep() * i) + "]");
            }
            results.add("{\"target\": " + gson.toJson(timeSeries.getName()) + ", \"datapoints\": [" + Joiner.on(", ").join(datapoints) + "]}");
        }
        String responseString = "[" + Joiner.on(", ").join(results) + "]";

Joiner.on(", ").join(datapoints) can be bottleneck if ArrayList 'datapoints' is large because Joiner uses StringBuilder to append each data in String, but the capacity of StringBuilder is not pre-determined, so it continuously copies whole array whenever it exceeds its capacity.

Here is the reference:
https://docs.oracle.com/javase/tutorial/java/data/buffers.html

It is recommend not to use Joiner in this case because it handles arbitrary length of ArrayLists. How about change the code to use StringBuilder directly with proper initialCapacity?

@Dark0096 Dark0096 added the performance If you need to refactor existing logic to get better performance label Aug 17, 2019
@Dark0096 Dark0096 self-assigned this Aug 17, 2019
@Dark0096 Dark0096 added this to the 1.0.0-RC milestone Aug 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance If you need to refactor existing logic to get better performance
Projects
None yet
Development

No branches or pull requests

2 participants