Bug #60227 Much overhead for MD5() and SHA1() on short strings
Submitted: 23 Feb 2011 21:56 Modified: 11 Jun 2012 16:37
Reporter: Linhai Song Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: DDL Severity:S5 (Performance)
Version:5.1.54, 5.5.26 OS:Any
Assigned to: CPU Architecture:Any
Tags: Contribution, md5, performance, sha1

[23 Feb 2011 21:56] Linhai Song
Description:
After reading report of mysqlbug49491, I find a place which is exact the same situation as mysqlbug49491, but it is not reported in the original bug report, and it is not fixed in the latest mysql-5.1.54 version.

The place I find is:

mysql-5.1.54/sql/table.cc:3020

void  TABLE_LIST::calc_md5(char *buffer)
{
  uchar digest[16];
  MY_MD5_HASH(digest, (uchar *) select_stmt.str, select_stmt.length);
  sprintf((char *) buffer,
            "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            digest[0], digest[1], digest[2], digest[3],
            digest[4], digest[5], digest[6], digest[7],
            digest[8], digest[9], digest[10], digest[11],
            digest[12], digest[13], digest[14], digest[15]);
}

I think this place should also be patched the same way as mysqlbug49491 

BTW. mysqlbug49491 is not fixed in latest mysql-5.0.91 version. The place I find is also in the latest mysql-5.0.91 version and latest mysql-5.5.8 version. 

 

How to repeat:
code review

Suggested fix:
I put the patch for mysqlbug49491 here:

static void array_to_hex(char *to, const unsigned char *str, uint len)
{
  const unsigned char *str_end= str + len;
  for (; str != str_end; ++str)
  {
    *to++= _dig_vec_lower[((uchar) *str) >> 4];
    *to++= _dig_vec_lower[((uchar) *str) & 0x0F];
  }
}
[12 Mar 2011 20:06] Linhai Song
What does tag "contribution" mean?

I have done some unit tests, and results show that the patch will work better than calling sprintf.

But code fragment I report is only executed once when executing "ALTER VIEW" command. I think using patch may make the performance only a little better.
[26 May 2012 10:38] Valeriy Kravchuk
Still same suboptimal code is used in 5.5.26. Time to fix this.
[11 Jun 2012 16:37] Paul DuBois
Noted in 5.6.6 changelog.

The MD5() and SHA1() functions had excessive overhead for short
strings.