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

Use crypto/subtle.XORBytes #294

Merged
merged 1 commit into from
May 28, 2024
Merged

Use crypto/subtle.XORBytes #294

merged 1 commit into from
May 28, 2024

Conversation

jech
Copy link
Contributor

@jech jech commented May 26, 2024

The function XORBytes has been included in the Go standard library since Go version 1.20. This allows us to remove a bunch of assembler code, and instead rely on the stdlib.

There are only three versions remaining:

  • xor_generic, which simply calls the stdlib function;
  • xor_old, which uses unsafe Go and is used with Go 1.19;
  • xor_arm, which is written in assembly and is used on 32-bit ARM.

At least on AMD64 using Go 1.22, performance is unchanged:

name                          old time/op    new time/op    delta
XORAligned/8Bytes-8             4.83ns ± 0%    5.04ns ± 1%  +4.18%  (p=0.008 n=5+5)
XORAligned/128Bytes-8           9.22ns ± 0%    9.21ns ± 0%    ~     (p=0.095 n=5+5)
XORAligned/2048Bytes-8          81.1ns ± 0%    79.4ns ± 0%  -2.12%  (p=0.016 n=4+5)
XORAligned/32768Bytes-8         1.42µs ± 1%    1.40µs ± 2%  -1.48%  (p=0.024 n=5+5)
XORUnalignedDst/8Bytes-8        5.95ns ± 2%    5.90ns ± 0%  -0.88%  (p=0.032 n=5+5)
XORUnalignedDst/128Bytes-8      11.4ns ± 0%    11.6ns ± 0%  +1.88%  (p=0.016 n=4+5)
XORUnalignedDst/2048Bytes-8     94.9ns ± 1%    94.8ns ± 0%    ~     (p=1.000 n=5+5)
XORUnalignedDst/32768Bytes-8    1.81µs ± 1%    1.81µs ± 2%    ~     (p=0.730 n=5+5)

name                          old speed      new speed      delta
XORAligned/8Bytes-8           1.66GB/s ± 0%  1.59GB/s ± 1%  -4.00%  (p=0.008 n=5+5)
XORAligned/128Bytes-8         13.9GB/s ± 0%  13.9GB/s ± 0%    ~     (p=0.095 n=5+5)
XORAligned/2048Bytes-8        25.2GB/s ± 0%  25.8GB/s ± 0%  +2.17%  (p=0.016 n=4+5)
XORAligned/32768Bytes-8       23.1GB/s ± 1%  23.4GB/s ± 2%  +1.49%  (p=0.032 n=5+5)
XORUnalignedDst/8Bytes-8      1.34GB/s ± 2%  1.36GB/s ± 0%  +0.88%  (p=0.032 n=5+5)
XORUnalignedDst/128Bytes-8    11.3GB/s ± 0%  11.1GB/s ± 0%  -1.85%  (p=0.016 n=4+5)
XORUnalignedDst/2048Bytes-8   21.6GB/s ± 1%  21.6GB/s ± 0%    ~     (p=1.000 n=5+5)
XORUnalignedDst/32768Bytes-8  18.1GB/s ± 1%  18.1GB/s ± 2%    ~     (p=0.690 n=5+5)

Of course, there is a significant regression under Go 1.19 and earlier. Note that only the aligned case is relevant for Pion (see xorBytesCTR in srtp/crypto.go).

name                          old time/op    new time/op    delta
XORAligned/8Bytes-8             4.65ns ± 0%    6.90ns ±12%   +48.54%  (p=0.016 n=4+5)
XORAligned/128Bytes-8           9.00ns ± 0%   17.64ns ± 7%   +95.88%  (p=0.008 n=5+5)
XORAligned/2048Bytes-8          81.2ns ± 0%   195.7ns ± 5%  +141.12%  (p=0.008 n=5+5)
XORAligned/32768Bytes-8         1.41µs ± 1%    3.05µs ± 4%  +115.37%  (p=0.008 n=5+5)
XORUnalignedDst/8Bytes-8        5.40ns ± 0%    7.47ns ± 2%   +38.39%  (p=0.016 n=5+4)
XORUnalignedDst/128Bytes-8      11.6ns ± 0%    19.6ns ± 4%   +69.00%  (p=0.008 n=5+5)
XORUnalignedDst/2048Bytes-8     94.8ns ± 0%   212.3ns ± 6%  +124.02%  (p=0.008 n=5+5)
XORUnalignedDst/32768Bytes-8    1.81µs ± 1%    3.29µs ±12%   +82.30%  (p=0.008 n=5+5)

