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

Optimize HashInput functions to use scratch buffer provided by onflow/atree #1296

Merged
merged 5 commits into from
Dec 9, 2021

Conversation

fxamacker
Copy link
Member

Closes #1258

Description

Optimize HashInput functions to use a small scratch buffer provided by onflow/atree.


  • 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-commenter
Copy link

codecov-commenter commented Dec 4, 2021

Codecov Report

Merging #1296 (60c41f9) into master (651817e) will increase coverage by 0.01%.
The diff coverage is 92.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1296      +/-   ##
==========================================
+ Coverage   77.39%   77.41%   +0.01%     
==========================================
  Files         279      279              
  Lines       36048    36113      +65     
==========================================
+ Hits        27899    27956      +57     
- Misses       7062     7066       +4     
- Partials     1087     1091       +4     
Flag Coverage Δ
unittests 77.41% <92.66%> (+0.01%) ⬆️

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

Impacted Files Coverage Δ
runtime/interpreter/value.go 80.21% <92.66%> (+0.10%) ⬆️

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 651817e...60c41f9. Read the comment docs.

@github-actions
Copy link

github-actions bot commented Dec 4, 2021

Cadence Benchstat comparison

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

Results

old.txtnew.txt
time/opdelta
RuntimeResourceDictionaryValues-214.0ms ± 5%13.8ms ± 2%~(p=0.209 n=7+7)
RuntimeFungibleTokenTransfer-21.22ms ±29%1.43ms ± 4%~(p=0.138 n=7+6)
ParseDeploy/byte_array-227.6ms ± 4%27.2ms ± 4%~(p=0.805 n=7+7)
ParseDeploy/decode_hex-21.10ms ± 4%1.07ms ± 1%~(p=0.051 n=7+6)
ParseArray-218.5ms ± 6%17.8ms ± 2%~(p=0.295 n=7+6)
QualifiedIdentifierCreation/One_level-22.08ns ± 1%2.07ns ± 0%~(p=0.461 n=7+7)
QualifiedIdentifierCreation/Three_levels-2130ns ± 3%130ns ± 7%~(p=0.778 n=7+7)
ContractInterfaceFungibleToken-237.3µs ± 6%37.5µs ± 3%~(p=0.620 n=7+7)
CheckContractInterfaceFungibleTokenConformance-2125µs ± 6%125µs ± 4%~(p=1.000 n=7+7)
InterpretRecursionFib-22.31ms ±10%2.24ms ± 9%~(p=0.535 n=7+7)
NewInterpreter/new_interpreter-2929ns ± 4%919ns ± 4%~(p=0.445 n=7+6)
ParseInfix-220.0µs ± 4%19.2µs ± 1%−3.98%(p=0.008 n=7+6)
NewInterpreter/new_sub-interpreter-21.80µs ± 4%1.72µs ± 8%−4.36%(p=0.026 n=7+7)
ParseFungibleToken-2379µs ± 2%359µs ± 1%−5.39%(p=0.002 n=6+6)
 
alloc/opdelta
RuntimeResourceDictionaryValues-24.04MB ± 0%4.04MB ± 0%~(p=0.181 n=6+7)
RuntimeFungibleTokenTransfer-2238kB ± 0%238kB ± 0%~(p=0.710 n=7+7)
QualifiedIdentifierCreation/One_level-20.00B 0.00B ~(all equal)
QualifiedIdentifierCreation/Three_levels-264.0B ± 0%64.0B ± 0%~(all equal)
ContractInterfaceFungibleToken-226.5kB ± 0%26.5kB ± 0%~(all equal)
CheckContractInterfaceFungibleTokenConformance-265.7kB ± 0%65.7kB ± 0%~(p=1.000 n=7+7)
InterpretRecursionFib-21.24MB ± 0%1.24MB ± 0%~(p=0.580 n=6+7)
NewInterpreter/new_interpreter-2720B ± 0%720B ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-21.11kB ± 0%1.11kB ± 0%~(all equal)
 
allocs/opdelta
QualifiedIdentifierCreation/One_level-20.00 0.00 ~(all equal)
QualifiedIdentifierCreation/Three_levels-22.00 ± 0%2.00 ± 0%~(all equal)
ContractInterfaceFungibleToken-2457 ± 0%457 ± 0%~(all equal)
CheckContractInterfaceFungibleTokenConformance-21.07k ± 0%1.07k ± 0%~(all equal)
InterpretRecursionFib-225.0k ± 0%25.0k ± 0%~(all equal)
NewInterpreter/new_interpreter-211.0 ± 0%11.0 ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-232.0 ± 0%32.0 ± 0%~(all equal)
RuntimeResourceDictionaryValues-2102k ± 0%102k ± 0%−0.00%(p=0.004 n=7+7)
RuntimeFungibleTokenTransfer-24.53k ± 0%4.53k ± 0%−0.02%(p=0.042 n=7+6)
 

Copy link
Member

@turbolent turbolent left a comment

Choose a reason for hiding this comment

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

Nice! Thank you for optimizing this 👍

Comment on lines +349 to +353
if length <= len(scratch) {
buf = scratch[:length]
} else {
buf = make([]byte, length)
}
Copy link
Member

Choose a reason for hiding this comment

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

Good idea!

Comment on lines +2314 to +2316
// HashInput returns a byte slice containing:
// - HashInputTypeInt8 (1 byte)
// - int8 value (1 byte)
Copy link
Member

Choose a reason for hiding this comment

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

Thank you for also adding comments to the unchanged functions 👍

@turbolent
Copy link
Member

@fxamacker It looks like coverage went down as the new cases where the input does not fit into the scratch are not covered by tests (e.g. https://app.codecov.io/gh/onflow/cadence/compare/1296/tree/runtime/interpreter/value.go#D1L352).

Could you please add some tests with longer/larger values? Thank you!

@fxamacker
Copy link
Member Author

@fxamacker It looks like coverage went down as the new cases where the input does not fit into the scratch are not covered by tests (e.g. https://app.codecov.io/gh/onflow/cadence/compare/1296/tree/runtime/interpreter/value.go#D1L352).

Could you please add some tests with longer/larger values? Thank you!

Yikes! Thanks for catching this! 👍 It would've been bad to merge with this mistake. I added more tests. Many thanks!

@fxamacker fxamacker requested a review from turbolent December 9, 2021 17:54
Copy link
Member

@turbolent turbolent left a comment

Choose a reason for hiding this comment

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

Nice!

@@ -1116,82 +1118,242 @@ func TestGetHashInput(t *testing.T) {
value: NewUIntValueFromUint64(10),
expected: []byte{byte(HashInputTypeUInt), 10},
},
"UInt min": {
Copy link
Member

Choose a reason for hiding this comment

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

Great idea to use min & max values 👍

@fxamacker fxamacker merged commit a9ef2ce into master Dec 9, 2021
@turbolent turbolent deleted the fxamacker/optimize-hashinput branch March 1, 2022 00:36
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.

Optimize HashInput
3 participants