Skip to content

Commit

Permalink
compile template method on first use instead of eval'ing
Browse files Browse the repository at this point in the history
When we added method compilation through unbound method we put this
in place because it sped up some benchmarks IIRC. But it means we
have to maintain two evaluation systems and hides issues with the
compiled approach in tests since most templates are run only once.

/cc @judofyr
  • Loading branch information
rtomayko committed Sep 19, 2011
1 parent ca2a1d5 commit 22aa477
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions lib/tilt/template.rb
Expand Up @@ -123,25 +123,15 @@ def prepare
end
end

# Execute the compiled template and return the result string. Template
# evaluation is guaranteed to be performed in the scope object with the
# locals specified and with support for yielding to the block.
#
# This method is only used by source generating templates. Subclasses that
# override render() may not support all features.
def evaluate(scope, locals, &block)
cached_evaluate(scope, locals, &block)
end

# Process the template and return the result. The first time this
# method is called, the template source is evaluated with instance_eval.
# On the sequential method calls it will compile the template to an
# unbound method which will lead to better performance. In any case,
# template executation is guaranteed to be performed in the scope object
# with the locals specified and with support for yielding to the block.
def cached_evaluate(scope, locals, &block)
# Redefine itself to use method compilation the next time:
def self.cached_evaluate(scope, locals, &block)
method = compiled_method(locals.keys)
method.bind(scope).call(locals, &block)
end

# Use instance_eval the first time:
evaluate_source(scope, locals, &block)
method = compiled_method(locals.keys)
method.bind(scope).call(locals, &block)
end

# Generates all template source by combining the preamble, template, and
Expand Down

0 comments on commit 22aa477

Please sign in to comment.