Details
-
Bug
-
Status: Closed (View Workflow)
-
Major
-
Resolution: Fixed
-
10.6, 10.11, 11.4, 11.8, 12.3
-
GCC, MSVC
-
Related to performance
-
improved performance of innodb conversion operations between big/little endian
Description
As I noted in MDEV-38989, the most portable and efficient way to assign potentially unaligned data is to invoke memcpy(3). Here is an example:
#include <stddef.h>
|
#include <stdint.h>
|
#include <string.h>
|
typedef size_t ulint; |
typedef unsigned char byte; |
|
|
void
|
mach_write_to_4(
|
/*============*/
|
byte* b, /*!< in: pointer to four bytes where to store */ |
ulint n) /*!< in: ulint integer to be stored */ |
{
|
b[0] = (byte)(n >> 24);
|
b[1] = (byte)(n >> 16);
|
b[2] = (byte)(n >> 8);
|
b[3] = (byte) n;
|
}
|
|
|
void mach_write_to_4b(void *b, uint32_t n) |
{
|
#ifdef WORDS_BIGENDIAN
|
#elif defined __GNUC__
|
n = __builtin_bswap32(n);
|
#elif defined _MSC_VER
|
n = _byteswap_ulong(n);
|
#else
|
# error
|
#endif
|
memcpy(b, &n, 4); |
}
|
The first function is what we currently have. On https://godbolt.org/z/bvPda9vrj the second variant would generate the best code for x86-64 or x86-64-v3. Example from GCC 15.2:
mach_write_to_4:
|
bswap esi
|
mov DWORD PTR [rdi], esi
|
ret
|
mach_write_to_4b:
|
movbe DWORD PTR [rdi], esi
|
ret
|
Interestingly, the versions of clang that I tested generated the same code (using movbe) for both functions.
Example from MSVC:
b$ = 8
|
n$ = 16
|
mach_write_to_4 PROC ; COMDAT
|
mov rax, rdx
|
mov BYTE PTR [rcx+3], dl
|
shr rax, 24
|
mov BYTE PTR [rcx], al
|
mov rax, rdx
|
shr rax, 16
|
mov BYTE PTR [rcx+1], al
|
mov rax, rdx
|
shr rax, 8
|
mov BYTE PTR [rcx+2], al
|
ret 0
|
mach_write_to_4 ENDP
|
|
|
b$ = 8
|
n$ = 16
|
mach_write_to_4b PROC ; COMDAT
|
bswap edx
|
mov DWORD PTR [rcx], edx
|
ret 0
|
mach_write_to_4b ENDP
|
There are similar issues for most functions that are defined in storage/innobase/include/mach0data.inl.
Attachments
Issue Links
- is blocked by
-
MDEV-37788 fix uintNkorr in byte_order_generic.h, avoid unaligned access
-
- Closed
-
- relates to
-
MDEV-38989 main.ctype_utf16le SEGV in Ubuntu 26.04 (amd64v3)
-
- Closed
-