Skip to content

[tstate] inline byte to string conversions #174

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

hexfusion
Copy link
Contributor

@hexfusion hexfusion commented May 15, 2023

I explored a few alternative approaches and generally found this PR does provide some small gains that could be considered as an incremental performance improvement. During research I found that using a 64bit hash such as xxhash64 for the map key was a large performance improvement this is a the approach taken with for example fast cache. But since we also need to handle hash collisions the complexity reduced performance until it was not worth the effort at small scale.

This approach is documented here https://pthevenet.com/posts/programming/go/bytesliceindexedmaps/#optimization local results from the benchmark confirm.

goos: linux
goarch: amd64
pkg: fastcache
cpu: Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
BenchmarkArrayKeyed-12              	18654550	        64.44 ns/op	       0 B/op	       0 allocs/op
BenchmarkOptimizedStringKeyed-12    	14765403	        86.89 ns/op	      16 B/op	       1 allocs/op
BenchmarkStringKeyed-12             	10360284	       125.4 ns/op	      48 B/op	       3 allocs/op
BenchmarkHexKeyed-12                	 6014667	       206.9 ns/op	      96 B/op	       4 allocs/op

before

goos: linux
goarch: amd64
pkg: github.com/ava-labs/hypersdk/tstate
cpu: Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
BenchmarkFetchAndSetScope
BenchmarkFetchAndSetScope/fetch_and_set_scope_4_keys
BenchmarkFetchAndSetScope/fetch_and_set_scope_4_keys-12                   885832              1440 ns/op             640 B/op          7 allocs/op
BenchmarkFetchAndSetScope/fetch_and_set_scope_8_keys
BenchmarkFetchAndSetScope/fetch_and_set_scope_8_keys-12                   403532              3230 ns/op            1537 B/op         12 allocs/op
BenchmarkFetchAndSetScope/fetch_and_set_scope_16_keys
BenchmarkFetchAndSetScope/fetch_and_set_scope_16_keys-12                  356056              5458 ns/op            3439 B/op         21 allocs/op
BenchmarkFetchAndSetScope/fetch_and_set_scope_32_keys
BenchmarkFetchAndSetScope/fetch_and_set_scope_32_keys-12                  173084              6636 ns/op            7181 B/op         39 allocs/op
BenchmarkFetchAndSetScope/fetch_and_set_scope_64_keys
BenchmarkFetchAndSetScope/fetch_and_set_scope_64_keys-12                   69446             19742 ns/op           15388 B/op         74 allocs/op
BenchmarkFetchAndSetScope/fetch_and_set_scope_128_keys
BenchmarkFetchAndSetScope/fetch_and_set_scope_128_keys-12                  48106             44673 ns/op           31211 B/op        140 allocs/op

image

after

goos: linux
goarch: amd64
pkg: github.com/ava-labs/hypersdk/tstate
cpu: Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
BenchmarkFetchAndSetScope
BenchmarkFetchAndSetScope/fetch_and_set_scope_4_keys
BenchmarkFetchAndSetScope/fetch_and_set_scope_4_keys-12                   851434              1400 ns/op             800 B/op          7 allocs/op
BenchmarkFetchAndSetScope/fetch_and_set_scope_8_keys
BenchmarkFetchAndSetScope/fetch_and_set_scope_8_keys-12                   516002              2178 ns/op            1825 B/op         12 allocs/op
BenchmarkFetchAndSetScope/fetch_and_set_scope_16_keys
BenchmarkFetchAndSetScope/fetch_and_set_scope_16_keys-12                  295729              3874 ns/op            3983 B/op         21 allocs/op
BenchmarkFetchAndSetScope/fetch_and_set_scope_32_keys
BenchmarkFetchAndSetScope/fetch_and_set_scope_32_keys-12                  160959              7256 ns/op            8235 B/op         39 allocs/op
BenchmarkFetchAndSetScope/fetch_and_set_scope_64_keys
BenchmarkFetchAndSetScope/fetch_and_set_scope_64_keys-12                   73342             14859 ns/op           17470 B/op         74 allocs/op
BenchmarkFetchAndSetScope/fetch_and_set_scope_128_keys
BenchmarkFetchAndSetScope/fetch_and_set_scope_128_keys-12                  43680             27489 ns/op           35332 B/op        140 allocs/op

