Permalink
Browse files

Tiny improvement to memory handling in CpsOp.Sequence

  • Loading branch information...
1 parent be42b29 commit ffe8cda775c5995c9274ed5dd08e44b4b23f442c @sorear committed Oct 30, 2011
Showing with 14 additions and 6 deletions.
  1. +14 −6 lib/CodeGen.cs
View
@@ -547,6 +547,9 @@ sealed class Tokens {
public const int NumInt32 = 2;
public const int NumInline = 10;
+
+ // other random stuff
+ public static readonly ClrOp[] EmptyClrOp = new ClrOp[0];
}
// This are expressional CLR operators. This is lower level than the
@@ -1662,7 +1665,7 @@ class CpsOp {
// the head MUST NOT have cases
public ClrOp head;
- public CpsOp(ClrOp head) : this(new ClrOp[0], head) { }
+ public CpsOp(ClrOp head) : this(Tokens.EmptyClrOp, head) { }
public CpsOp(ClrOp[] stmts, ClrOp head) {
if (head.HasCases)
throw new Exception("head must not have cases");
@@ -1722,18 +1725,23 @@ class CpsOp {
public static CpsOp Sequence(params CpsOp[] terms) {
if (terms.Length == 0) return new CpsOp(ClrNoop.Instance);
- List<ClrOp> stmts = new List<ClrOp>();
+ int k = -1;
+ foreach (CpsOp t in terms)
+ k += 1 + t.stmts.Length;
+ ClrOp[] stmts = new ClrOp[k];
+ k = 0;
+
for (int i = 0; i < terms.Length - 1; i++) {
if (terms[i].head.Returns != Tokens.Void)
throw new Exception("Non-void expression used in nonfinal sequence position" + terms[i].head.Returns);
foreach (ClrOp s in terms[i].stmts)
- stmts.Add(s);
- stmts.Add(terms[i].head);
+ stmts[k++] = s;
+ stmts[k++] = terms[i].head;
}
foreach (ClrOp s in terms[terms.Length - 1].stmts)
- stmts.Add(s);
- return new CpsOp(stmts.ToArray(), terms[terms.Length - 1].head);
+ stmts[k++] = s;
+ return new CpsOp(stmts, terms[terms.Length - 1].head);
}
static ClrOp StripResult(ClrOp it) {

0 comments on commit ffe8cda

Please sign in to comment.