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

uint256: optimize Byte #158

Merged
merged 4 commits into from
May 6, 2024
Merged

uint256: optimize Byte #158

merged 4 commits into from
May 6, 2024

Conversation

AaronChen0
Copy link
Contributor

The main difference is

z[0] = (number & (0xff00000000000000 >> offset)) >> (56 - offset)

vs

z[0] = (number >> (56 - offset)) & 0xff

go test ./...

returns

ok  	github.com/holiman/uint256	0.972s

Benchmark

code:

func BenchmarkByte(bench *testing.B) {
	a := new(Int).SetBytes(hex2Bytes("f123456789abcdeffedcba9876543210f2f3f4f5f6f7f8f9fff3f4f5f6f7f8f9"))
	n := NewInt(16)

	result := new(Int)
	bench.ResetTimer()
	for i := 0; i < bench.N; i++ {
		result.Set(a)
		result.Byte(n)
	}
}
goos: linux
goarch: amd64
pkg: github.com/holiman/uint256
cpu: AMD Ryzen 7 7735H with Radeon Graphics         
        │     old     │                new                 │
        │   sec/op    │   sec/op     vs base               │
Byte-16   2.849n ± 2%   2.571n ± 2%  -9.77% (p=0.000 n=10)

        │    old     │              new               │
        │    B/op    │    B/op     vs base            │
Byte-16   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal

        │    old     │              new               │
        │ allocs/op  │ allocs/op   vs base            │
Byte-16   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal

Copy link

codecov bot commented Apr 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (e290e7a) to head (e2c1559).
Report is 2 commits behind head on master.

❗ Current head e2c1559 differs from pull request most recent head 0a354ff. Consider uploading reports for the commit 0a354ff to get more accurate results

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #158   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            5         5           
  Lines         1649      1648    -1     
=========================================
- Hits          1649      1648    -1     

@holiman
Copy link
Owner

holiman commented May 6, 2024

name    old time/op    new time/op    delta
Byte-8     298ns ±42%     253ns ±29%  -15.07%  (p=0.049 n=10+10)

LGTM

@AaronChen0
Copy link
Contributor Author

Nice.

Copy link
Owner

@holiman holiman left a comment

Choose a reason for hiding this comment

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

Nice work!

@holiman holiman merged commit 143cca3 into holiman:master May 6, 2024
5 checks passed
@AaronChen0 AaronChen0 deleted the byte branch May 6, 2024 07:42
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