image

benchvset-after.cpu.txt

[1] https://github.com/VictoriaMetrics/fastcache

hexfusion added 2 commits May 14, 2023 11:57
Signed-off-by: Sam Batschelet <[email protected]>
@hexfusion hexfusion temporarily deployed to long-ci May 15, 2023 15:17 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 15, 2023 15:17 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 15, 2023 15:17 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 15, 2023 15:17 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 15, 2023 15:17 — with GitHub Actions Inactive
@hexfusion hexfusion changed the title Hexfusion/optimized string Inline byte to string conversions May 15, 2023
@hexfusion hexfusion temporarily deployed to long-ci May 15, 2023 15:43 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 15, 2023 15:43 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 15, 2023 15:43 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 15, 2023 15:43 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 15, 2023 15:43 — with GitHub Actions Inactive
@hexfusion hexfusion force-pushed the hexfusion/optimized-string branch from fe7f852 to 3002fd4 Compare May 15, 2023 15:53
@hexfusion hexfusion temporarily deployed to long-ci May 15, 2023 15:53 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 15, 2023 15:53 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 15, 2023 15:53 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 15, 2023 15:53 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 15, 2023 15:53 — with GitHub Actions Inactive
Signed-off-by: Sam Batschelet <[email protected]>
@hexfusion hexfusion temporarily deployed to long-ci May 15, 2023 15:54 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 15, 2023 15:54 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 15, 2023 15:54 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 15, 2023 15:54 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 15, 2023 15:54 — with GitHub Actions Inactive
@hexfusion hexfusion self-assigned this May 15, 2023
Signed-off-by: Sam Batschelet <[email protected]>
@hexfusion hexfusion temporarily deployed to long-ci May 16, 2023 00:59 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 16, 2023 00:59 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 16, 2023 00:59 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 17, 2023 12:51 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 17, 2023 12:51 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 17, 2023 12:51 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 17, 2023 12:51 — with GitHub Actions Inactive
@hexfusion hexfusion marked this pull request as ready for review May 17, 2023 12:56
@hexfusion hexfusion requested a review from patrick-ogrady as a code owner May 17, 2023 12:56
ts.scopeStorage = storage
}

