Bug #64304 assemble to implement procedure of transforming between big-end and little-end
Submitted: 12 Feb 2012 17:04 Modified: 12 Feb 2012 19:57
Reporter: xiaobin lin (OCA) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: InnoDB storage engine Severity:S5 (Performance)
Version:5.5 OS:Any
Assigned to: CPU Architecture:Any
Tags: assemble, big-end, Contribution, little-end

[12 Feb 2012 17:04] xiaobin lin
Description:
  I am looking into the a situation that when MySQL get lots of int type data from InnoDB.

 I find that the code of translating from little-endian to big-endian cost CPU.

in row_sel_field_store_in_mysql_format
                  for (;;) {
                        ptr--;
                        *ptr = *data;
                        if (ptr == dest) {
                                break;
                        }
                        data++;
                }

  I try to modify it replace it by assembler, using bswap. 

  In my test case, the CPU cost percent of the fuction "row_sel_store_mysql_rec" decrease from 50% to 44%, results come from oprofile.

How to repeat:
read the code and performance test

Suggested fix:
If the length is 4 or 8, which the most frequent cases that transforming int or and, "bswap" and "bswapq" can be used. 

The patch file is attached. It does not consider platform-across issue, in the environment that bswapq is not supported, calling bswap twice can be used to instead.
[12 Feb 2012 17:06] xiaobin lin
It is based on 5.5.22. bzr  revno: 3694

Attachment: swap_int_assemble.patch (application/octet-stream, text), 2.51 KiB.

[12 Feb 2012 19:57] Valeriy Kravchuk
Thank you for the patch contributed.
[14 Feb 2012 14:17] Marko Mäkelä
In GCC, there are built-in functions for these. On Windows x86 and x86-64, we might have to use asm, but the GCC builtins should work on all target platforms according to the documentation.

int32_t __builtin_bswap32 (int32_t x)
int64_t __builtin_bswap64 (int64_t x)

Note that these are only to be used on little-endian platforms. On big-endian platforms, any byte order conversion must be skipped.