Handmade Hero»Forums»Code
Stefan
20 posts
Day 18 Performance Issue with Delphi tanslation
Edited by Stefan on
I'm translating the C code to Delphi and ran into a performance issue on a specific piece:

Original:
1
2
3
4
5
6
7
inline real32
Win32GetSecondsElapsed(LARGE_INTEGER Start, LARGE_INTEGER End)
{
    real32 Result = ((real32)(End.QuadPart - Start.QuadPart) /
                     (real32)GlobalPerfCountFrequency);
    return(Result);
}


My version:
1
2
3
4
function Win32GetSecondsElapsed(Start, &End : LARGE_INTEGER): real32; inline;
begin
  Result := (&End.QuadPart - Start.QuadPart) / GlobalPerfCountFrequency;
end;


After calling the sleep function there is this code to make sure that we hit the target frame rate:
1
2
real32 TestSecondsElapsedForFrame = Win32GetSecondsElapsed(LastCounter, Win32GetWallClock());
Assert(TestSecondsElapsedForFrame < TargetSecondsPerFrame);


If I use the same code (Only that it's the Delphi version), I get an assert error.
If I change the code to this:
1
TestSecondsElapsedForFrame := ((LastCounter.QuadPart - Win32GetWallClock().QuadPart) / GlobalPerfCountFrequency);

The error goes away, so the call to the function in Delphi takes long enough to push me over the time allowed for the sleep to complete.
Does anyone know how I can fix this?
I have tried changing the parameters in the Win32GetSecondsElapsed to be passed as pointers, but it did not help.
I thought it may be because it's being passed by value and a copy needs to be made, but that is not it.
I think that the 'inline' directive is not taking effect.
Stefan
20 posts
Day 18 Performance Issue with Delphi tanslation
So I figured out that it is in fact not the function call that is taking long.
My inline code that suppoidly works, was wrong:
it should be:
1
TestSecondsElapsedForFrame := ((Win32GetWallClock().QuadPart - LastCounter.QuadPart) / GlobalPerfCountFrequency);

instead of
1
TestSecondsElapsedForFrame := ((LastCounter.QuadPart - Win32GetWallClock().QuadPart) / GlobalPerfCountFrequency);

Now this also give me an assert error.

I will investigate it again later.
Mārtiņš Možeiko
2565 posts / 2 projects
Day 18 Performance Issue with Delphi tanslation
What is &End syntax in argument list in Delphi? As far as I remember my Delphi days passing value by pointer was with ^ syntax.

Why there is difference in Start and &End declarations and usage? Are you sure you are passing values correctly and not using, I don't know, for example address of variable a counter value?
Stefan
20 posts
Day 18 Performance Issue with Delphi tanslation
Since 'End' is a reserved word in Delphi, it allows you to place an & in front of the variable, so that it does not cause a compile issue. It is purely syntactical.
Mārtiņš Možeiko
2565 posts / 2 projects
Day 18 Performance Issue with Delphi tanslation
Ah, ok. That makes sense. Then I don't know why it doesn't work. My guess would be that there is bug somewhere else in code. Have you tried outputting Start/End values inside this function to somewhere (debugger output, console, file?) and verify that variables have reasonable values.