Permalink
Browse files

Unroll the range iterator for further wins (I think this idea was fro…

…m an old blog of colomon++ 's)
  • Loading branch information...
1 parent c85099a commit 599cbcb32698c19b208d9da6db55e29224f5c3d3 @sorear committed Dec 2, 2012
Showing with 29 additions and 21 deletions.
  1. +29 −21 lib/Kernel.cs
View
@@ -3853,32 +3853,40 @@ class CtxNumRangeReify : ContextHandler<Variable[]> {
public override Variable[] Get(Variable obj) {
var nrmo = setting.NumericRangeIterMO;
P6any o = obj.Fetch();
- var curv = (Variable)o.GetSlot(nrmo, "$!current");
- var endv = (Variable)o.GetSlot(nrmo, "$!limit");
- var excl = (Variable)o.GetSlot(nrmo, "$!exclusive");
+ var cur = ((Variable)o.GetSlot(nrmo, "$!current")).Fetch();
+ var end = ((Variable)o.GetSlot(nrmo, "$!limit")).Fetch();
+ var excl = ((Variable)o.GetSlot(nrmo, "$!exclusive")).Fetch();
- var curo = curv.Fetch();
- var endo = endv.Fetch();
+ const int unroll = 10;
+ var buffer = new Variable[unroll + 1];
+ int wp = 0;
+ bool terminate = false;;
- int cmp = Builtins.numcompare_core(setting, curv, curo, endv, endo, false);
+ while (wp < unroll && !terminate) {
+ int cmp = Builtins.numcompare_core(setting, cur, cur, end, end, false);
- if (cmp == Builtins.O_IS_LESS) {
- P6opaque newobj = new P6opaque(setting.NumericRangeIterMO);
- newobj.slots[0] = curo.mo.mro_succ.Get(curv);
- newobj.slots[1] = endo;
- newobj.slots[2] = excl;
-
- return new Variable[] {
- curo,
- newobj
- };
- }
- else if (cmp == Builtins.O_IS_GREATER || excl.Fetch().mo.mro_raw_Bool.Get(excl)) {
- return new Variable[0];
+ if (cmp == Builtins.O_IS_LESS) {
+ buffer[wp++] = cur;
+ cur = cur.mo.mro_succ.Get(cur);
+ }
+ else if (cmp == Builtins.O_IS_GREATER || excl.Fetch().mo.mro_raw_Bool.Get(excl)) {
+ terminate = true;
+ }
+ else {
+ terminate = true;
+ buffer[wp++] = cur;
+ }
}
- else {
- return new Variable[] { curo };
+
+ if (!terminate) {
+ P6opaque newobj = new P6opaque(nrmo);
+ newobj.slots[0] = cur;
+ newobj.slots[1] = end;
+ newobj.slots[2] = excl;
+ buffer[wp++] = newobj;
}
+ Array.Resize(ref buffer, wp);
+ return buffer;
}
}

0 comments on commit 599cbcb

Please sign in to comment.