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

[JENKINS-65442] json api improve performance when encoding unicode characters #214

Merged
merged 1 commit into from Apr 24, 2021
Merged

[JENKINS-65442] json api improve performance when encoding unicode characters #214

merged 1 commit into from Apr 24, 2021

Conversation

scddev
Copy link
Contributor

@scddev scddev commented Apr 22, 2021

Improve performance when encoding escape sequences.

String.format has to compile the pattern for each character which consumes a lot of CPU time when requesting larger json data.

Use bit shift and lookup to simplify conversion.

Doing a small comparison on my notebook reveals:

  • oldStyle 522ms
  public StringBuilder oldStyle() {
    StringBuilder b = new StringBuilder();
    for(int i=0;i<0xffff;i++) {
      b.append("\\u" + String.format("%04x", i));
    }
    return b;
  }
  • newStyle 13ms
  private static final char[] HEX = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

  public StringBuilder newStyle() {
    StringBuilder b = new StringBuilder();
    for(int i=0;i<0xffff;i++) {
      b.append("\\u");
      b.append(HEX[(i >> 12) & 0xf]);
      b.append(HEX[(i >> 8) & 0xf]);
      b.append(HEX[(i >> 4) & 0xf]);
      b.append(HEX[i & 0xf]);
    }
    return b;
  }

@scddev scddev changed the title improve performance when encoding unicode characters [JENKINS-65442] improve performance when encoding unicode characters Apr 23, 2021
@scddev scddev changed the title [JENKINS-65442] improve performance when encoding unicode characters [JENKINS-65442] json api improve performance when encoding unicode characters Apr 23, 2021
Copy link
Member

@oleg-nenashev oleg-nenashev left a comment

Choose a reason for hiding this comment

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

Would be nice to have a benchmark data for it, e.g. JMH.
No objections if it is merged as is. CC @timja @jglick @daniel-beck as core maintainers

@jglick jglick merged commit 5f5221b into jenkinsci:master Apr 24, 2021
@scddev scddev deleted the unicode-performance branch April 25, 2021 08:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants