Cost of .dup vs. instantiation
TheFlyingFiddle via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed May 28 10:52:16 PDT 2014
On Wednesday, 28 May 2014 at 14:36:25 UTC, Chris wrote:
> I use Appender to fill an array. The Appender is a class
> variable and is not instantiated with each function call to
> save instantiation. However, the return value or the function
> must be dup'ed, like so:
>
> Appender!(MyType[]) append;
> public auto doSomething() {
> scope (exit) { // clear append }
> // ... do something
> append ~= item;
> return (append.data).dup
> }
>
> My question is whether I save anything with Appender as a class
> variable here. I have to .dup the return value (+ clear the
> Appender). If I had a new Appender with each function call, it
> might be just as good.
>
> public auto doSomething() {
> Appender!(MyType[]) append;
> // ....
> return append.data.
> }
>
> Right or wrong?
When it comes to optimizations it's hard to say. Benchmarking is
better than relying advice/opinions on the internet in any case.
That being said i doubt that the instantiation cost of the
Appender is relevant. (Btw the appender is not a class variable!
It is a struct with reference semantics). Reusing an appender is
more for those cases where you want to reuse the underlying
memory of the appender itself.
More information about the Digitalmars-d-learn
mailing list