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

Override next() and endOfJson() methods because JIT C2 keeps de-optimizing them #185

Merged
merged 1 commit into from
Oct 3, 2024

Conversation

donavdey
Copy link
Collaborator

@donavdey donavdey commented Sep 29, 2024

@gavlyukovskiy's benchmarks chart shows that streaming mode PR degrades performance of byte array mode by ~30%. Upon further examination, it turned out that JIT C2 de-optimizes next() and endOfJson() methods (the most performance critical methods) because of a new reloadBuffer() condition. Logically, this condition is never triggered in byte array mode, but apparently the condition invalidates some of JIT assumptions and previous optimizations.
This PR decouples buffering logic from MaskingState to keep JIT optimizations for next() and endOfJson() intact for byte array API.

The preformance chart:
image

@donavdey donavdey changed the title Override next() and endOfJson() methods as JIT C2 keeps de-optimizing them Override next() and endOfJson() methods because JIT C2 keeps de-optimizing them Sep 29, 2024
Copy link

github-actions bot commented Sep 29, 2024

Note

These results are affected by shared workloads on GitHub runners. Use the results only to detect possible regressions, but always rerun on more stable machine before making any conclusions!

Benchmark results (pull-request, 5aa6566)

Benchmark                                                           (characters)  (jsonPath)  (jsonSize)  (keyLength)  (maskedKeyProbability)  (numberOfTargetKeys)  (streamInputType)  (streamOutputType)   Mode  Cnt         Score          Error   Units
BaselineBenchmark.countBytes                                             unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4   2585327.508 ±   173400.393   ops/s
BaselineBenchmark.countBytes:gc.alloc.rate.norm                          unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4         0.001 ±        0.001    B/op
BaselineBenchmark.jacksonParseAndMask                                    unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4     29891.306 ±      774.213   ops/s
BaselineBenchmark.jacksonParseAndMask:gc.alloc.rate.norm                 unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4     64360.071 ±        0.046    B/op
BaselineBenchmark.jacksonParseOnly                                       unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4     49545.539 ±     1101.083   ops/s
BaselineBenchmark.jacksonParseOnly:gc.alloc.rate.norm                    unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4     24312.042 ±        0.029    B/op
BaselineBenchmark.regexReplace                                           unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4      3297.635 ±       87.032   ops/s
BaselineBenchmark.regexReplace:gc.alloc.rate.norm                        unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4     61656.638 ±        0.438    B/op
BaselineBenchmark.writeFile                                              unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4      3934.729 ±     1785.057   ops/s
BaselineBenchmark.writeFile:gc.alloc.rate.norm                           unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4     10664.532 ±        0.186    B/op
InstanceCreationBenchmark.jsonMasker                                         N/A         N/A         N/A          N/A                     N/A                  1000                N/A                 N/A  thrpt    4       646.344 ±       24.777   ops/s
InstanceCreationBenchmark.jsonMasker:gc.alloc.rate.norm                      N/A         N/A         N/A          N/A                     N/A                  1000                N/A                 N/A  thrpt    4   2796876.295 ±        5.361    B/op
JsonMaskerBenchmark.jsonMaskerByteArrayStreams                           unicode       false         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4    240961.473 ±     2178.964   ops/s
JsonMaskerBenchmark.jsonMaskerByteArrayStreams:gc.alloc.rate.norm        unicode       false         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4     10816.008 ±        0.001    B/op
JsonMaskerBenchmark.jsonMaskerByteArrayStreams                           unicode        true         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4    227197.828 ±     6235.494   ops/s
JsonMaskerBenchmark.jsonMaskerByteArrayStreams:gc.alloc.rate.norm        unicode        true         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4     11560.009 ±        0.001    B/op
JsonMaskerBenchmark.jsonMaskerBytes                                      unicode       false         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4    429341.562 ±    10313.013   ops/s
JsonMaskerBenchmark.jsonMaskerBytes:gc.alloc.rate.norm                   unicode       false         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4      2240.005 ±        0.001    B/op
JsonMaskerBenchmark.jsonMaskerBytes                                      unicode        true         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4    456095.474 ±    11347.753   ops/s
JsonMaskerBenchmark.jsonMaskerBytes:gc.alloc.rate.norm                   unicode        true         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4      1392.004 ±        0.001    B/op
JsonMaskerBenchmark.jsonMaskerString                                     unicode       false         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4    223573.281 ±     3543.870   ops/s
JsonMaskerBenchmark.jsonMaskerString:gc.alloc.rate.norm                  unicode       false         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4     10144.009 ±        0.001    B/op
JsonMaskerBenchmark.jsonMaskerString                                     unicode        true         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4    203927.611 ±     4862.871   ops/s
JsonMaskerBenchmark.jsonMaskerString:gc.alloc.rate.norm                  unicode        true         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4     10312.010 ±        0.001    B/op
LargeKeySetInstanceCreationBenchmark.jsonMasker                              N/A         N/A         N/A          100                     N/A                  1000                N/A                 N/A  thrpt    4         9.949 ±        1.890   ops/s
LargeKeySetInstanceCreationBenchmark.jsonMasker:gc.alloc.rate.norm           N/A         N/A         N/A          100                     N/A                  1000                N/A                 N/A  thrpt    4  65715554.325 ± 13620690.109    B/op
StreamTypeBenchmark.jsonMaskerStreams                                        N/A         N/A         1kb          N/A                     N/A                   N/A    ByteArrayStream     ByteArrayStream  thrpt    4    192172.159 ±     6610.340   ops/s
StreamTypeBenchmark.jsonMaskerStreams:gc.alloc.rate.norm                     N/A         N/A         1kb          N/A                     N/A                   N/A    ByteArrayStream     ByteArrayStream  thrpt    4     11560.011 ±        0.009    B/op
StreamTypeBenchmark.jsonMaskerStreams                                        N/A         N/A         1kb          N/A                     N/A                   N/A    ByteArrayStream          FileStream  thrpt    4      3542.720 ±     2092.232   ops/s
StreamTypeBenchmark.jsonMaskerStreams:gc.alloc.rate.norm                     N/A         N/A         1kb          N/A                     N/A                   N/A    ByteArrayStream          FileStream  thrpt    4      8600.594 ±        0.219    B/op
StreamTypeBenchmark.jsonMaskerStreams                                        N/A         N/A         1kb          N/A                     N/A                   N/A         FileStream     ByteArrayStream  thrpt    4     85713.409 ±     3058.500   ops/s
StreamTypeBenchmark.jsonMaskerStreams:gc.alloc.rate.norm                     N/A         N/A         1kb          N/A                     N/A                   N/A         FileStream     ByteArrayStream  thrpt    4     11688.025 ±        0.021    B/op
StreamTypeBenchmark.jsonMaskerStreams                                        N/A         N/A         1kb          N/A                     N/A                   N/A         FileStream          FileStream  thrpt    4      3887.383 ±     2300.627   ops/s
StreamTypeBenchmark.jsonMaskerStreams:gc.alloc.rate.norm                     N/A         N/A         1kb          N/A                     N/A                   N/A         FileStream          FileStream  thrpt    4      8728.543 ±        0.242    B/op
ValueMaskerBenchmark.maskWithRawValueFunction                            unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4    608624.449 ±    13969.214   ops/s
ValueMaskerBenchmark.maskWithRawValueFunction:gc.alloc.rate.norm         unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4      1600.003 ±        0.001    B/op
ValueMaskerBenchmark.maskWithStatic                                      unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4    669668.743 ±    19126.847   ops/s
ValueMaskerBenchmark.maskWithStatic:gc.alloc.rate.norm                   unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4      1240.003 ±        0.001    B/op
ValueMaskerBenchmark.maskWithTextValueFunction                           unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4    522610.592 ±    17825.049   ops/s
ValueMaskerBenchmark.maskWithTextValueFunction:gc.alloc.rate.norm        unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4      1888.004 ±        0.001    B/op

