Skip additional string length access in rb_mysql_client_escape and benchmark escaping "clean" strings as well

This commit is contained in:
Lourens Naudé 2010-08-07 01:00:41 +01:00 committed by Brian Lopez
parent 931765ee05
commit f601c3cef8
2 changed files with 27 additions and 25 deletions

View File

@ -7,31 +7,33 @@ require 'mysql'
require 'mysql2'
require 'do_mysql'
number_of = 1000
str = "abc'def\"ghi\0jkl%mno"
def run_escape_benchmarks(str, number_of = 1000)
Benchmark.bmbm do |x|
mysql = Mysql.new("localhost", "root")
x.report do
puts "Mysql #{str.inspect}"
number_of.times do
mysql.quote str
end
end
Benchmark.bmbm do |x|
mysql = Mysql.new("localhost", "root")
x.report do
puts "Mysql"
number_of.times do
mysql.quote str
mysql2 = Mysql2::Client.new(:host => "localhost", :username => "root")
x.report do
puts "Mysql2 #{str.inspect}"
number_of.times do
mysql2.escape str
end
end
do_mysql = DataObjects::Connection.new("mysql://localhost/test")
x.report do
puts "do_mysql #{str.inspect}"
number_of.times do
do_mysql.quote_string str
end
end
end
end
mysql2 = Mysql2::Client.new(:host => "localhost", :username => "root")
x.report do
puts "Mysql2"
number_of.times do
mysql2.escape str
end
end
do_mysql = DataObjects::Connection.new("mysql://localhost/test")
x.report do
puts "do_mysql"
number_of.times do
do_mysql.quote_string str
end
end
end
run_escape_benchmarks "abc'def\"ghi\0jkl%mno"
run_escape_benchmarks "clean string"

View File

@ -335,7 +335,7 @@ static VALUE rb_mysql_client_escape(VALUE self, VALUE str) {
Data_Get_Struct(self, MYSQL, client);
REQUIRE_OPEN_DB(client);
newLen = mysql_real_escape_string(client, escaped, StringValuePtr(str), RSTRING_LEN(str));
newLen = mysql_real_escape_string(client, escaped, StringValuePtr(str), oldLen);
if (newLen == oldLen) {
// no need to return a new ruby string if nothing changed
return str;