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

Pool lexers to reduce allocations and improve performance #1610

Merged
merged 4 commits into from
May 11, 2022

Conversation

turbolent
Copy link
Member

@turbolent turbolent commented Apr 28, 2022

Description

@simonhf pointed out that execution is allocating a lot of memory, specifically emitting tokens in the token stream (append on a slice). Pre-allocating an array reduces the overhead of append.

I then also realized that the token stream itself is allocated and not used after parsing, so we can reduce allocations by introducing an object pool for lexers.

This significantly reduces amount of allocated memory and also increases performance as a side-effect.

Also, improve the benchmarking workflow:

  • Enable memory statistics for all benchmarks
  • Sort benchmarks by name. This makes it easier to find benchmarks in the output (e.g. see performance improvement -> check memory benchmark results)

  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work
  • Code follows the standards mentioned here
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

@codecov
Copy link

codecov bot commented Apr 28, 2022

Codecov Report

Merging #1610 (7d84b90) into master (f615bea) will increase coverage by 0.01%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #1610      +/-   ##
==========================================
+ Coverage   74.73%   74.74%   +0.01%     
==========================================
  Files         288      288              
  Lines       55340    55356      +16     
==========================================
+ Hits        41357    41375      +18     
+ Misses      12489    12487       -2     
  Partials     1494     1494              
Flag Coverage Δ
unittests 74.74% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
runtime/parser2/lexer/lexer.go 96.15% <100.00%> (+0.20%) ⬆️
runtime/parser2/parser.go 90.33% <100.00%> (+0.08%) ⬆️
runtime/interpreter/storage.go 72.78% <0.00%> (+1.36%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f615bea...7d84b90. Read the comment docs.

@github-actions
Copy link

github-actions bot commented Apr 28, 2022

Cadence Benchstat comparison

This branch with compared with the base branch onflow:master commit f615bea
The command for i in {1..N}; do go test ./... -run=XXX -bench=. -benchmem -shuffle=on; done was used.
Bench tests were run a total of 7 times on each branch.

Results

old.txtnew.txt
time/opdelta
CheckContractInterfaceFungibleTokenConformance-2164µs ± 7%157µs ± 3%~(p=0.101 n=7+6)
ContractInterfaceFungibleToken-245.1µs ± 9%44.8µs ±14%~(p=0.710 n=7+7)
InterpretRecursionFib-22.90ms ± 9%2.75ms ± 4%~(p=0.234 n=7+6)
NewInterpreter/new_interpreter-21.26µs ± 9%1.25µs ± 8%~(p=0.902 n=7+7)
NewInterpreter/new_sub-interpreter-22.53µs ± 9%2.39µs ± 8%~(p=0.128 n=7+7)
ParseArray-214.4ms ± 9%8.0ms ± 5%−44.59%(p=0.001 n=7+7)
ParseDeploy/byte_array-222.4ms ±10%11.7ms ± 3%−47.66%(p=0.001 n=7+7)
ParseDeploy/decode_hex-21.32ms ± 7%1.28ms ± 6%~(p=0.318 n=7+7)
ParseFungibleToken-2211µs ± 3%152µs ± 4%−27.76%(p=0.001 n=6+7)
ParseInfix-29.32µs ±10%6.97µs ± 8%−25.17%(p=0.001 n=7+7)
QualifiedIdentifierCreation/One_level-23.07ns ± 8%2.96ns ± 6%~(p=0.259 n=7+7)
QualifiedIdentifierCreation/Three_levels-2155ns ± 8%152ns ± 8%~(p=0.456 n=7+7)
RuntimeFungibleTokenTransfer-21.51ms ±26%1.60ms ± 8%~(p=0.731 n=7+6)
RuntimeResourceDictionaryValues-27.56ms ±12%7.39ms ± 6%~(p=0.620 n=7+7)
Transfer-294.3ns ± 9%95.1ns ± 8%~(p=0.535 n=7+7)
 
alloc/opdelta
CheckContractInterfaceFungibleTokenConformance-266.3kB ± 0%66.3kB ± 0%~(p=1.000 n=7+7)
ContractInterfaceFungibleToken-226.7kB ± 0%26.7kB ± 0%+0.00%(p=0.033 n=6+7)
InterpretRecursionFib-21.14MB ± 0%1.14MB ± 0%~(p=1.000 n=7+7)
NewInterpreter/new_interpreter-2848B ± 0%848B ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-21.34kB ± 0%1.34kB ± 0%~(all equal)
ParseArray-213.5MB ± 0%3.0MB ± 0%−77.91%(p=0.001 n=7+6)
ParseDeploy/byte_array-221.0MB ± 0%4.3MB ± 2%−79.56%(p=0.001 n=7+7)
ParseDeploy/decode_hex-2218kB ± 0%213kB ± 0%−2.08%(p=0.001 n=7+7)
ParseFungibleToken-2199kB ± 0%36kB ± 0%−81.77%(p=0.001 n=7+7)
ParseInfix-26.76kB ± 0%2.10kB ± 0%−68.87%(p=0.001 n=7+7)
QualifiedIdentifierCreation/One_level-20.00B 0.00B ~(all equal)
QualifiedIdentifierCreation/Three_levels-264.0B ± 0%64.0B ± 0%~(all equal)
RuntimeFungibleTokenTransfer-2273kB ± 0%234kB ± 0%−14.27%(p=0.001 n=7+7)
RuntimeResourceDictionaryValues-22.25MB ± 0%2.24MB ± 0%−0.53%(p=0.001 n=7+7)
Transfer-248.0B ± 0%48.0B ± 0%~(all equal)
 
allocs/opdelta
CheckContractInterfaceFungibleTokenConformance-21.07k ± 0%1.07k ± 0%~(all equal)
ContractInterfaceFungibleToken-2460 ± 0%460 ± 0%~(all equal)
InterpretRecursionFib-223.8k ± 0%23.8k ± 0%~(all equal)
NewInterpreter/new_interpreter-213.0 ± 0%13.0 ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-240.0 ± 0%40.0 ± 0%~(all equal)
ParseArray-270.0k ± 0%70.0k ± 0%−0.03%(p=0.001 n=7+6)
ParseDeploy/byte_array-2105k ± 0%105k ± 0%−0.02%(p=0.000 n=7+6)
ParseDeploy/decode_hex-286.0 ± 0%79.0 ± 0%−8.14%(p=0.001 n=7+7)
ParseFungibleToken-21.07k ± 0%1.06k ± 0%−1.12%(p=0.001 n=7+7)
ParseInfix-273.0 ± 0%66.0 ± 0%−9.59%(p=0.001 n=7+7)
QualifiedIdentifierCreation/One_level-20.00 0.00 ~(all equal)
QualifiedIdentifierCreation/Three_levels-22.00 ± 0%2.00 ± 0%~(all equal)
RuntimeFungibleTokenTransfer-24.58k ± 0%4.57k ± 0%−0.22%(p=0.001 n=7+7)
RuntimeResourceDictionaryValues-237.6k ± 0%37.6k ± 0%−0.03%(p=0.000 n=7+5)
Transfer-21.00 ± 0%1.00 ± 0%~(all equal)
 

@turbolent turbolent changed the title Pre-allocate tokens Pool lexers Apr 28, 2022
@turbolent turbolent changed the title Pool lexers Pool lexers to reduce allocations and improve performance Apr 28, 2022
@turbolent turbolent self-assigned this Apr 28, 2022
@turbolent turbolent marked this pull request as ready for review April 28, 2022 16:34
Copy link
Member

@SupunS SupunS left a comment

Choose a reason for hiding this comment

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

Nice optimization! 👌 Just a one question

Copy link
Contributor

@janezpodhostnik janezpodhostnik left a comment

Choose a reason for hiding this comment

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

LGTM.

@turbolent turbolent merged commit 26cd4b7 into master May 11, 2022
@turbolent turbolent deleted the bastian/optimize-lexer branch May 11, 2022 20:58
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.

5 participants