From f601c3cef8837b38b9f9f67b23620ea858bf3321 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lourens=20Naud=C3=A9?= Date: Sat, 7 Aug 2010 01:00:41 +0100 Subject: [PATCH] Skip additional string length access in rb_mysql_client_escape and benchmark escaping "clean" strings as well --- benchmark/escape.rb | 50 +++++++++++++++++++++++---------------------- ext/mysql2/client.c | 2 +- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/benchmark/escape.rb b/benchmark/escape.rb index d52d2bc..1b159da 100644 --- a/benchmark/escape.rb +++ b/benchmark/escape.rb @@ -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 \ No newline at end of file +run_escape_benchmarks "abc'def\"ghi\0jkl%mno" +run_escape_benchmarks "clean string" \ No newline at end of file diff --git a/ext/mysql2/client.c b/ext/mysql2/client.c index 3cbd7b2..9451554 100644 --- a/ext/mysql2/client.c +++ b/ext/mysql2/client.c @@ -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;