Repository navigation
Conversation
|
Does it imply compile-time detection of AVX support? |
|
To be able to accept this code we need CLA(s) signed by all the authors of the code. Is it just you @Nick-Nuon or are there multiple? |
Yes, it looks like so. This needs to be able to fall back to non-AVX2 code if the instructions are not present. |
I would also include @lemire as large parts of the PR are adapted from his work/simdutf. Though only I appear in the commit history, I don't think this PR would have been possible without his feedback and encouragement (or at least it would have taken much longer). |
Gotcha, going out for a jog, but will take a look at it this morning. |
|
@Nick-Nuon I love getting credit, but no, I did not write a single line of code in this PR and I have no right to any of it. |
@beldmit @t8m Thanks for taking the time to review. I might be reading too much into your comments , but after looking around a bit ,I added a check against OPENSSL_ia32cap_P just in case. |
You interpreted their comments correctly. In case something goes wrong on a particular platform, the platform can then OpenSSL does not seem to use intrinsics anywhere else that I know of, are there likely to be any problems with alignment of in/out parameters? |
|
Thank you for your efforts! Do I correctly understand, that now the code will compile when the compiler support AVX instructions, and there is no need to provide any explicit flags? |
t8m
left a comment
There was a problem hiding this comment.
As this removes lots of special handling in the Base64 BIO code, I'd like to ask @vdukhovni for a review as he was changing that code not so long ago.
AFAIK , not that I know of. I used _mm_loadu_si128 and _mm256_storeu_si256 intrinsics. There might be a small performance hit (that is IMO offset by development time and complexity), but if there are issues beyond this , they lie outside my current field of knowledge. |
You're welcome! As of now, technically it will compile but for the AVX2 path to be activated, locally , I have to pass -mavx2 as compiler flag to CMake. I'll see what I can do about it. |
| @@ -0,0 +1,634 @@ | |||
| #include <string.h> | |||
|
|
|||
| #ifdef __AVX2__ | |||
There was a problem hiding this comment.
@Nick-Nuon The __AVX2__ flag is set at compile time (not runtime). Often, openssl won't be build specifically with assumed AVX2 support at compile time. So __AVX2__ will just not be defined and this code won't built.
This may change in the future. Some distributions are planning x86_64_v3 ports such as Arch although it is slow going.
There was a problem hiding this comment.
FYI, CachyOS already supported x86-64-v3 and x86-64-v4.
There was a problem hiding this comment.
Thank you. Yes, so in that case, then some users would get openssl compiled for recent processors. But that's not very typical.
There was a problem hiding this comment.
For me, it's a chicken or egg question, as x86-64-v3 distributions are not widely available, and people generally choose to consider only runtime dispatch rather than compile-time dispatch (or both), which in turn leads to the performance benefits of x86-64-v3 not being perceived as significant enough to be worthwhile.
Since this PR seems to be OpenSSL's first attempt to use intrinsics for performance optimization, it would be nice to consider both runtime and compile-time dispatch, especially in conjunction with LTO and x86-64-v3, which may provide more inlining opportunities to maximize performance.
There was a problem hiding this comment.
I think that if the OpenSSL community says 'no need for runtime dispatching, let us rely on compile-time' then @Nick-Nuon would be happy to simplify the code.
Alternatively, it is possible to modify the existing code so that the runtime dispatching is disabled when __AVX2__ is defined.
Since this PR seems to be OpenSSL's first attempt to use intrinsics for performance optimization
This is an important remark. Nick's PR should be viewed as a step forward in this direction. Once this PR is merged, it can serve as a model for future similar PRs.
There was a problem hiding this comment.
OpenSSL's assembly typically does run time dispatching.
There was a problem hiding this comment.
Alternatively, it is possible to modify the existing code so that the runtime dispatching is disabled when
__AVX2__is defined.
That's exactly what I meant, support runtime dispatching but eliminate it when feasible to promote optimization.
There was a problem hiding this comment.
OpenSSL's assembly typically does run time dispatching.
Yes, it's an OpenSSL tradition, but uninlineable asm needs to follow the calling convention anyway, in which case the runtime dispatching overhead is probably nothing in comparison. But intrinsics open up new possibilities, so I hope it's perfect from the start so that it can be a reference for future PR-like best practices.
| j = evp_encodeblock_int(ctx, out, in, inl - (inl % ctx->length), | ||
| &wrap_cnt); | ||
| } else { | ||
| #if defined(HAS_IA32CAP) && defined(__AVX2__) |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
| return evp_encodeblock_int(NULL, t, f, dlen); | ||
| int wrap_cnt = 0; | ||
|
|
||
| #if defined(HAS_IA32CAP) && defined(__AVX2__) |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
| @@ -0,0 +1,634 @@ | |||
| #include <string.h> | |||
|
|
|||
| #ifdef __AVX2__ | |||
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
They are gone now. Working on it, still taking care of the clang format/code style check. |
|
Coding style test should be good to go! |
|
This pull request is ready to merge |
Reviewed-by: Dmitry Belyavskiy <[email protected]> Reviewed-by: Paul Dale <[email protected]> (Merged from #29178)
|
Squashed and merged. Thanks for persevering and getting this worthwhile improvement in. |
You welcome, it was a pleasure working on it. Merry Christmas everyone. |
Recently, PR openssl#29178 introduced some refactoring that sped up our base64 encoding, but in so doing dropped the ability for our base64 BIO's to not include newlines on write (the BIO_FLAG_BASE64_NO_NL flag). Fix it by checking the BIO flags whenever the base64 bio starts a new encoder, and, if set, sets the corresponding EVP_ENCODE_CTX_NO_NEWLINES flags in the underlying encoder. Fixes openssl#29618
This is a suggested improvement to the base64 encoding kernel of OpenSSL following issue #11460 raised by @byronhe (Thanks to @nhorman for keeping the issue open). The objective is to improve base64 encoding speed on x64 systems with AVX support. Our approach is based on a similar function part of the simdutf C++ library which is widely deployed (e.g., in main browsers).
The following benchmarks were made using gcc, on Arch Linux with a Ryzen 5600 CPU. The benchmarking tools as well as the results are made available here:
https://github.com/Nick-Nuon/OpenSSL_B64_benchmarks.
API functions EVP_EncodeUpdate and EVP_EncodeFinal
Our first improvement affects these two API functions.
We benchmarked with three datasets:
-A subset of the Enron emails dataset
-A number of small images
-The .mobi version of 'Pride and Prejudice' (24.2 mb file)
Standard Alphabet
Standard Alphabet / PEM mode
SRP Alphabet
SRP Alphabet/ PEM mode
CLI
There are also improvements with respect to the CLI. Benchmarking against the "big" file we get:
Original:
Ours:
Taken from:
https://github.com/Nick-Nuon/OpenSSL_B64_benchmarks/tree/main/CLI_hyperfine_results
https://github.com/Nick-Nuon/OpenSSL_B64_benchmarks/tree/main/CLI_hyperfine_control
In order to test the effectiveness of this PR for CLI input of small sizes , we use a synthetic dataset of randomly generated files, increasing by intervals of 10000 bytes, we then use a linear regression to plot the performance of the old kernel vs our improved one.
Taken from:
https://github.com/Nick-Nuon/OpenSSL_B64_benchmarks/tree/main/CLI_smallsize_control
https://github.com/Nick-Nuon/OpenSSL_B64_benchmarks/tree/main/CLI_smallsize_results
Newline insertions at custom intervals
Originally, the OpenSSL base64 encoding kernel includes a buffer of ctx->length. It is possible by changing the ctx->length value to change the interval at which newlines are inserted.
When said value is a multiple of three, this translates to newline insertion at multiple of fours. Separate SIMD optimized branches were created to deals with different scenarios:
For the sake of brevity, we only post the graphs for the first case (which is overall also the best performing) to give a rough idea:
We briefly summarize the rest, as we deem them to be rare.
Amongst insertion at multiples of fours, the case where the insertion is done at 12 byte intervals is the worse at around 2x improvement over baselines, with the rest varying between the two extremes.
The cases where ctx->lengths isn't a multiple of threes corresponds to splitting the input into constant intervals(up to 80 bytes) and then calling the encode function on it.
These are not optimized and are about 30% slower than baseline in the worse cases.
For more details, we invite interested readers to delve deeper into the logs , or better yet, replicate them on their own hardware as they are small enough to inspect with the good old Mark I eyeball.
https://github.com/Nick-Nuon/OpenSSL_B64_benchmarks/tree/main/API_results
https://github.com/Nick-Nuon/OpenSSL_B64_benchmarks/tree/main/API_control
https://github.com/Nick-Nuon/OpenSSL_B64_benchmarks/tree/main/custom_interval_results
All tests pass locally with default build options. As I couldn't find a test that dealt specifically with custom newline insertions, I took the liberty to include one as to ensure a drop-in replacement.