Skip to content

HttpHeaderHelper: String.replace instead of Pattern.matcher().replaceAll#483

Merged
amarkevich merged 2 commits intoapache:masterfrom
amarkevich:feature/HttpHeaderHelper
Dec 17, 2018
Merged

HttpHeaderHelper: String.replace instead of Pattern.matcher().replaceAll#483
amarkevich merged 2 commits intoapache:masterfrom
amarkevich:feature/HttpHeaderHelper

Conversation

@amarkevich
Copy link
Copy Markdown
Contributor

benchmark [1] results:

  • old: 40 MB 47 ms
  • new: 9 MB 0 ms
public class HttpHeaderHelperMapCharsetTest {
    private static final int CNT = 131072;
    static final String charset = java.nio.charset.StandardCharsets.UTF_8.name();
    private static final String[] STR = new String[CNT];
    static {
        for (int i = 0; i < CNT; ++i) {
            if (i % 3 == 0) {
                STR[i] = "\"" + charset + "\"";
            } else if (i % 3 == 1) {
                STR[i] = "'" + charset + "'";
            } else {
                STR[i] = charset;
            }
        }
    }

    @org.junit.Test
    public void mapCharset() {
        metrics("HttpHeaderHelper::mapCharset", HttpHeaderHelper::mapCharset);
    }

    @org.junit.Test
    public void mapCharset2() {
        metrics("HttpHeaderHelper::mapCharset2", HttpHeaderHelper::mapCharset2);
    }

    private static void metrics(String label, java.util.function.Function<String, String> f) {
        Runtime.getRuntime().gc();
        long m = Runtime.getRuntime().freeMemory();
        long t = System.currentTimeMillis();
        for (int i = 0; i < CNT; ++i) {
            org.junit.Assert.assertEquals(charset, f.apply(STR[i]));
        }
        System.out.println(label + ": " + ((m - Runtime.getRuntime().freeMemory()) / (1024 * 1024)) + " MB "
                + (System.currentTimeMillis() - t) + " ms");
    }

    public static void main(String[] args) {
        final HttpHeaderHelperMapCharsetTest splitTest = new HttpHeaderHelperMapCharsetTest();
        for (int i = 0; i < 10; ++i) {
            System.out.println("Round #" + i);
            splitTest.mapCharset();
            splitTest.mapCharset2();
        }
    }

}

Copy link
Copy Markdown
Contributor

@andymc12 andymc12 left a comment

Choose a reason for hiding this comment

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

LGTM - thanks!

@amarkevich amarkevich merged commit 27ad1dc into apache:master Dec 17, 2018
@amarkevich amarkevich deleted the feature/HttpHeaderHelper branch December 17, 2018 17:49
rmannibucau pushed a commit to rmannibucau/cxf that referenced this pull request Mar 18, 2019
…All (apache#483)

* HttpHeaderHelper: String.replace instead of Pattern.matcher().replaceAll

* cxf-rt-transports-jms: reuse HttpHeaderHelper.findCharset
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.

3 participants