Permalink
Browse files

speed up parameter substitution

store the offsets of the bind values, then only index to bind value
locations before joining the array
  • Loading branch information...
tenderlove committed Apr 9, 2014
1 parent b1ba768 commit 6388b4b15383c93c647c4b3732df2cb8940444bf
Showing with 12 additions and 2 deletions.
  1. +12 −2 activerecord/lib/active_record/statement_cache.rb
@@ -27,8 +27,18 @@ def sql_for(binds, connection)
end
class PartialQuery < Query
+ def initialize values
+ @values = values
+ @indexes = values.each_with_index.find_all { |thing,i|
+ Arel::Nodes::BindParam === thing
+ }.map(&:last)
+ end
+
def sql_for(binds, connection)
- @sql.compile binds, connection
+ val = @values.dup
+ binds = binds.dup
+ @indexes.each { |i| val[i] = connection.quote(*binds.shift.reverse) }
+ val.join
end
end
@@ -37,7 +47,7 @@ def self.query(visitor, ast)
end
def self.partial_query(visitor, ast, collector)
- collected = visitor.accept(ast, collector)
+ collected = visitor.accept(ast, collector).value
PartialQuery.new collected
end

0 comments on commit 6388b4b

Please sign in to comment.