View Issue Details

IDProjectCategoryView StatusLast Update
0008214FPCCompilerpublic2007-10-22 15:25
ReporterChristian Iversen Assigned ToFlorian  
PrioritylowSeverityfeatureReproducibilityalways
Status closedResolutionfixed 
Platformi386OSLinux 
Product Version2.2.0 
Fixed in Version2.4.0 
Summary0008214: Returning interface from function is inefficient
DescriptionI've attached a piece of a sample program in "Additional information".

Please note the following:

1) Functions f1 and f2 are _exactly_ equally fast
2) They even generate exactly the same code (right down to reg names)
3) Only the way in which the result is handled, is different

4) 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 Informationprocedure 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.
TagsNo tags attached.
Fixed in Revision8558
FPCOldBugId0
FPCTarget-
Attached Files

Relationships

related to 0009472 closedYuriy Sydorov "as" increase .RefCount in INTERFACE 
related to 0020907 closedJonas Maebe [Feature] A string Result value of a function should be initialized automatically 

Activities

Florian

2007-01-26 09:24

administrator   ~0011122

This isn't easy to fix. Doing the transformation
s:=f; => f(s)
requires several checks basically if f has side effects on s and if s is used while constructing the call, e.g.: s:=f(f2(s)); For most practical cases this doesn't matter but the compiler has to take care of it because people would find it.

Thorsten Engler

2007-01-27 21:28

reporter   ~0011150

For what it's worth, Delphi threats functions returning an interface always as procedures with an additional var (not out!) parameter.

Florian

2007-01-27 21:48

administrator   ~0011151

Yes, FPC too, but the address passed is a reference to a temp. var and after function call an interface copy is done else things like
var
  i : iunknown;

function f : iunknown;
  begin
    result:=nil;
    i:=tinterfacedobject.create;
  end;


begin
  i:=f;
  writeln(longint(i));
end.
will go wrong.

Yuriy Sydorov

2007-09-04 11:18

manager   ~0014498

More correct example is:

var
  i : iunknown;

function f : iunknown;
var
  ii: iinterfacedobject;
  begin
    result:=nil;
    ii:=i as iinterfacedobject; // <- Here i is destroyed already
  end;


begin
  i:=f;
end.

Issue History

Date Modified Username Field Change
2007-01-25 23:27 Christian Iversen New Issue
2007-01-25 23:27 Christian Iversen Status new => assigned
2007-01-25 23:27 Christian Iversen Assigned To => Jonas Maebe
2007-01-25 23:27 Christian Iversen FPCOldBugId => 0
2007-01-25 23:27 Christian Iversen FPCTarget => -
2007-01-26 09:24 Florian Note Added: 0011122
2007-01-26 10:09 Jonas Maebe Assigned To Jonas Maebe =>
2007-01-26 10:09 Jonas Maebe Priority normal => low
2007-01-26 10:09 Jonas Maebe Status assigned => new
2007-01-27 19:04 Jonas Maebe Severity minor => feature
2007-01-27 21:28 Thorsten Engler Note Added: 0011150
2007-01-27 21:48 Florian Note Added: 0011151
2007-02-21 17:25 Florian Status new => assigned
2007-02-21 17:25 Florian Assigned To => Florian
2007-09-03 14:04 Florian Relationship added related to 0009472
2007-09-04 11:18 Yuriy Sydorov Note Added: 0014498
2007-09-20 23:35 Peter Vreman Fixed in Revision => 8558
2007-09-20 23:35 Peter Vreman Status assigned => resolved
2007-09-20 23:35 Peter Vreman Fixed in Version => 2.3.1
2007-09-20 23:35 Peter Vreman Resolution open => fixed
2007-10-22 15:25 Florian Status resolved => closed
2011-12-19 16:58 Jonas Maebe Relationship added related to 0020907