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

performance: optimising purego performance by expanding loops #75

Closed
wants to merge 1 commit into from

Conversation

aimuz
Copy link

@aimuz aimuz commented Jan 9, 2024

Improves performance by up to 18% by unfolding loops

benchstat old.out new.out                                                
goos: darwin
goarch: amd64
pkg: github.com/cespare/xxhash/v2
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
              │    old.out    │               new.out               │
              │    sec/op     │   sec/op     vs base                │
Sum64/4B-12      3.289n ±  0%   3.288n ± 0%        ~ (p=0.467 n=15)
Sum64/16B-12     5.876n ± 14%   6.066n ± 1%        ~ (p=0.480 n=15)
Sum64/100B-12    15.78n ±  1%   15.20n ± 1%   -3.68% (p=0.000 n=15)
Sum64/4KB-12     368.8n ±  2%   314.2n ± 0%  -14.80% (p=0.000 n=15)
Sum64/10MB-12   1123.0µ ±  5%   947.4µ ± 3%  -15.63% (p=0.000 n=15)
geomean          166.1n         155.3n        -6.50%

              │    old.out    │               new.out                │
              │      B/s      │     B/s       vs base                │
Sum64/4B-12     1.133Gi ±  0%   1.133Gi ± 0%        ~ (p=0.512 n=15)
Sum64/16B-12    2.536Gi ± 12%   2.457Gi ± 1%        ~ (p=0.486 n=15)
Sum64/100B-12   5.902Gi ±  1%   6.125Gi ± 1%   +3.79% (p=0.000 n=15)
Sum64/4KB-12    10.10Gi ±  2%   11.86Gi ± 0%  +17.39% (p=0.000 n=15)
Sum64/10MB-12   8.293Gi ±  5%   9.830Gi ± 3%  +18.53% (p=0.000 n=15)
geomean         4.270Gi         4.567Gi        +6.96%

Referenced RFC 1071

Signed-off-by: aimuz [email protected]

@aimuz
Copy link
Author

aimuz commented Jan 9, 2024

Maybe assembly can be optimised this way too

@cespare
Copy link
Owner

cespare commented Jan 9, 2024

Hi @aimuz, thanks for sending this over.

I ran some quick benchmarks on some bigger desktop and server CPUs and got significantly worse results than you did:

goos: linux
goarch: amd64
pkg: github.com/cespare/xxhash/v2
cpu: AMD Ryzen 9 3900X 12-Core Processor
              │   old.txt   │              new.txt               │
              │   sec/op    │   sec/op     vs base               │
Sum64/4B-24     2.760n ± 4%   2.767n ± 3%       ~ (p=0.987 n=10)
Sum64/16B-24    4.595n ± 4%   4.615n ± 2%       ~ (p=0.342 n=10)
Sum64/100B-24   13.73n ± 4%   12.65n ± 3%  -7.83% (p=0.000 n=10)
Sum64/4KB-24    295.5n ± 3%   278.7n ± 2%  -5.69% (p=0.000 n=10)
Sum64/10MB-24   723.4µ ± 3%   680.1µ ± 2%  -5.98% (p=0.000 n=10)
geomean         130.1n        125.1n       -3.82%

              │   old.txt    │               new.txt               │
              │     B/s      │     B/s       vs base               │
Sum64/4B-24     1.350Gi ± 4%   1.346Gi ± 3%       ~ (p=0.971 n=10)
Sum64/16B-24    3.242Gi ± 4%   3.229Gi ± 2%       ~ (p=0.353 n=10)
Sum64/100B-24   6.784Gi ± 4%   7.358Gi ± 3%  +8.47% (p=0.000 n=10)
Sum64/4KB-24    12.61Gi ± 3%   13.37Gi ± 2%  +6.03% (p=0.000 n=10)
Sum64/10MB-24   12.87Gi ± 3%   13.69Gi ± 2%  +6.36% (p=0.000 n=10)
geomean         5.453Gi        5.669Gi       +3.97%
goos: linux
goarch: amd64
pkg: github.com/cespare/xxhash/v2
cpu: Intel(R) Xeon(R) Platinum 8488C
             │ c7i_old_purego.txt │         c7i_new_purego.txt         │
             │       sec/op       │   sec/op     vs base               │
Sum64/4B-4            2.440n ± 2%   2.445n ± 2%       ~ (p=0.868 n=10)
Sum64/16B-4           4.721n ± 2%   4.713n ± 4%       ~ (p=0.631 n=10)
Sum64/100B-4          12.46n ± 1%   12.25n ± 2%  -1.69% (p=0.005 n=10)
Sum64/4KB-4           305.4n ± 3%   295.6n ± 2%  -3.21% (p=0.004 n=10)
Sum64/10MB-4          725.1µ ± 2%   729.9µ ± 4%       ~ (p=0.529 n=10)
geomean               126.0n        124.9n       -0.85%

             │ c7i_old_purego.txt │         c7i_new_purego.txt          │
             │        B/s         │     B/s       vs base               │
Sum64/4B-4           1.527Gi ± 2%   1.524Gi ± 2%       ~ (p=0.912 n=10)
Sum64/16B-4          3.156Gi ± 2%   3.162Gi ± 4%       ~ (p=0.631 n=10)
Sum64/100B-4         7.479Gi ± 1%   7.605Gi ± 2%  +1.68% (p=0.007 n=10)
Sum64/4KB-4          12.20Gi ± 3%   12.60Gi ± 2%  +3.32% (p=0.004 n=10)
Sum64/10MB-4         12.85Gi ± 2%   12.76Gi ± 4%       ~ (p=0.529 n=10)
geomean              5.628Gi        5.676Gi       +0.85%

I think that makes sense; manual loop unrolling gets less and less useful the more sophisticated the CPU.

To fully evaluate this change I'd want to test on more CPUs, look at the generated assembly, and also evaluate the code size impact. But overall I'm not too excited about this change.

@cespare
Copy link
Owner

cespare commented Apr 4, 2024

I'm going to close this PR for now. If anyone wants to pick this up (or make similar changes), it would help to do the kind of analyses I suggested in the previous post.

@cespare cespare closed this Apr 4, 2024
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