optimisations, and some fixes for ruby 1.8.7

This commit is contained in:
Andrew Williams 2011-03-06 09:58:54 +10:30
parent 2a9305ebd3
commit 3ad4e1e979
5 changed files with 47 additions and 34 deletions

View file

@ -1,5 +1,6 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
require 'rubygems'
require 'benchmark' require 'benchmark'
$:.unshift(File.dirname(__FILE__) + '/../lib') $:.unshift(File.dirname(__FILE__) + '/../lib')
@ -50,6 +51,8 @@ def run_benchmark
n.times { b.string = "test" } n.times { b.string = "test" }
end end
next if ENV["BENCHMARK_STRING"]
x.report("assign integer:") do x.report("assign integer:") do
n.times { b.number = 1 } n.times { b.number = 1 }
end end
@ -102,12 +105,14 @@ end
begin begin
if supports_dirty? if supports_dirty?
puts "with use_dirty true" if !ENV['BENCHMARK_DIRTY_OFF']
set_dirty(true) set_dirty(true)
puts "with use_dirty true"
run_benchmark run_benchmark
end
puts "\nwith use_dirty false"
set_dirty(false) set_dirty(false)
end end
puts "\nwith use_dirty false"
run_benchmark run_benchmark
end end

View file

@ -62,6 +62,7 @@ module CouchRest::Model
super super
end end
# ruby 1.9
def keep_if def keep_if
if use_dirty? && block_given? if use_dirty? && block_given?
self.keys.each do |key| self.keys.each do |key|

View file

@ -22,7 +22,7 @@ module CouchRest
def use_dirty? def use_dirty?
bdoc = base_doc bdoc = base_doc
bdoc && !bdoc.disable_dirty && bdoc.use_dirty bdoc && bdoc.use_dirty && !bdoc.disable_dirty
end end
def couchrest_attribute_will_change!(attr) def couchrest_attribute_will_change!(attr)

View file

@ -6,7 +6,9 @@ module CouchRest
included do included do
extlib_inheritable_accessor(:properties) unless self.respond_to?(:properties) extlib_inheritable_accessor(:properties) unless self.respond_to?(:properties)
extlib_inheritable_accessor(:prop_by_name) unless self.respond_to?(:prop_by_name)
self.properties ||= [] self.properties ||= []
self.prop_by_name ||= {}
raise "You can only mixin Properties in a class responding to [] and []=, if you tried to mixin CastedModel, make sure your class inherits from Hash or responds to the proper methods" unless (method_defined?(:[]) && method_defined?(:[]=)) raise "You can only mixin Properties in a class responding to [] and []=, if you tried to mixin CastedModel, make sure your class inherits from Hash or responds to the proper methods" unless (method_defined?(:[]) && method_defined?(:[]=))
end end
@ -88,7 +90,7 @@ module CouchRest
alias :attributes :properties_with_values alias :attributes :properties_with_values
def find_property(property) def find_property(property)
property.is_a?(Property) ? property : self.class.properties.detect {|p| p.to_s == property.to_s} property.is_a?(Property) ? property : self.class.prop_by_name[property.to_s]
end end
# The following methods should be accessable by the Model::Base Class, but not by anything else! # The following methods should be accessable by the Model::Base Class, but not by anything else!
@ -208,6 +210,7 @@ module CouchRest
validates_casted_model property.name validates_casted_model property.name
end end
properties << property properties << property
prop_by_name[property.to_s] = property
property property
end end

View file

