On Thursday, 29 May 2014 at 12:04:35 UTC, monarch_dodra wrote:
On Thursday, 29 May 2014 at 08:49:10 UTC, Chris wrote:
monarch_dodra:
Hm. This last point might be an issue. If I process a large
input (text in this case) then I might run into trouble with
"append" as a class variable. I also had a weird bug, because
I didn't clear the memory for overwrite.
You can always implement an "upper bound" approach, where if
your input data becomes larger than a certain size, you return
the data directly, and reset your appender. EG:
Appender!(MyType[]) append;
public auto doSomething() {
scope (failure) { append.clear; }
// ... do something
append ~= item;
MyType[] ret;
if (append.data.length < 10_000)
{
ret = append.data).dup;
append.clear; //clears buffer, keeps memory.
}
else
{
ret = append.data;
append = appender!(MyType[])(); //jettison old appender
data.
}
return ret;
}
Yes, you're right, this is a possible solution. However, I don't
feel good about keeping memory I don't need. This might be a
problem when the code will finally be ported to mobile devices.
I benchmarked the two implementations (class variable vs local
variable) and the performance (i.e. speed) remains the same.