Benchmark results (master, 9f0690f)

Benchmark                                                           (characters)  (jsonPath)  (jsonSize)  (keyLength)  (maskedKeyProbability)  (numberOfTargetKeys)  (streamInputType)  (streamOutputType)   Mode  Cnt         Score          Error   Units
BaselineBenchmark.countBytes                                             unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4   2594523.890 ±   137613.553   ops/s
BaselineBenchmark.countBytes:gc.alloc.rate.norm                          unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4         0.001 ±        0.001    B/op
BaselineBenchmark.jacksonParseAndMask                                    unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4     29525.026 ±      770.102   ops/s
BaselineBenchmark.jacksonParseAndMask:gc.alloc.rate.norm                 unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4     64400.072 ±        0.047    B/op
BaselineBenchmark.jacksonParseOnly                                       unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4     50165.445 ±     1696.802   ops/s
BaselineBenchmark.jacksonParseOnly:gc.alloc.rate.norm                    unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4     24312.042 ±        0.030    B/op
BaselineBenchmark.regexReplace                                           unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4      5289.468 ±       51.475   ops/s
BaselineBenchmark.regexReplace:gc.alloc.rate.norm                        unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4     61656.398 ±        0.273    B/op
BaselineBenchmark.writeFile                                              unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4      4271.258 ±     1124.191   ops/s
BaselineBenchmark.writeFile:gc.alloc.rate.norm                           unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4     10664.491 ±        0.225    B/op
InstanceCreationBenchmark.jsonMasker                                         N/A         N/A         N/A          N/A                     N/A                  1000                N/A                 N/A  thrpt    4       673.879 ±        2.458   ops/s
InstanceCreationBenchmark.jsonMasker:gc.alloc.rate.norm                      N/A         N/A         N/A          N/A                     N/A                  1000                N/A                 N/A  thrpt    4   2638443.705 ±        5.250    B/op
JsonMaskerBenchmark.jsonMaskerByteArrayStreams                           unicode       false         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4    249175.225 ±     4845.940   ops/s
JsonMaskerBenchmark.jsonMaskerByteArrayStreams:gc.alloc.rate.norm        unicode       false         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4     10816.008 ±        0.001    B/op
JsonMaskerBenchmark.jsonMaskerByteArrayStreams                           unicode        true         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4    286544.860 ±     4058.838   ops/s
JsonMaskerBenchmark.jsonMaskerByteArrayStreams:gc.alloc.rate.norm        unicode        true         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4     11560.007 ±        0.001    B/op
JsonMaskerBenchmark.jsonMaskerBytes                                      unicode       false         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4    319078.028 ±     5939.419   ops/s
JsonMaskerBenchmark.jsonMaskerBytes:gc.alloc.rate.norm                   unicode       false         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4      2256.006 ±        0.001    B/op
JsonMaskerBenchmark.jsonMaskerBytes                                      unicode        true         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4    393461.727 ±    15494.485   ops/s
JsonMaskerBenchmark.jsonMaskerBytes:gc.alloc.rate.norm                   unicode        true         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4      1408.005 ±        0.001    B/op
JsonMaskerBenchmark.jsonMaskerString                                     unicode       false         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4    199802.434 ±     4687.642   ops/s
JsonMaskerBenchmark.jsonMaskerString:gc.alloc.rate.norm                  unicode       false         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4     10160.010 ±        0.001    B/op
JsonMaskerBenchmark.jsonMaskerString                                     unicode        true         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4    201062.299 ±     1522.368   ops/s
JsonMaskerBenchmark.jsonMaskerString:gc.alloc.rate.norm                  unicode        true         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4     10328.010 ±        0.001    B/op
LargeKeySetInstanceCreationBenchmark.jsonMasker                              N/A         N/A         N/A          100                     N/A                  1000                N/A                 N/A  thrpt    4         9.922 ±        5.359   ops/s
LargeKeySetInstanceCreationBenchmark.jsonMasker:gc.alloc.rate.norm           N/A         N/A         N/A          100                     N/A                  1000                N/A                 N/A  thrpt    4  59128749.289 ± 13728888.470    B/op
StreamTypeBenchmark.jsonMaskerStreams                                        N/A         N/A         1kb          N/A                     N/A                   N/A    ByteArrayStream     ByteArrayStream  thrpt    4    288021.806 ±    12455.750   ops/s
StreamTypeBenchmark.jsonMaskerStreams:gc.alloc.rate.norm                     N/A         N/A         1kb          N/A                     N/A                   N/A    ByteArrayStream     ByteArrayStream  thrpt    4     11560.007 ±        0.006    B/op
StreamTypeBenchmark.jsonMaskerStreams                                        N/A         N/A         1kb          N/A                     N/A                   N/A    ByteArrayStream          FileStream  thrpt    4      4097.317 ±     2718.311   ops/s
StreamTypeBenchmark.jsonMaskerStreams:gc.alloc.rate.norm                     N/A         N/A         1kb          N/A                     N/A                   N/A    ByteArrayStream          FileStream  thrpt    4      8600.519 ±        0.503    B/op
StreamTypeBenchmark.jsonMaskerStreams                                        N/A         N/A         1kb          N/A                     N/A                   N/A         FileStream     ByteArrayStream  thrpt    4     89602.257 ±     1131.023   ops/s
StreamTypeBenchmark.jsonMaskerStreams:gc.alloc.rate.norm                     N/A         N/A         1kb          N/A                     N/A                   N/A         FileStream     ByteArrayStream  thrpt    4     11704.024 ±        0.020    B/op
StreamTypeBenchmark.jsonMaskerStreams                                        N/A         N/A         1kb          N/A                     N/A                   N/A         FileStream          FileStream  thrpt    4      3664.378 ±      972.793   ops/s
StreamTypeBenchmark.jsonMaskerStreams:gc.alloc.rate.norm                     N/A         N/A         1kb          N/A                     N/A                   N/A         FileStream          FileStream  thrpt    4      8760.574 ±        0.317    B/op
ValueMaskerBenchmark.maskWithRawValueFunction                            unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4    408223.177 ±     9581.712   ops/s
ValueMaskerBenchmark.maskWithRawValueFunction:gc.alloc.rate.norm         unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4      1616.005 ±        0.001    B/op
ValueMaskerBenchmark.maskWithStatic                                      unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4    425712.475 ±    17256.799   ops/s
ValueMaskerBenchmark.maskWithStatic:gc.alloc.rate.norm                   unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4      1256.005 ±        0.001    B/op
ValueMaskerBenchmark.maskWithTextValueFunction                           unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4    349979.092 ±     3193.310   ops/s
ValueMaskerBenchmark.maskWithTextValueFunction:gc.alloc.rate.norm        unicode         N/A         1kb          N/A                     0.1                   N/A                N/A                 N/A  thrpt    4      1904.006 ±        0.001    B/op

@donavdey donavdey self-assigned this Sep 29, 2024
Copy link

sonarcloud bot commented Oct 3, 2024

Copy link
Collaborator

@gavlyukovskiy gavlyukovskiy left a comment

Choose a reason for hiding this comment

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

Nice, so after all you went with decoupling them 😀 To me, that now looks much better

@donavdey donavdey merged commit 72d1f4b into master Oct 3, 2024
4 checks passed
@donavdey donavdey deleted the buffered-masking-state branch October 3, 2024 17:09
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.

2 participants