Skip to content

Commit

Permalink
Changed foreach to for to avoid copies + cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Poita committed Dec 30, 2012
1 parent d24519e commit 9d8c78d
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions std/array.d
Expand Up @@ -1461,24 +1461,25 @@ ElementEncodingType!(ElementType!RoR)[] join(RoR, R)(RoR ror, R sep)
else
alias sep sepArr;

auto result = appender!(RetType)();
auto result = appender!RetType();
static if(isForwardRange!RoR &&
(isNarrowString!RetType || hasLength!RoRElem))
{
// Reserve appender length if it can be computed.
size_t resultLen = 0;
foreach (r; ror.save)
resultLen += r.length + sepArr.length;
resultLen -= sepArr.length;
immutable sepArrLength = sepArr.length;
for (auto temp = ror.save; !temp.empty; temp.popFront())
resultLen += temp.front.length + sepArrLength;
resultLen -= sepArrLength;
result.reserve(resultLen);
version(unittest) scope(exit) assert(result.data.length == resultLen);
}
put(result, ror.front);
ror.popFront();
foreach (r; ror)
for (; !ror.empty; ror.popFront())
{
put(result, sepArr);
put(result, r);
put(result, ror.front);
}
return result.data;
}
Expand All @@ -1488,20 +1489,22 @@ ElementEncodingType!(ElementType!RoR)[] join(RoR)(RoR ror)
if(isInputRange!RoR &&
isInputRange!(ElementType!RoR))
{
alias typeof(return) RetType;

if (ror.empty)
return typeof(return).init;
return RetType.init;

alias ElementType!RoR R;
auto result = appender!(typeof(return))();
auto result = appender!RetType();
static if(isForwardRange!RoR && (hasLength!R || isNarrowString!R))
{
// Reserve appender length if it can be computed.
immutable resultLen = reduce!("a + b.length")(cast(size_t) 0, ror.save);
result.reserve(resultLen);
version(unittest) scope(exit) assert(result.data.length == resultLen);
}
foreach (r; ror)
put(result, r);
for (; !ror.empty; ror.popFront())
put(result, ror.front);
return result.data;
}

Expand Down

0 comments on commit 9d8c78d

Please sign in to comment.