Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-39027

suboptimal code for InnoDB big endian access functions (MSVC, GCC)

    XMLWordPrintable

Details

    • 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

          Activity

            People

              wlad Vladislav Vaintroub
              marko Marko Mäkelä
              Vladislav Vaintroub Vladislav Vaintroub
              Marko Mäkelä Marko Mäkelä
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.