Returning interface from function is inefficient
Original Reporter info from Mantis: Flawless
-
Reporter name: Christian Iversen
Original Reporter info from Mantis: Flawless
- Reporter name: Christian Iversen
Description:
I've attached a piece of a sample program in "Additional information".
Please note the following:
-
Functions f1 and f2 are exactly equally fast
-
They even generate exactly the same code (right down to reg names)
-
Only the way in which the result is handled, is different
-
The time difference is quite real. When using f2, the code is 10-20% slower.
It's probably a very simply fix in the compiler.
Additional information:
procedure f1(out x: xstr);
begin
x := '42';
end;
function f2: xstr;
begin
result := '42';
end;
procedure Main;
var
S: XStr;
X: XInt;
begin
if DoWhatIsFastest then
begin
writeln('f1');
for x := 0 to 1000*1000*10 do
f1(S);
end else
begin
writeln('f2');
for x := 0 to 1000*1000*10 do
s := f2;
end;
end;
Generated code for first assign:
leal -4(%ebp),%eax
call FPC_INTF_DECR_REF
leal -4(%ebp),%eax
call P$TEST_F1$XSTR
Generated code for second assign:
leal -12(%ebp),%eax
call FPC_INTF_DECR_REF
movl $0,-12(%ebp)
leal -12(%ebp),%eax
call P$TEST_F2$$XSTR
movl -12(%ebp),%edx
leal -4(%ebp),%eax
call fpc_intf_assign
(Particularly the fpc_intf_assign takes a while to run)
If this could be fixed, it would mean we could rewrite certain parts of a performance-critical library to use a more natural function-result style of programming, which would be very nice indeed.
Mantis conversion info:
- Mantis ID: 8214
- OS: Linux
- OS Build: 2.6
- Platform: i386
- Version: 2.2.0
- Fixed in version: 2.4.0
- Fixed in revision: 8558 (#e0cf0151)
- Monitored by: » VAleks2 (Aleks Vtyurin)