Skip to content

Conversation

@KimMachineGun
Copy link

@KimMachineGun KimMachineGun commented Aug 8, 2023

For now, *String methods are just syntactic sugars for []byte(str) conversion; thus, they make unnecessary allocations and copies. This change optimizes string operations not to make any allocations and (data) copies.

Below is benchstat result: (it would make bigger improvements on the longer key)

goos: darwin
goarch: arm64
pkg: github.com/bits-and-blooms/bloom/v3
                            │   old.txt    │              new.txt               │
                            │    sec/op    │   sec/op     vs base               │
SeparateTestAndAddString-10   314.1n ±  1%   294.8n ± 3%  -6.18% (p=0.001 n=10)
CombinedTestAndAddString-10   305.5n ± 11%   281.8n ± 8%  -7.76% (p=0.004 n=10)
geomean                       309.8n         288.2n       -6.97%

                            │  old.txt   │                new.txt                │
                            │    B/op    │   B/op    vs base                     │
SeparateTestAndAddString-10   224.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=10)
CombinedTestAndAddString-10   112.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=10)
geomean                       158.4                  ?                       ¹ ²
¹ summaries must be >0 to compute geomean
² ratios must be >0 to compute geomean

                            │  old.txt   │                 new.txt                 │
                            │ allocs/op  │ allocs/op   vs base                     │
SeparateTestAndAddString-10   2.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=10)
CombinedTestAndAddString-10   1.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=10)
geomean                       1.414                    ?                       ¹ ²
¹ summaries must be >0 to compute geomean
² ratios must be >0 to compute geomean

FYI. I didn't use unsafe.Slice (introduced in Go 1.17) in this change to support older versions.

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.

1 participant