name                          old speed      new speed      delta
XORAligned/8Bytes-8           1.72GB/s ± 0%  1.16GB/s ±11%   -32.44%  (p=0.016 n=4+5)
XORAligned/128Bytes-8         14.2GB/s ± 0%   7.3GB/s ± 7%   -48.83%  (p=0.008 n=5+5)
XORAligned/2048Bytes-8        25.2GB/s ± 0%  10.5GB/s ± 5%   -58.49%  (p=0.008 n=5+5)
XORAligned/32768Bytes-8       23.2GB/s ± 1%  10.8GB/s ± 5%   -53.54%  (p=0.008 n=5+5)
XORUnalignedDst/8Bytes-8      1.48GB/s ± 0%  1.07GB/s ± 2%   -27.73%  (p=0.016 n=5+4)
XORUnalignedDst/128Bytes-8    11.0GB/s ± 0%   6.5GB/s ± 4%   -40.80%  (p=0.008 n=5+5)
XORUnalignedDst/2048Bytes-8   21.6GB/s ± 0%   9.7GB/s ± 6%   -55.26%  (p=0.008 n=5+5)
XORUnalignedDst/32768Bytes-8  18.1GB/s ± 1%  10.0GB/s ±11%   -44.97%  (p=0.008 n=5+5)

However, Go 1.19. is obsolescent (it's mostly relevant for Debian Stable), so it's worth to pay that price in order to remove 350 lines of inscrutable assembler:

 utils/xor/xor_amd64.go   | 29 ----------------
 utils/xor/xor_amd64.s    | 56 -------------------------------
 utils/xor/xor_arm64.go   | 31 -----------------
 utils/xor/xor_arm64.s    | 69 --------------------------------------
 utils/xor/xor_generic.go | 72 ++++-----------------------------------
 utils/xor/xor_old.go     | 77 ++++++++++++++++++++++++++++++++++++++++++
 utils/xor/xor_ppc64x.go  | 29 ----------------
 utils/xor/xor_ppc64x.s   | 87 ------------------------------------------------
 8 files changed, 83 insertions(+), 367 deletions(-)

Copy link

codecov bot commented May 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.11%. Comparing base (fa1ca71) to head (4092685).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #294      +/-   ##
==========================================
- Coverage   83.30%   83.11%   -0.20%     
==========================================
  Files          40       39       -1     
  Lines        2761     2724      -37     
==========================================
- Hits         2300     2264      -36     
+ Misses        337      335       -2     
- Partials      124      125       +1     
Flag Coverage Δ
go 82.94% <100.00%> (-0.16%) ⬇️
wasm 65.11% <100.00%> (-0.29%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jech jech force-pushed the use-crypto-subtle branch 2 times, most recently from 6840abe to 1b5363e Compare May 26, 2024 20:31
@stv0g
Copy link
Member

stv0g commented May 26, 2024

Looks good to me :) I think we are ready to bump the minimum Go version to 1.20 by now.

Can you add a nolint-Comment to silence the linter error?

@jech
Copy link
Contributor Author

jech commented May 26, 2024

I think we are ready to bump the minimum Go version to 1.20 by now.

Please don't, Debian stable still uses 1.19.

Can you add a nolint-Comment to silence the linter error?

Done.

@jech
Copy link
Contributor Author

jech commented May 26, 2024

Just fixed a bug in the xor_old code, which wasn't optimising the AMD64 case correctly. About to push.

The function XORBytes has been included in the Go standard library
since Go version 1.20.  This allows us to remove a bunch of
assembler code, and instead rely on the stdlib.

There are only three versions remaining:

  - xor_generic, which simply calls the stdlib function;
  - xor_old, which uses unsafe Go and is used with Go 1.19;
  - xor_arm, which is written in assembly and is used on
    32-bit ARM, since the stdlib doesn't implement a fast
    version of XORBytes on that architecture.
@stv0g
Copy link
Member

stv0g commented May 28, 2024

Okay, we just a flake WASM test failing before. I've reran the pipeline. It passes now.

Feel free to merge it :)

@jech
Copy link
Contributor Author

jech commented May 28, 2024

I don't have commit rights (I'd need to setup 2FA on github, which I'm not willing to do).

@stv0g stv0g merged commit 8c1c18b into pion:master May 28, 2024
14 checks passed
@jech jech deleted the use-crypto-subtle branch May 28, 2024 17:23
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