TAChart: sorting can be a bit faster for free
Original Reporter info from Mantis: Marcin Wiazowski
-
Reporter name:
Original Reporter info from Mantis: Marcin Wiazowski
- Reporter name:
Description:
I'm attaching a patch, that makes TCustomSortedChartSource.DoSort() a bit faster - when using SpeedTest application, attached to #35356 (closed), Test 3 and Test 5 give about 7% shorter execution time.
Currently, we have a QuickSort() procedure, which is internal to TCustomSortedChartSource.DoSort():
procedure TCustomSortedChartSource.DoSort;
procedure QuickSort(L, R: Longint);
begin
...
TCustomSortedChartSource's FData.List is used here
TCustomSortedChartSource's DoCompare() is used here
...
end;
begin
...
QuickSort(...);
end;
This creates some needless indirection levels. Along with "L" and "R", QuickSort() gets an additional, implicit "Self" parameter, passed by the compiler - so QuickSort() is able to access TCustomSortedChartSource instance.
Accessing list looks like:
- read Self, stored on the stack
- by using Self, read FData
- by using FData, read List
Accessing compare function looks like:
- read Self, stored on the stack
- by using Self, call DoCompare()
The attached patch moves QuickSort() outside the TCustomSortedChartSource.DoSort() implementation - so "Self" is no longer passed to it. Instead, QuickSort() gets two new parameters: "List" and "Compare".
Accessing list looks like:
- read List, stored on the stack
Accessing compare function looks like:
- by using address, stored on the stack, call DoCompare()
Since list and compare function are extremely heavily used, this optimization - although simple - gives a noticeable improvement. On the contrary, as I checked, similar changes in other TCustomSortedChartSource's methods don't give any noticeable improvement.
Additionally, QuickSort() uses now a stdcall calling convention.
When using a default, i.e. pascal calling convention, parameters are passed to QuickSort() in CPU registers, and then QuickSort() - in its "begin" section - stores these registers on the stack.
When using a stdcall calling convention, parameters are passed to QuickSort() on the stack - so "begin" no longer needs to store them on the stack, since they are already there.
Mantis conversion info:
- Mantis ID: 35630
- Build: 61288
- Version: 2.1 (SVN)
- Fixed in revision: 61305 (#bd29c5ea)