Skip to content

Commit

Permalink
Refactor: add copyArrayOnWrite
Browse files Browse the repository at this point in the history
  • Loading branch information
9rnsr committed Dec 3, 2015
1 parent d96441a commit e089604
Showing 1 changed file with 21 additions and 41 deletions.
62 changes: 21 additions & 41 deletions src/dinterpret.d
Expand Up @@ -1057,6 +1057,19 @@ public:
return false;
}

static Expressions* copyArrayOnWrite(Expressions* exps, Expressions* original)
{
if (exps is original)
{
if (!original)
exps = new Expressions();
else
exps = original.copy();
++CtfeStatus.numArrayAllocs;
}
return exps;
}

/******************************** Statement ***************************/

override void visit(Statement s)
Expand Down Expand Up @@ -2564,11 +2577,7 @@ public:
*/
if (ex !is exp)
{
if (expsx is e.exps)
{
expsx = e.exps.copy();
++CtfeStatus.numArrayAllocs;
}
expsx = copyArrayOnWrite(expsx, e.exps);
(*expsx)[i] = ex;
}
}
Expand Down Expand Up @@ -2634,11 +2643,7 @@ public:
*/
if (ex !is exp)
{
if (expsx is e.elements)
{
expsx = e.elements.copy();
++CtfeStatus.numArrayAllocs;
}
expsx = copyArrayOnWrite(expsx, e.elements);
(*expsx)[i] = ex;
}
}
Expand Down Expand Up @@ -2698,16 +2703,8 @@ public:
if (ek !is ekey ||
ev !is evalue)
{
if (keysx is e.keys)
{
keysx = e.keys.copy();
++CtfeStatus.numArrayAllocs;
}
if (valuesx is e.values)
{
valuesx = e.values.copy();
++CtfeStatus.numArrayAllocs;
}
keysx = copyArrayOnWrite(keysx, e.keys);
valuesx = copyArrayOnWrite(valuesx, e.values);
(*keysx)[i] = ek;
(*valuesx)[i] = ev;
}
Expand Down Expand Up @@ -2735,16 +2732,8 @@ public:
continue;

// Remove ekey
if (keysx is e.keys)
{
keysx = e.keys.copy();
++CtfeStatus.numArrayAllocs;
}
if (valuesx is e.values)
{
valuesx = e.values.copy();
++CtfeStatus.numArrayAllocs;
}
keysx = copyArrayOnWrite(keysx, e.keys);
valuesx = copyArrayOnWrite(valuesx, e.values);
keysx.remove(i - 1);
valuesx.remove(i - 1);

Expand Down Expand Up @@ -2793,12 +2782,7 @@ public:
auto ne = new NullExp(e.loc);
ne.type = e.sd.vthis.type;

if (!e.elements)
expsx = new Expressions();
else
expsx = e.elements.copy();
++CtfeStatus.numArrayAllocs;

expsx = copyArrayOnWrite(expsx, e.elements);
expsx.push(ne);
++dim;
}
Expand Down Expand Up @@ -2831,11 +2815,7 @@ public:
*/
if (ex !is exp)
{
if (expsx is e.elements)
{
expsx = e.elements.copy();
++CtfeStatus.numArrayAllocs;
}
expsx = copyArrayOnWrite(expsx, e.elements);
(*expsx)[i] = ex;
}
}
Expand Down

0 comments on commit e089604

Please sign in to comment.