Well, your real issue is obviously that you are using that code, and that you believe it slows down your app. Question is still, how much it really affects your final app. (see last part of this post)
In any case, in your app, you will need the result to be used, therefore you will not have an otherwise unused local var as target (and non local vars are afaik always treated like being used / but there is a whole lot more to it).
So in your real live, that final assignment (temp to your var) has to happen. And there is no easy way round it. And since it is a managed var, it is taking extra time. (managed vars need ref count checking, and that is done (to an extend) thread save).
Even if code is inlined, those temp var effects may still happen (probably will). And if you are using a
non-local var (class field, global) , then they
may not even be optimizable / because that would change the behaviour of the app => not yours maybe, but in general, and outside the scope of a compiler being able to know).
If you must have a managed var, then the
//a,b: array of integer;
b := a;
setlength(b, length(b));
may be more efficient.
But you can't move it into a subroutine => then you will have temp vars again
Test case:
program Project1;
var a,b: array of integer;
begin
a := nil;
SetLength(a, 1000);
b := a;
writeln(ptruint(pointer(a)));
writeln(ptruint(pointer(b))); // pointing same address
setlength(b, length(b));
writeln(ptruint(pointer(a)));
writeln(ptruint(pointer(b))); // pointing diff address
readln;
end.
Well, maybe you can use a function, if you pass the arguments as "var param"
procedure CopyData(const Source: TFoo; var Dest: TFoo);
Because then you don't get any ref count interference when calling the procedure. You do get a pointer, but that should have less impact. And you loose having the data in a register (if ever it was), which again can have impact.
Though not sure if "SetLength" is register friendly. Or for that matter, any of the internally called function when managed types are involved.
The only faster bit, then would likely be to use pointers instead of managed types. (I assume you don't use threads)
And use GetMem or AllocMem. Copy the date yourself.
Use pointermath {$T+} to access the data "AData[n]" (like an array)
But consider first, if it is really worth it. How much time are you going to save in your entire app?
Is your app currently doing a loop with 1,000,000 or more such assignments? Is it stack like half a minute on that loop?
Even then, you still need to copy the memory, so it still will take time.
If your app is not stuck for such long on that loop, then likely the saving will not make a major difference to the overall speed of your app.
If you are about to optimize your app, then I recommend "valgrind --tool=callgrind" and kcachegrind.
Though that requires you to be able to run the app on Linux (a VM will do).
You get a nice chart where time was spent, and how much of the time.