[ 
https://issues.apache.org/jira/browse/LOG4J2-1151?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14944226#comment-14944226
 ] 

Remko Popma commented on LOG4J2-1151:
-------------------------------------

I overlooked something: invalid input. The JDK code checks if the specified 
characters are really single-byte, and if invalid they are represented with a 
'?'.

I tried to accomodate for this but it impacts performance quite a bit. 
Explanation of benchmarks below:
* "custom cast-to-byte": simply cast each char to a byte without checking if it 
is a single byte char or not
* "ported JDK 8": copy of the JDK 8 code
* "verify and cast": attempt to do simple check in addition to cast-to-byte 
(not a JDK 8 port)

*Conclusions*
* On JDK 8, it is better to simply use the built-in String.getBytes methods
* On JDK 7, the ported JDK 8 code gives best performance without sacrificing 
functionality (invalid input is handled as with the built-in String.getBytes 
methods)


*jdk 1.8.0_60 (64 bit Hotspot)*

{code}
Benchmark                                                                   
Mode  Samples    Score    Error  Units
o.a.l.l.p.j.StringEncodingBenchmark.iso8859_1CustomCastToByte             
sample    99563   87.159 ± 23.752  ns/op
o.a.l.l.p.j.StringEncodingBenchmark.iso8859_1CustomPortedJDK8             
sample   153776  149.088 ±  2.656  ns/op
o.a.l.l.p.j.StringEncodingBenchmark.iso8859_1CustomVerifyAndCast          
sample    99694  127.062 ±  2.692  ns/op
o.a.l.l.p.j.StringEncodingBenchmark.iso8859_1StringGetBytesCharSet        
sample   112143   80.945 ±  2.699  ns/op
o.a.l.l.p.j.StringEncodingBenchmark.iso8859_1StringGetBytesString         
sample   169835   94.945 ± 17.172  ns/op
o.a.l.l.p.j.StringEncodingBenchmark.usAsciiStringGetBytesCharSet          
sample   174893  136.186 ±  2.051  ns/op
o.a.l.l.p.j.StringEncodingBenchmark.usAsciiStringGetBytesString           
sample   151682  157.029 ± 13.066  ns/op
{code}

*jdk 1.7.0_55 (64 bit Hotspot)*

{code}
Benchmark                                                                   
Mode  Samples     Score    Error  Units
o.a.l.l.p.j.StringEncodingBenchmark.iso8859_1CustomCastToByte             
sample   101091    84.999 ±  3.336  ns/op
o.a.l.l.p.j.StringEncodingBenchmark.iso8859_1CustomPortedJDK8             
sample   171849   149.490 ± 12.233  ns/op
o.a.l.l.p.j.StringEncodingBenchmark.iso8859_1CustomVerifyAndCast          
sample   150746   302.352 ±  2.940  ns/op
o.a.l.l.p.j.StringEncodingBenchmark.iso8859_1StringGetBytesCharSet        
sample   196531   244.018 ± 18.239  ns/op
o.a.l.l.p.j.StringEncodingBenchmark.iso8859_1StringGetBytesString         
sample   146952   306.058 ±  2.919  ns/op
o.a.l.l.p.j.StringEncodingBenchmark.usAsciiStringGetBytesCharSet          
sample   194692   250.779 ± 12.239  ns/op
o.a.l.l.p.j.StringEncodingBenchmark.usAsciiStringGetBytesString           
sample   143561   316.768 ±  3.482  ns/op
{code}


> Performance improvement: backport fast Java 8 ISO-8859-1 String to byte[] 
> encoder to AbstractStringLayout
> ---------------------------------------------------------------------------------------------------------
>
>                 Key: LOG4J2-1151
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1151
>             Project: Log4j 2
>          Issue Type: Improvement
>          Components: Layouts
>    Affects Versions: 2.4
>            Reporter: Remko Popma
>            Assignee: Remko Popma
>             Fix For: 2.4.1
>
>
> Java 8 made large improvements to String encoding performance for all 
> character sets. Especially for the ISO8859_1 encoding, Java 8 introduced 
> logic that simply casts each char in the array to a byte, giving a huge 
> performance increase.
> This ticket proposes to backport that logic into AbstractStringLayout to get 
> the same performance in Java 7.
> Focussing on the results for ISO8859_1:
> {quote}
> Java 7 String.getBytes(): ~200-300 ns/op
> Java 8 String.getBytes(): ~70-80 ns/op
> Custom logic: ~90 ns/op (on both Java 7 and 8)
> {quote}
> Benchmarks below are with a representative message string of 100 characters, 
> on 64 bit Windows 10, Dual Intel Core i5-3317U @1.70GHz with hyperthreading 
> on (4 cores).
> *jdk 1.8.0_60 (64 bit Hotspot)*
> {code}
> Benchmark                                                              Mode  
> Samples    Score    Error  Units
> o.a.l.l.p.j.StringEncodingBenchmark.customIso8859_1                  sample   
> 156848   94.014 ±  2.362  ns/op <- custom
> o.a.l.l.p.j.StringEncodingBenchmark.encoderIso8859_1                 sample   
> 103773  859.529 ± 27.461  ns/op
> o.a.l.l.p.j.StringEncodingBenchmark.stringGetBytes                   sample   
> 125785  363.129 ± 17.674  ns/op
> o.a.l.l.p.j.StringEncodingBenchmark.stringGetBytesCharSet88591       sample   
> 117191   72.606 ±  2.771  ns/op <- variation 1
> o.a.l.l.p.j.StringEncodingBenchmark.stringGetBytesCharSetShiftJIS    sample   
> 102914  452.721 ± 21.274  ns/op
> o.a.l.l.p.j.StringEncodingBenchmark.stringGetBytesString88591        sample   
> 181547   79.653 ±  1.735  ns/op <- variation 2
> o.a.l.l.p.j.StringEncodingBenchmark.stringGetBytesStringShiftJIS     sample   
> 125817  354.961 ±  4.072  ns/op
> {code}
> *jdk 1.7.0_55 (64 bit Hotspot)*
> {code}
> Benchmark                                                              Mode  
> Samples     Score    Error  Units
> o.a.l.l.p.j.StringEncodingBenchmark.customIso8859_1                  sample   
> 148456    93.248 ±  2.193  ns/op <- custom
> o.a.l.l.p.j.StringEncodingBenchmark.encoderIso8859_1                 sample   
> 176198   516.731 ± 24.054  ns/op
> o.a.l.l.p.j.StringEncodingBenchmark.stringGetBytes                   sample   
> 121660   731.530 ± 27.116  ns/op
> o.a.l.l.p.j.StringEncodingBenchmark.stringGetBytesCharSet88591       sample   
>  99636   234.709 ±  3.234  ns/op <- variation 1
> o.a.l.l.p.j.StringEncodingBenchmark.stringGetBytesCharSetShiftJIS    sample   
> 125754  1401.008 ± 25.385  ns/op
> o.a.l.l.p.j.StringEncodingBenchmark.stringGetBytesString88591        sample   
> 147124   316.411 ± 21.454  ns/op <- variation 2
> o.a.l.l.p.j.StringEncodingBenchmark.stringGetBytesStringShiftJIS     sample   
> 114733   808.315 ± 27.177  ns/op
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to