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 'mysql2'
require 'do_mysql' require 'do_mysql'
number_of = 1000 def run_escape_benchmarks(str, number_of = 1000)
str = "abc'def\"ghi\0jkl%mno" Benchmark.bmbm do |x|
mysql = Mysql.new("localhost", "root")
Benchmark.bmbm do |x| x.report do
mysql = Mysql.new("localhost", "root") puts "Mysql #{str.inspect}"
x.report do number_of.times do
puts "Mysql" mysql.quote str
number_of.times do end
mysql.quote str
end end
end
mysql2 = Mysql2::Client.new(:host => "localhost", :username => "root") mysql2 = Mysql2::Client.new(:host => "localhost", :username => "root")
x.report do x.report do
puts "Mysql2" puts "Mysql2 #{str.inspect}"
number_of.times do number_of.times do
mysql2.escape str mysql2.escape str
end
end end
end
do_mysql = DataObjects::Connection.new("mysql://localhost/test") do_mysql = DataObjects::Connection.new("mysql://localhost/test")
x.report do x.report do
puts "do_mysql" puts "do_mysql #{str.inspect}"
number_of.times do number_of.times do
do_mysql.quote_string str do_mysql.quote_string str
end
end end
end 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); Data_Get_Struct(self, MYSQL, client);
REQUIRE_OPEN_DB(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) { if (newLen == oldLen) {
// no need to return a new ruby string if nothing changed // no need to return a new ruby string if nothing changed
return str; return str;