// checkScope returns whether [k] is in ts.readScope.
func (ts *TState) checkScope(_ context.Context, k []byte) bool {
for _, s := range ts.scope {
// TODO: benchmark and see if creating map is worth overhead
Copy link
Contributor Author

@hexfusion hexfusion May 17, 2023

Choose a reason for hiding this comment

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

this very much depends on the size of the set. since lookup is O(n) its reasonable to have a map if we might have 8+ keys. I added the change for ref but we can revert the commit if we feel the set size would remain similar to tokenvm (4).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Although the lookup is much faster for map it does increase the alloc in FetchAndSetScope by a fair amount. As this set grows into possibly hundreds the time gained on lookup will be probably overshadowed by memory bloat/cpu time.

before

goos: linux
goarch: amd64
pkg: github.com/ava-labs/hypersdk/tstate
cpu: Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
BenchmarkFetchAndSetScope/fetch_and_set_scope_4_keys_with_length_65-12         	  894080	      1408 ns/op	     800 B/op	       7 allocs/op
BenchmarkFetchAndSetScope/fetch_and_set_scope_8_keys_with_length_65-12         	  687463	      2001 ns/op	    1473 B/op	      11 allocs/op
BenchmarkFetchAndSetScope/fetch_and_set_scope_16_keys_with_length_65-12        	  372249	      3031 ns/op	    2833 B/op	      19 allocs/op
BenchmarkFetchAndSetScope/fetch_and_set_scope_32_keys_with_length_65-12        	  243076	      5262 ns/op	    5424 B/op	      35 allocs/op
BenchmarkFetchAndSetScope/fetch_and_set_scope_64_keys_with_length_65-12        	  110935	     14345 ns/op	   11416 B/op	      68 allocs/op
BenchmarkFetchAndSetScope/fetch_and_set_scope_128_keys_with_length_65-12       	   42150	     26776 ns/op	   22680 B/op	     132 allocs/op

after

goos: linux
goarch: amd64
pkg: github.com/ava-labs/hypersdk/tstate
cpu: Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
BenchmarkFetchAndSetScope/fetch_and_set_scope_4_keys_with_length_65-12         	  716866	      1802 ns/op	    1392 B/op	      14 allocs/op
BenchmarkFetchAndSetScope/fetch_and_set_scope_8_keys_with_length_65-12         	  404772	      2923 ns/op	    2529 B/op	      22 allocs/op
BenchmarkFetchAndSetScope/fetch_and_set_scope_16_keys_with_length_65-12        	  282474	      4318 ns/op	    4824 B/op	      38 allocs/op
BenchmarkFetchAndSetScope/fetch_and_set_scope_32_keys_with_length_65-12        	  157590	      7921 ns/op	    9284 B/op	      70 allocs/op
BenchmarkFetchAndSetScope/fetch_and_set_scope_64_keys_with_length_65-12        	   83341	     15155 ns/op	   19376 B/op	     136 allocs/op
BenchmarkFetchAndSetScope/fetch_and_set_scope_128_keys_with_length_65-12       	   42186	     40291 ns/op	   38448 B/op	     264 allocs/op

Copy link
Contributor Author

Choose a reason for hiding this comment

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

reverting for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can see the cost of lookup as the set increases.

BenchmarkCheckScope/set_scope_4_keys_with_length_65-12         	55952859	        21.66 ns/op	       0 B/op	       0 allocs/op
BenchmarkCheckScope/set_scope_8_keys_with_length_65-12         	30695019	        39.45 ns/op	       0 B/op	       0 allocs/op
BenchmarkCheckScope/set_scope_16_keys_with_length_65-12        	14245750	        84.93 ns/op	       0 B/op	       0 allocs/op
BenchmarkCheckScope/set_scope_32_keys_with_length_65-12        	 7016282	       160.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkCheckScope/set_scope_64_keys_with_length_65-12        	 3868280	       286.7 ns/op	       0 B/op	       0 allocs/op
BenchmarkCheckScope/set_scope_128_keys_with_length_65-12       	 2166471	       547.1 ns/op	       0 B/op	       0 allocs/op

@hexfusion hexfusion force-pushed the hexfusion/optimized-string branch from 8f89b1a to f687764 Compare May 17, 2023 16:07
@hexfusion hexfusion temporarily deployed to long-ci May 17, 2023 16:07 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 17, 2023 16:07 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 17, 2023 16:07 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 17, 2023 16:07 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 17, 2023 16:07 — with GitHub Actions Inactive
hexfusion added 2 commits May 17, 2023 12:11
Signed-off-by: Sam Batschelet <[email protected]>
Signed-off-by: Sam Batschelet <[email protected]>
@hexfusion hexfusion temporarily deployed to long-ci May 17, 2023 17:36 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 17, 2023 17:36 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 17, 2023 17:36 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 17, 2023 17:36 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 17, 2023 17:36 — with GitHub Actions Inactive
@patrick-ogrady
Copy link
Contributor

We should do the same for: https://github.com/ava-labs/hypersdk/blob/main/chain/processor.go

@patrick-ogrady patrick-ogrady changed the title Inline byte to string conversions [tstate] inline byte to string conversions May 19, 2023
@hexfusion hexfusion temporarily deployed to long-ci May 19, 2023 02:01 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 19, 2023 02:01 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 19, 2023 02:01 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 19, 2023 02:01 — with GitHub Actions Inactive
@hexfusion hexfusion temporarily deployed to long-ci May 19, 2023 02:01 — with GitHub Actions Inactive
@github-actions
Copy link

This PR has become stale because it has been open for 30 days with no activity. Adding the lifecycle/frozen label will exempt this PR from future lifecycle events..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants