Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

change module storage from ivars to constants

this provides a modest speedup for processing view functions and
accessing design documents. See test/benches/ivar_vs_constant.rb for
more information.
  • Loading branch information...
commit 1b3f24f78fd55301035260fd6c17cd5fe8db0b28 1 parent fcdd045
Matthew Lyon authored
10 lib/couch_db/design.rb
View
@@ -4,18 +4,16 @@ class HaltedFunction < StandardError; end
extend self
- def documents
- @design_documents ||= {}
- end
-
+ DOCUMENTS = {}
+
def handle(command=[])
case cmd = command.shift
when 'new'
id, ddoc = command[0], command[1]
- documents[id] = ddoc
+ DOCUMENTS[id] = ddoc
true
else
- doc = documents[cmd]
+ doc = DOCUMENTS[cmd]
action, name = command.shift
func = name ? doc[action][name] : doc[action]
func = Sandbox.make_proc(func)
15 lib/couch_db/view.rb
View
@@ -2,14 +2,12 @@ module CouchDB
module View
extend self
- def map_functions
- @functions ||= []
- end
-
+ FUNCTIONS = []
+
def add_map_function(funcstr)
response = Sandbox.make_proc(funcstr)
if response.is_a?(Proc)
- map_functions.push(response)
+ FUNCTIONS.push(response)
true
else
response
@@ -17,15 +15,12 @@ def add_map_function(funcstr)
end
def reset
- map_functions.clear
+ FUNCTIONS.clear
end
def map(doc)
- map_functions.map do |func|
+ FUNCTIONS.map do |func|
MapRunner.new(func).run(doc)
- # runner = MapRunner.new
- # runner.instance_exec(doc, &func)
- # runner.results
end
end
74 test/benches/ivar_vs_constant.rb
View
@@ -0,0 +1,74 @@
+module Smackdown
+ extend self
+ HSH = {}
+
+ def hsh
+ @hsh ||= {}
+ end
+
+ ARY = []
+
+ def ary
+ @ary ||= []
+ end
+end
+
+require 'benchmark'
+
+puts "\nsetting hash key"
+n = 100000
+Benchmark.bm do |x|
+ x.report("to ::CONSTANT ") do
+ n.times {|i| Smackdown::HSH[(i%1000).to_s] = i }
+ end
+
+ x.report("to @ivar via .method") do
+ n.times {|i| Smackdown.hsh[(i%1000).to_s] = i }
+ end
+end
+
+puts "\nreading hash key"
+n = 1000000
+Benchmark.bm do |x|
+ x.report("from ::CONSTANT "){ n.times{|i| Smackdown::HSH[(i%1000).to_s] } }
+ x.report("from @ivar via .method"){ n.times{|i| Smackdown.hsh[(i%1000).to_s] } }
+end
+
+puts "\npushing to array, clearing every 1000"
+n = 100000
+Benchmark.bm do |x|
+ x.report("to ::CONSTANT ") do
+ n.times do |i|
+ Smackdown::ARY.clear if i%1000 == 1
+ Smackdown::ARY << i
+ end
+ end
+
+ x.report("to @ivar via .method") do
+ n.times do |i|
+ Smackdown.ary.clear if i%1000 == 1
+ Smackdown.ary << i
+ end
+ end
+end
+
+puts "\nmapping from array"
+n = 1000
+
+Smackdown.ary.clear
+Smackdown::ARY.clear
+1000.times do
+ Smackdown::ARY << rand(10000)
+ Smackdown.ary << rand(10000)
+end
+
+Benchmark.bm do |x|
+ x.report("from ::CONSTANT ") do
+ n.times { Smackdown::ARY.map{|i| i.to_s } }
+ end
+
+ x.report("from @ivar via .method") do
+ n.times { Smackdown.ary.map{|i| i.to_s } }
+ end
+end
+
8 test/design_doc_test.rb
View
@@ -2,18 +2,18 @@
context "teaching a design document" do
setup do
- CouchDB::Design.documents.clear
+ CouchDB::Design::DOCUMENTS.clear
@ddoc = {"_id" => "foo"}
CouchDB.run(["ddoc", "new", "foo", @ddoc])
end
test "stores the document" do
- assert_equal @ddoc, CouchDB::Design.documents["foo"]
+ assert_equal @ddoc, CouchDB::Design::DOCUMENTS["foo"]
end
test "survives a reset" do
CouchDB.run(["reset"])
- assert 1, CouchDB::Design.documents.size
+ assert 1, CouchDB::Design::DOCUMENTS.size
end
-end
+end
4 test/view_test.rb
View
@@ -9,8 +9,8 @@
test "compiles functions correctly" do
response = CouchDB.run(["add_fun", "lambda {|doc| emit(nil, nil)}"])
assert_equal true, response
- assert_equal 1, CouchDB::View.map_functions.size
- assert_kind_of Proc, CouchDB::View.map_functions.first
+ assert_equal 1, CouchDB::View::FUNCTIONS.size
+ assert_kind_of Proc, CouchDB::View::FUNCTIONS.first
end
test "errors on non-valid source" do
Please sign in to comment.
Something went wrong with that request. Please try again.