-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: outline logic in Decode to allow for stack allocations
I took extra efforts for this to be a backward compatible change, I think `DecodedMultihash` should return a value struct not a pointer. I also updated the error type to a value because this allows for 1 instead of 2 allocations when erroring. ``` name old time/op new time/op delta Decode-12 102ns ± 3% 18ns ± 3% -82.47% (p=0.000 n=9+9) name old alloc/op new alloc/op delta Decode-12 64.0B ± 0% 0.0B -100.00% (p=0.000 n=10+10) name old allocs/op new allocs/op delta Decode-12 1.00 ± 0% 0.00 -100.00% (p=0.000 n=10+10) ``` I originally found this problem by benchmarking `go-cid`: ``` github.com/ipfs/go-cid.CidFromBytes /home/hugo/go/pkg/mod/github.com/ipfs/[email protected]/cid.go Total: 4.64GB 10.75GB (flat, cum) 100% 638 . . if len(data) > 2 && data[0] == mh.SHA2_256 && data[1] == 32 { 639 . . if len(data) < 34 { 640 . . return 0, Undef, ErrInvalidCid{fmt.Errorf("not enough bytes for cid v0")} 641 . . } 642 . . 643 . 6.11GB h, err := mh.Cast(data[:34]) _, err := Decode(buf) multihash.go:215 644 . . if err != nil { 645 . . return 0, Undef, ErrInvalidCid{err} 646 . . } ``` We can see it call `mh.Cast` and `mh.Cast` call `Decode` and instantly drops the `DecodedMultihash`. The point of this is purely to validate the multihash by checking err.
- Loading branch information
Showing
4 changed files
with
92 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// !go1.20 | ||
package multihash | ||
|
||
import "testing" | ||
|
||
func mustNotAllocate(_ *testing.T, f func()) { | ||
// the compiler isn't able to detect our outlined stack allocation on before | ||
// 1.20 so let's not test for it. We don't mind if outdated versions are slightly slower. | ||
f() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// go1.20 | ||
package multihash | ||
|
||
import "testing" | ||
|
||
func mustNotAllocate(t *testing.T, f func()) { | ||
t.Helper() | ||
if b := testing.AllocsPerRun(10, f); b != 0 { | ||
t.Errorf("it allocated ! %f", b) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters