Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Optimized ERB rendering a bit.

  • Loading branch information...
commit f2bb98611ffc35c3cb201c2b6063d891ff841f43 1 parent 606e328
@myronmarston authored
Showing with 8 additions and 1 deletion.
  1. +3 −0  CHANGELOG.md
  2. +5 −1 lib/vcr/cassette.rb
View
3  CHANGELOG.md
@@ -7,6 +7,9 @@
migrate your cassettes and deal with migration warnings, then upgrade to the current release.
* Added some code to VCR::Cassette.new to check the options passed to the cassette and raise an error if any
invalid options are passed.
+* Optimized ERB rendering a bit. Rather than creating a new struct subclass for each time we render an ERB
+ cassette with locals, we keep a cache of reusable struct subclasses based on the desired attributes.
+ [Benchmarking](http://gist.github.com/512948) reveals this is about 28% faster.
## 1.0.3 (August 5, 2010)
View
6 lib/vcr/cassette.rb
@@ -104,6 +104,10 @@ def load_recorded_interactions
VCR.http_stubbing_adapter.stub_requests(recorded_interactions)
end
+ @@struct_cache = Hash.new do |hash, attributes|
+ hash[attributes] = Struct.new(*attributes)
+ end
+
def raw_yaml_content
content = File.read(file)
return content unless @erb
@@ -114,7 +118,7 @@ def raw_yaml_content
return template.result unless @erb.is_a?(Hash)
# create an object with methods for each desired local variable...
- local_variables = Struct.new(*@erb.keys).new(*@erb.values)
+ local_variables = @@struct_cache[@erb.keys].new(*@erb.values)
# instance_eval seems to be the only way to get the binding for ruby 1.9: http://redmine.ruby-lang.org/issues/show/2161
template.result(local_variables.instance_eval { binding })
Please sign in to comment.
Something went wrong with that request. Please try again.