uint256 icon indicating copy to clipboard operation
uint256 copied to clipboard

uint256: optimize Byte

Open AaronChen0 opened this issue 1 year ago • 1 comments

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

AaronChen0 avatar Apr 28 '24 07:04 AaronChen0

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

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

:exclamation: 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     

codecov[bot] avatar Apr 28 '24 07:04 codecov[bot]

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

LGTM

holiman avatar May 06 '24 06:05 holiman

Nice.

AaronChen0 avatar May 06 '24 07:05 AaronChen0