Skip to content
Browse files

optimization for methods with a catch-all branch

  • Loading branch information...
1 parent 13d4749 commit 847e893f31d901119bdbeb7fe24dc4a83ab23053 @vito committed May 7, 2012
Showing with 45 additions and 26 deletions.
  1. +45 −26 lib/atomy/method.rb
View
71 lib/atomy/method.rb
@@ -129,14 +129,28 @@ def build
g.set_line 0
done = g.new_label
- mismatch = g.new_label
g.push_state Rubinius::AST::ClosedScope.new(0)
g.state.push_name @name
has_args = @branches.any? { |b| b.total_args > 0 || b.splat }
+ always_matches =
+ @branches.any? { |b|
+ # receiver must always match
+ b.receiver.always_matches_self? &&
+ # must take no arguments (otherwise calling with invalid arg count
+ # would mismatch, as branches can take different arg sizes)
+ #
+ # TODO?: if all branches take same arg size, check if any are
+ # wildcards
+ (b.total_args == 0) &&
+
+ # and either have no splat or a wildcard splat
+ (!b.splat || b.splat.wildcard?)
+ }
+
if has_args
g.splat_index = 0
end
@@ -146,35 +160,40 @@ def build
build_methods(g, @branches, done)
- unless @name == :initialize
- g.invoke_primitive :vm_check_super_callable, 0
- g.gif mismatch
+ unless always_matches
+ unless @name == :initialize
+ no_super = g.new_label
- g.push_proc
- if g.state.super?
- g.zsuper g.state.super.name
- else
- g.zsuper nil
- end
+ g.invoke_primitive :vm_check_super_callable, 0
+ g.gif no_super
- g.goto done
- end
+ g.push_proc
+ if g.state.super?
+ g.zsuper g.state.super.name
+ else
+ g.zsuper nil
+ end
- # no method branches matched; fail
- mismatch.set!
- g.push_self
- g.push_cpath_top
- g.find_const :Atomy
- g.find_const :MethodFail
- g.push_literal @name
- if has_args
- g.push_local 0
- else
- g.push_nil
+ g.goto done
+
+ no_super.set!
+ end
+
+ # no method branches matched; fail
+ g.push_self
+ g.push_cpath_top
+ g.find_const :Atomy
+ g.find_const :MethodFail
+ g.push_literal @name
+ if has_args
+ g.push_local 0
+ else
+ g.push_nil
+ end
+ g.send :new, 2
+ g.allow_private
+ g.send :raise, 1
end
- g.send :new, 2
- g.allow_private
- g.send :raise, 1
# successfully evaluated a branch
done.set!

0 comments on commit 847e893

Please sign in to comment.
Something went wrong with that request. Please try again.