Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

[not bug] range check optimization #348

Closed
monaka opened this Issue Jul 6, 2012 · 0 comments

Comments

Projects
None yet
2 participants
Contributor

monaka commented Jul 6, 2012

This is not bug.
I think compares in range.c is well-behaved but slightly redundant.
How about optimize like this?

diff --git a/src/range.c b/src/range.c
index 859bb27..34ca79a 100644
--- a/src/range.c
+++ b/src/range.c
@@ -185,14 +185,14 @@ mrb_range_eq(mrb_state *mrb, mrb_value range)
 static int
 r_le(mrb_state *mrb, mrb_value a, mrb_value b)
 {
-  //int c;
   mrb_value r = mrb_funcall(mrb, a, "<=>", 1, b); /* compare result */
   /* output :a < b => -1, a = b =>  0, a > b => +1 */

-  if (mrb_nil_p(r)) return FALSE;
+  if (mrb_type(r) == MRB_TT_FIXNUM) {
+    int c = mrb_fixnum(r);
+    if (c == 0 || c == -1) return TRUE;
+  }

-  if (mrb_obj_equal(mrb, r, mrb_fixnum_value(0))) return TRUE;
-  if (mrb_obj_equal(mrb, r, mrb_fixnum_value(-1))) return TRUE;
   return FALSE;
 }

@matz matz closed this in 84ec555 Jul 6, 2012

@monaka monaka added a commit to monaka/mruby that referenced this issue Jul 7, 2012

@monaka monaka Range optimization. similar to #348 e896c1c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment