Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-935

Performance: which String.getBytes method to use

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.1
    • 2.4.1
    • Appenders, Core
    • None

    Description

      I did some investigation to compare the performance of String.getBytes(String) to String.getBytes(Charset) and it turns out that the String variant is significantly faster (at least on Windows 7 and Solaris 10).

      I am not sure what causes this but the code path for String.getBytes(Charset) leads to java.lang.StringCoding.encode(Charset, char[], int, int) which creates a new StringEncoder for each invocation. By contrast, String.getBytes(String) leads to java.lang.StringCoding.encode(String, char[], int, int) which uses a cached StringEncoder (at least in Java 6 and 7). This may be one reason.

      I also compared Java 6, 7 and 8. Note that with each upgrade performance is getting better, with a huge jump in Java 8 for ISO-8859-1 (I suspect this is done by simply casting each char to a byte, dropping the 8 most significant bits of each 16-bit char).

      Benchmark results follow below, sorted by throughput (best first).

      java -jar benchmarks.jar ".*StringEncodingBenchmark.*" -f 1 -t 1 -wi 5 -i 5

      Windows 7 (64bit) 2-core Intel i5-2500 CPU @3.30Ghz

      jdk1.6.0_45
      Benchmark                                                  Mode  Samples         Score       Error Units
      o.a.l.l.p.j.StringEncBenchmark.GetBytesString88591        thrpt        5  12967053.847 ± 80324.398  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesCharSet88591       thrpt        5   6816575.144 ± 50311.921  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytes                   thrpt        5   3431255.953 ± 51075.510  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesStringDefault      thrpt        5   3356148.444 ± 29690.404  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesStringShiftJIS     thrpt        5   3339120.398 ± 13213.076  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesCharSetDefault     thrpt        5   1637800.057 ± 12736.976  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesCharSetShiftJIS    thrpt        5   1516240.836 ± 12076.700  ops/s
      
      jdk1.7.0_55
      Benchmark                                                  Mode  Samples         Score        Error  Units
      o.a.l.l.p.j.StringEncBenchmark.GetBytesCharSet88591       thrpt        5  13515690.815 ± 579537.298  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesString88591        thrpt        5  10818301.339 ±  60140.387  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesStringDefault      thrpt        5   4713065.699 ±  73247.202  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytes                   thrpt        5   4587139.396 ± 109611.806  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesCharSetDefault     thrpt        5   3629802.089 ±  38100.113  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesStringShiftJIS     thrpt        5   3388620.097 ±  37114.869  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesCharSetShiftJIS    thrpt        5   2024599.702 ±  46543.717  ops/s
      
      jdk1.8.0_25
      Benchmark                                                  Mode  Samples         Score        Error  Units
      o.a.l.l.p.j.StringEncBenchmark.GetBytesCharSet88591       thrpt        5  52009871.813 ± 502287.883  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesString88591        thrpt        5  26618044.840 ± 236755.306  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytes                   thrpt        5   5342101.771 ±  39781.910  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesStringDefault      thrpt        5   5281169.402 ±  55524.193  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesStringShiftJIS     thrpt        5   5276164.715 ± 100823.625  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesCharSetShiftJIS    thrpt        5   4279903.988 ±  49535.013  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesCharSetDefault     thrpt        5   3858350.559 ±  70966.741  ops/s
      

      Solaris 10 (64bit) 4-core Xeon X5570 dual CPU @2.93Ghz

      jdk1.6.0_20
      Benchmark                                                  Mode  Samples        Score        Error  Units
      o.a.l.l.p.j.StringEncBenchmark.GetBytesStringDefault      thrpt        5  7736847.092 ±  51274.226  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytes                   thrpt        5  7421582.562 ±  76260.755  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesString88591        thrpt        5  6829890.456 ± 189812.619  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesCharSetDefault     thrpt        5  6811579.239 ± 148393.528  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesCharSet88591       thrpt        5  3921075.542 ±  38000.495  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesStringShiftJIS     thrpt        5  2309217.326 ±  20982.591  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesCharSetShiftJIS    thrpt        5  1029117.710 ±  14768.961  ops/s
      
      
      jdk1.7.0_06
      Benchmark                                                  Mode  Samples         Score        Error  Units
      o.a.l.l.p.j.StringEncBenchmark.GetBytesCharSetDefault     thrpt        5  12028518.507 ±  93571.792  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytes                   thrpt        5  11548051.157 ±  45019.349  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesStringDefault      thrpt        5  11033635.427 ± 103410.346  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesCharSet88591       thrpt        5   4950536.544 ±  44039.456  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesString88591        thrpt        5   4161493.706 ±  31591.263  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesStringShiftJIS     thrpt        5   2312330.720 ±   9396.859  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesCharSetShiftJIS    thrpt        5   1312534.481 ±  26186.142  ops/s
      
      
      jdk1.8.0_11
      Benchmark                                                  Mode  Samples         Score        Error  Units
      o.a.l.l.p.j.StringEncBenchmark.GetBytesCharSet88591       thrpt        5  33946446.764 ± 933451.351  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesString88591        thrpt        5  20917837.751 ± 257779.242  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesStringDefault      thrpt        5  13328817.946 ± 151911.634  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytes                   thrpt        5  13096046.662 ±  77208.908  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesCharSetDefault     thrpt        5  12356403.648 ± 170268.254  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesStringShiftJIS     thrpt        5   4261255.777 ±  33116.543  ops/s
      o.a.l.l.p.j.StringEncBenchmark.GetBytesCharSetShiftJIS    thrpt        5   3478309.565 ±  78097.111  ops/s
      

      Attachments

        Issue Links

          Activity

            People

              rpopma Remko Popma
              rpopma Remko Popma
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: