Skip to content

Commit 248b6a2

Browse files
l0rinchodlinator
andcommitted
optimization: peel align-head and unroll body to 64 bytes
Benchmarks indicated that obfuscating multiple bytes already gives an order of magnitude speed-up, but: * GCC still emitted scalar code; * Clang’s auto-vectorized loop ran on the slow unaligned-load path. Fix contains: * peeling the misaligned head enabled the hot loop starting at an 8-byte address; * `std::assume_aligned<8>` tells the optimizer the promise holds - required to keep Apple Clang happy; * manually unrolling the body to 64 bytes enabled GCC to auto-vectorize. Note that `target.size() > KEY_SIZE` condition is just an optimization, the aligned and unaligned loops work without it as well - it's why the alignment calculation still contains `std::min`. > C++ compiler .......................... GNU 14.2.0 | ns/byte | byte/s | err% | ins/byte | cyc/byte | IPC | bra/byte | miss% | total | benchmark |--------------------:|--------------------:|--------:|----------------:|----------------:|-------:|---------------:|--------:|----------:|:---------- | 0.03 | 32,464,658,919.11 | 0.0% | 0.50 | 0.11 | 4.474 | 0.08 | 0.0% | 5.29 | `ObfuscationBench` > C++ compiler .......................... Clang 20.1.7 | ns/byte | byte/s | err% | ins/byte | cyc/byte | IPC | bra/byte | miss% | total | benchmark |--------------------:|--------------------:|--------:|----------------:|----------------:|-------:|---------------:|--------:|----------:|:---------- | 0.02 | 41,231,547,045.17 | 0.0% | 0.30 | 0.09 | 3.463 | 0.02 | 0.0% | 5.47 | `ObfuscationBench` Co-authored-by: Hodlinator <[email protected]>
1 parent e7114fc commit 248b6a2

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/util/obfuscation.h

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <bit>
1515
#include <climits>
1616
#include <ios>
17+
#include <memory>
1718

1819
class Obfuscation
1920
{
@@ -33,9 +34,26 @@ class Obfuscation
3334
{
3435
if (!*this) return;
3536

36-
const KeyType rot_key{m_rotations[key_offset % KEY_SIZE]}; // Continue obfuscation from where we left off
37-
for (; target.size() >= KEY_SIZE; target = target.subspan(KEY_SIZE)) {
38-
XorWord(target.first<KEY_SIZE>(), rot_key);
37+
KeyType rot_key{m_rotations[key_offset % KEY_SIZE]}; // Continue obfuscation from where we left off
38+
if (target.size() > KEY_SIZE) {
39+
// Obfuscate until 64-bit alignment boundary
40+
if (const auto misalign{std::bit_cast<uintptr_t>(target.data()) % KEY_SIZE}) {
41+
const size_t alignment{std::min(KEY_SIZE - misalign, target.size())};
42+
XorWord(target.first(alignment), rot_key);
43+
44+
target = {std::assume_aligned<KEY_SIZE>(target.data() + alignment), target.size() - alignment};
45+
rot_key = m_rotations[(key_offset + alignment) % KEY_SIZE];
46+
}
47+
// Aligned obfuscation in 64-byte chunks
48+
for (constexpr auto unroll{8}; target.size() >= KEY_SIZE * unroll; target = target.subspan(KEY_SIZE * unroll)) {
49+
for (size_t i{0}; i < unroll; ++i) {
50+
XorWord(target.subspan(i * KEY_SIZE, KEY_SIZE), rot_key);
51+
}
52+
}
53+
// Aligned obfuscation in 64-bit chunks
54+
for (; target.size() >= KEY_SIZE; target = target.subspan(KEY_SIZE)) {
55+
XorWord(target.first<KEY_SIZE>(), rot_key);
56+
}
3957
}
4058
XorWord(target, rot_key);
4159
}

0 commit comments

Comments
 (0)