This piece of code can be optimized by a new condition, using length:
if (new_db_name->length == INFORMATION_SCHEMA_NAME.length &&
my_strcasecmp(system_charset_info, new_db_name->str,
INFORMATION_SCHEMA_NAME.str) == 0)
so in most cases my_strcasecmp() will even not be executed.
Quick search using "grep my_stecasecmp | grep INFORMATION_SCHEMA"
discovers three pieces where this optimization can be done:
mysql_priv.h:
#define is_schema_db(X) \
!my_strcasecmp(system_charset_info, INFORMATION_SCHEMA_NAME.str, (X))
sql_db.cc: if (!my_strcasecmp(system_charset_info, db, INFORMATION_SCHEMA_NAME.str))
sql_show.cc: if (!my_strcasecmp(system_charset_info, INFORMATION_SCHEMA_NAME.str,

http://lists.mysql.com/commits/94104 is Ok to push.
But I'd also perhaps add an overloaded function:
inline bool is_schema_db(const LEX_STR *str);
because it is exactly LEX_STR which is tested in most of the cases.

[15 Dec 2009 11:34]
Alexander Barkov

or even:
inline bool is_schema_db(const LEX_STR &);

[15 Dec 2009 15:50]
Mark Callaghan

Can we continue the discussion on this topic?
"!my_strcasecmp" can be found throughout the code. Will all of those calls be faster if there is a check for string lengths prior to doing the comparison? Alas, some people might use 'my_strcasecmp(...) == 0' making the search harder. I wish that style was not used.
A similar optimization opportunity exists in sql_string.cc in the stringcmp function. This is used in several places where the caller checks for equality not for order. If a stringeq method where added it could optimize for this check by first checking string length.
int stringcmp(const String *s,const String *t)
{
uint32 s_len=s->length(),t_len=t->length(),len=min(s_len,t_len);
int cmp= memcmp(s->ptr(), t->ptr(), len);
return (cmp) ? cmp : (int) (s_len - t_len);
}
callers to stringcmp that really want equality checks, from cscope for 5.0.84
item.cc eq 874 return !stringcmp(&str_value, &item->str_value);
item.cc eq 3117 return !stringcmp(&str_value, &item->str_value);
item.cc eq 5045 return !stringcmp(&str_value, &arg->str_value);
item.cc field_is_equal_to_item
6486 return !stringcmp(&field_tmp,item_result);

[19 Jan 2010 8:13]
Alexander Barkov

Hi Mark,
thank you very much for your suggestion about the further improvements way.
We cannot do all these improvements in MySQL-5.1 under the terms of this bug report. So we created a new WorkLog task:
WL#5230 Replace strcasecmp() used for checking equality to a more efficient function
It should appear soon under this location:
http://forge.mysql.com/worklog/task.php?id=5230
Thanks!

Did you confirm that this change improves performance? The change made here is much more substantial than the change suggested by Domas and may result in many more calls to strlen. The change suggested by Domas required no additional calls to strlen.
The problem reported by Domas requires changes to mysql_change_db() and add_table_to_list(). In both cases, the length of both strings is known in the check for equality with "information_schema".

The patch http://lists.mysql.com/commits/97684 removing
newly introduced strlen() calls looks ok.
So now as a result we have two overloaded functions:
is_schema_db(const char *name);
(for the cases when length is not know)
and
is_schema_db(const char *name, size_t length)
(for the case when length is known).
The second version is about 3 times faster.
In the future we will do refactoring (as Sinisa suggests):
"WL#2894 change strlen() to LEX_STRING or String"
to make length always available and thus get rid
of the slower version.

[22 Jan 2010 10:03]
Alexander Barkov

Mark,
> Did you confirm that this change improves performance?
Yes, according to our tests:
is_schema_db("twiki", 5);
is 3 times faster than
is_schema_db("twiki");
Thank you very much for helping us!

Noted in 5.1.44, 5.5.2, 6.0.14 changelogs.
The method for comparing INFORMATION_SCHEMA names and database names
was nonoptimal and an improvement was made: When the database name
length is already known, a length check is made first and content
comparison skipped if the lengths are unequal.
Setting report to Need Merge pending push to Celosia.