Permalink
Browse files

Increase virtual row performance by using a shared VirtualRow instance

I've thought about doing this for a while, but never got around to
it.  Basically, Sequel::VirtualRow instances are purely used for
DSL purposes, they are stateless.  Instead of creating new
instances everytime you want to evaluate a virtual row, use a
shared instance.  This improves performance up to 100% in the best
case, and about 30% in the typical case for something like {a > 1}.
  • Loading branch information...
1 parent 1f65d16 commit 0859d65a8c2807c1d097e2c9363e4a2d09088ba8 @jeremyevans committed Jan 9, 2013
Showing with 9 additions and 5 deletions.
  1. +2 −0 CHANGELOG
  2. +3 −3 lib/sequel/core.rb
  3. +4 −2 lib/sequel/sql.rb
View
@@ -1,5 +1,7 @@
=== HEAD
+* Increase virtual row performance by using a shared VirtualRow instance (jeremyevans)
+
* Allow the :dataset association option to accept the association reflection as an argument (jeremyevans)
* Improve association method performance by caching intermediate dataset (jeremyevans)
View
@@ -380,14 +380,14 @@ def self.tsk_require(*args)
end
# If the supplied block takes a single argument,
- # yield a new <tt>SQL::VirtualRow</tt> instance to the block
- # argument. Otherwise, evaluate the block in the context of a new
+ # yield an <tt>SQL::VirtualRow</tt> instance to the block
+ # argument. Otherwise, evaluate the block in the context of a
# <tt>SQL::VirtualRow</tt> instance.
#
# Sequel.virtual_row{a} # Sequel::SQL::Identifier.new(:a)
# Sequel.virtual_row{|o| o.a{}} # Sequel::SQL::Function.new(:a)
def self.virtual_row(&block)
- vr = SQL::VirtualRow.new
+ vr = VIRTUAL_ROW
case block.arity
when -1, 0
vr.instance_exec(&block)
View
@@ -1497,8 +1497,8 @@ class ValueList < ::Array
#
# An instance of this class is yielded to the block supplied to <tt>Dataset#filter</tt>, <tt>Dataset#order</tt>, and <tt>Dataset#select</tt>
# (and the other methods that accept a block and pass it to one of those methods).
- # If the block doesn't take an argument, the block is instance_evaled in the context of
- # a new instance of this class.
+ # If the block doesn't take an argument, the block is instance_execed in the context of
+ # an instance of this class.
#
# +VirtualRow+ uses +method_missing+ to return either an +Identifier+, +QualifiedIdentifier+, +Function+, or +WindowFunction+,
# depending on how it is called.
@@ -1621,6 +1621,8 @@ def method_missing(m, *args, &block)
Function.new(m, *args)
end
end
+
+ Sequel::VIRTUAL_ROW = new
end
# A +Window+ is part of a window function specifying the window over which the function operates.

0 comments on commit 0859d65

Please sign in to comment.