Repository navigation
Veracode: Use internal alignment logarithm in free_aligned_sized - #3053
Conversation
Before/after assembly and benchmark resultsAdditional code-generation and runtime validationCompared the final linked libraries before and after this change using Selected alignment-decoding instructions are shown below. Labels are GCC, x86-64Before, the low-word path uses a 32-bit scan, with a separate path for cmpq $0x7ffffffe, %rsi
ja .Lhigh_word
bsfl %esi, %eax
movl $-1, %edx
cmovnel %eax, %edxAfter, the compiler can handle alignment movq %rsi, %rax
# ... fast-path selection omitted ...
bsrq %rax, %rdxClang ThinLTO, x86-64Before, the low-word path is: cmpq $0x7ffffffe, %rsi
ja .Lhigh_word
testq %rsi, %rsi
je .Lzero
tzcntl %esi, %eax
testl %eax, %eax
jne .LdefaultAfter: bsrq %rsi, %rax
xorq $0x3f, %rax
cmpq $0x3f, %rax
jne .Lconvert_flags
# ... unchanged fast-path work omitted ...
.Lconvert_flags:
xorl $0x3f, %eaxOn AArch64, the split-word MeasurementsThe benchmark preallocates objects using Values below are before → after medians:
The GCC and AArch64 measurements use seven alternating before/after The change produces smaller code and fewer retired instructions in these Native debug/profiling and ThinLTO tests passed on both architectures. |
| TEST_BEGIN(test_mallocx_alignment_flags) { | ||
| for (unsigned lg_align = 0; lg_align < sizeof(size_t) * 8; lg_align++) { | ||
| size_t alignment = ZU(1) << lg_align; | ||
| expect_u_eq(lg_floor(alignment), lg_align, |
There was a problem hiding this comment.
This test aims at proving the old flag calculation is the same as the new one? That is useful for validating the change, but it couples the suite to both implementations without really exercising free_aligned_sized. If in the future we further change how the flag is processed, it will not automatically reflect here anyway.
I would suggest expand test/integration/free_size.c where free_aligned_sized is really called and exercise several alignment/size combinations there.
For valid nonzero power-of-two alignments, the existing lg_floor helper produces the same flags as MALLOCX_ALIGN. Use it at this internal call site to avoid expanding the public macro arithmetic reported in issue jemalloc#3050.
a3d299b to
261e92b
Compare
For valid nonzero power-of-two alignments, the existing lg_floor helper produces the same flags as MALLOCX_ALIGN. Use it at this internal call site to avoid expanding the public macro arithmetic reported in issue #3050.
Similar to before I cannot test this as I do not have veracode license. It is speculative based on screenshot. As an added benefit, the tested Clang 21 ThinLTO builds generate fewer instructions for this path on both x86-64 and AArch64.