@ -246,25 +246,25 @@ describe "With use_dirty(on)" do
end end
it "should report changes if an array index is modified" do it "should report changes if an array index is modified" do
should_change_array do |array| should_change_array do |array, obj|
array[0] = "keyword" array[0] = "keyword"
end end
end end
it "should report no changes if an array index is unmodified" do it "should report no changes if an array index is unmodified" do
should_not_change_array do |array| should_not_change_array do |array, obj|
array[0] = array[0] array[0] = array[0]
end end
end end
it "should report changes if an array is appended with <<" do it "should report changes if an array is appended with <<" do
should_change_array do |array| should_change_array do |array, obj|
array << 'keyword' array << 'keyword'
end end
end end
it "should report changes if an array is popped" do it "should report changes if an array is popped" do
should_change_array do |array| should_change_array do |array, obj|
array.pop array.pop
end end
end end
@ -278,13 +278,13 @@ describe "With use_dirty(on)" do
end end
it "should report changes if an array is pushed" do it "should report changes if an array is pushed" do
should_change_array do |array| should_change_array do |array, obj|
array.push("keyword") array.push("keyword")
end end
end end
it "should report changes if an array is shifted" do it "should report changes if an array is shifted" do
should_change_array do |array| should_change_array do |array, obj|
array.shift array.shift
end end
end end
@ -298,13 +298,13 @@ describe "With use_dirty(on)" do
end end
it "should report changes if an array is unshifted" do it "should report changes if an array is unshifted" do
should_change_array do |array| should_change_array do |array, obj|
array.unshift("keyword") array.unshift("keyword")
end end
end end
it "should report changes if an array is cleared" do it "should report changes if an array is cleared" do
should_change_array do |array| should_change_array do |array, obj|
array.clear array.clear
end end
end end
@ -332,83 +332,87 @@ describe "With use_dirty(on)" do
end end
it "should report changes if a hash is modified" do it "should report changes if a hash is modified" do
should_change_hash do |hash| should_change_hash do |hash, obj|
hash['color'] = 'orange' hash['color'] = 'orange'
end end
end end
it "should report no changes if a hash is unmodified" do it "should report no changes if a hash is unmodified" do
should_not_change_hash do |hash| should_not_change_hash do |hash, obj|
hash['color'] = hash['color'] hash['color'] = hash['color']
end end
end end
it "should report changes when deleting from a hash" do it "should report changes when deleting from a hash" do
should_change_hash do |hash| should_change_hash do |hash, obj|
hash.delete('color') hash.delete('color')
end end
end end
it "should report no changes when deleting a non existent key from a hash" do it "should report no changes when deleting a non existent key from a hash" do
should_not_change_hash do |hash| should_not_change_hash do |hash, obj|
hash.delete('non-existent-key') hash.delete('non-existent-key')
end end
end end
it "should report changes when clearing a hash" do it "should report changes when clearing a hash" do
should_change_hash do |hash| should_change_hash do |hash, obj|
hash.clear hash.clear
end end
end end
it "should report changes when merging changes to a hash" do it "should report changes when merging changes to a hash" do
should_change_hash do |hash| should_change_hash do |hash, obj|
hash.merge!('foo' => 'bar') hash.merge!('foo' => 'bar')
end end
end end
it "should report no changes when merging no changes to a hash" do it "should report no changes when merging no changes to a hash" do
should_not_change_hash do |hash| should_not_change_hash do |hash, obj|
hash.merge!('color' => hash['color']) hash.merge!('color' => hash['color'])
end end
end end
it "should report changes when replacing hash content" do it "should report changes when replacing hash content" do
should_change_hash do |hash| should_change_hash do |hash, obj|
hash.replace('foo' => 'bar') hash.replace('foo' => 'bar')
end end
end end
it "should report no changes when replacing hash content with same content" do it "should report no changes when replacing hash content with same content" do
should_not_change_hash do |hash| should_not_change_hash do |hash, obj|
hash.replace(hash) hash.replace(hash)
end end
end end
it "should report changes when removing records with delete_if" do it "should report changes when removing records with delete_if" do
should_change_hash do |hash| should_change_hash do |hash, obj|
hash.delete_if { true } hash.delete_if { true }
end end
end end
it "should report no changes when removing no records with delete_if" do it "should report no changes when removing no records with delete_if" do
should_not_change_hash do |hash| should_not_change_hash do |hash, obj|
hash.delete_if { false } hash.delete_if { false }
end end
end end
if {}.respond_to?(:keep_if)
it "should report changes when removing records with keep_if" do it "should report changes when removing records with keep_if" do
should_change_hash do |hash| should_change_hash do |hash, obj|
hash.keep_if { false } hash.keep_if { false }
end end
end end
it "should report no changes when removing no records with keep_if" do it "should report no changes when removing no records with keep_if" do
should_not_change_hash do |hash| should_not_change_hash do |hash, obj|
hash.keep_if { true } hash.keep_if { true }
end end
end end
end end
end
end end