Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

A bit of optimization to ~double the speed of ~; make .join non-quadr…

…atic
  • Loading branch information...
commit 2ad9ba8a1fbff85976309fd78a28fdd86c6e6a95 1 parent b04ce0c
@sorear authored
Showing with 40 additions and 25 deletions.
  1. +12 −0 lib/Builtins.cs
  2. +11 −25 lib/CORE.setting
  3. +17 −0 lib/CodeGen.cs
View
12 lib/Builtins.cs
@@ -3064,4 +3064,16 @@ public class Blackhole : Variable {
public static Variable MakeJunction(Constants c, int type, Variable[] args) {
return c.setting.MakeJunction(type, args);
}
+
+ [ImplicitConsts] public static Variable concat(Constants c, params Variable[] bits) {
+ string[] sbits = new string[bits.Length];
+ for (int i = 0; i < bits.Length; i++)
+ sbits[i] = bits[i].Fetch().mo.mro_raw_Str.Get(bits[i]);
+ return c.setting.MakeStr(string.Concat(sbits));
+ }
+
+ [ImplicitConsts] public static Variable list_join(Constants c, Variable sep, Variable lst) {
+ string ssep = sep.Fetch().mo.mro_raw_Str.Get(sep);
+ return c.setting.MakeStr(string.Join(ssep, UnboxLoS(lst)));
+ }
}
View
36 lib/CORE.setting
@@ -1276,17 +1276,9 @@ my class Bool is Int does IntBasedEnum {
}
# }}}
# Fundamental scalar operators {{{
-sub infix:<~> is Niecza::absprec<r= list> is pure (\|$bits) { Q:CgOp {
- (letn buf (strbuf_new)
- i (int 0)
- ar (unbox fvarlist (@ {$bits}))
- max (fvarlist_length (l ar))
- [whileloop 0 0 (< (l i) (l max)) (prog
- [strbuf_append (l buf)
- (obj_getstr (fvarlist_item (l i) (l ar)))]
- [l i (+ (l i) (int 1))])]
- [box Str (strbuf_seal (l buf))])
-} }
+sub infix:<~> is Niecza::absprec<r= list> is pure is Niecza::builtin('concat', 0) (\|$bits) {
+ Q:CgOp { (concat (unbox fvarlist (@ {$bits}))) }
+}
sub infix:<max>(*@values) is pure is Niecza::absprec<k= list> { @values.max }
sub infix:<min>(*@values) is pure is equiv<max> { @values.min }
@@ -1325,12 +1317,12 @@ sub prefix:<not> is pure is Niecza::absprec<h= unary left> (\x) { not(x) }
sub infix:<x> is pure is Niecza::absprec<s=> ($str, $ct) {
my $i = +$ct;
- my $j = ''; # XXX use strbuf
+ my @j;
while $i >= 1 {
$i--;
- $j ~= $str;
+ push @j, $str;
}
- $j;
+ @j.join;
}
sub infix:<leg> is pure is Niecza::absprec<n= non> is diffy ($s1, $s2) {
@@ -1660,13 +1652,7 @@ my class List is Cool does Positional {
method elems() { +self }
- method join($sep = '') {
- my $t;
- for unitem(self) -> $x {
- $t = (defined($t) ?? ($t ~ $sep ~ $x) !! ~$x);
- }
- $t // '';
- }
+ method join($sep = '') { Q:CgOp { (list_join {$sep} {unitem(self)}) } }
method ACCEPTS(\topic) {
self // nextsame;
@@ -2708,13 +2694,13 @@ sub Niecza-trans($str, *@changes) {
}
}
- my $r = "";
+ my @r;
while $lsm.next_substitution {
- $r ~= $lsm.unsubstituted_text ~ $lsm.substituted_text;
+ push @r, $lsm.unsubstituted_text ~ $lsm.substituted_text;
}
- $r ~= $lsm.unsubstituted_text;
+ push @r, $lsm.unsubstituted_text;
- return $r;
+ return @r.join;
}
# }}}
View
17 lib/CodeGen.cs
@@ -3182,9 +3182,26 @@ class NamProcessor {
bool implicit_frame = Attribute.IsDefined(mi, typeof(ImplicitFrameAttribute));
bool implicit_consts = Attribute.IsDefined(mi, typeof(ImplicitConstsAttribute));
bool force_cps = Attribute.IsDefined(mi, typeof(CpsAttribute));
+
+ ParameterInfo[] pi = mi.GetParameters();
+ int paix = pi.Length - 1;
+ bool param_array = pi.Length > 0 &&
+ Attribute.IsDefined(pi[paix], typeof(ParamArrayAttribute));
+
return delegate(CpsOp[] cpses) {
+
if (implicit_consts) cpses = Utils.PrependArr(CpsOp.Constants(), cpses);
if (implicit_frame) cpses = Utils.PrependArr(CpsOp.CallFrame(), cpses);
+
+ if (param_array && (cpses.Length != pi.Length || !pi[paix].ParameterType.IsAssignableFrom(cpses[paix].head.Returns))) {
+ CpsOp[] n1 = new CpsOp[pi.Length];
+ CpsOp[] n2 = new CpsOp[cpses.Length - paix];
+ Array.Copy(cpses, 0, n1, 0, n1.Length - 1);
+ Array.Copy(cpses, paix, n2, 0, n2.Length);
+ n1[n1.Length - 1] = CpsOp.NewArray(pi[paix].ParameterType.GetElementType(), n2);
+ cpses = n1;
+ }
+
return CpsOp.CpsCall(force_cps ? Tokens.Variable : cps, mi, cpses);
};
}
Please sign in to comment.
Something went wrong with that request. Please try again.