-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PORT] sorting code improvements+optimizations (#4797)
* Micros timSort slightly (#74889) ## About The Pull Request Datum var reads are expensive, and sorting code does a lot of them. let's work on that together, as a group. There's maybe 250ms of sorting cost sitting in mostly global variable work. I'd like to start chopping at that. My tracy profiles aren't the most helpful, estimating this to save about 14% of timSort over the course of init, tho that's slightly noisy and not the most reliable ## Why It's Good For The Game speed * Code cleanup: Sorting (#83017) 1. Removes code duplication 2. Fully documents `sortTim()` 3. Makes a define with default sortTim behavior, straight and to the point for 95% of cases 4. Migrates other sorts into the same file 5. Removes some redundancy where they're reassigning a variable using an in place sorter For the record, we only use timSort More documentation, easier to read, uses `length` over `len`, etc Should be no gameplay effect at all --------- Co-authored-by: LemonInTheDark <[email protected]> Co-authored-by: Jeremiah <[email protected]>
- Loading branch information
1 parent
7620fa3
commit f978433
Showing
16 changed files
with
116 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/// Sorts the list in place with timSort, default settings. | ||
#define SORT_TIM(to_sort, associative) if(length(to_sort) >= 2) { \ | ||
var/datum/sort_instance/sorter = GLOB.sortInstance; \ | ||
if (isnull(sorter)) { \ | ||
sorter = new; \ | ||
} \ | ||
sorter.L = to_sort; \ | ||
sorter.cmp = GLOBAL_PROC_REF(cmp_numeric_asc); \ | ||
sorter.associative = associative; \ | ||
sorter.timSort(1, 0); \ | ||
} | ||
|
||
|
||
/// Helper for the sorting procs. Prevents some code duplication. Creates /datum/sort_instance/sorter | ||
#define CREATE_SORT_INSTANCE(to_sort, cmp, associative, fromIndex, toIndex) \ | ||
if(length(to_sort) < 2) { \ | ||
return to_sort; \ | ||
} \ | ||
fromIndex = fromIndex % length(to_sort); \ | ||
toIndex = toIndex % (length(to_sort) + 1); \ | ||
if (fromIndex <= 0) { \ | ||
fromIndex += length(to_sort); \ | ||
} \ | ||
if (toIndex <= 0) { \ | ||
toIndex += length(to_sort) + 1; \ | ||
} \ | ||
var/datum/sort_instance/sorter = GLOB.sortInstance; \ | ||
if (isnull(sorter)) { \ | ||
sorter = new; \ | ||
} \ | ||
sorter.L = to_sort; \ | ||
sorter.cmp = cmp; \ | ||
sorter.associative = associative; | ||
|
||
|
||
/** | ||
* ## Tim Sort | ||
* Hybrid sorting algorithm derived from merge sort and insertion sort. | ||
* | ||
* **Sorts in place**. | ||
* You might not need to get the return value. | ||
* | ||
* @see | ||
* https://en.wikipedia.org/wiki/Timsort | ||
* | ||
* @param {list} to_sort - The list to sort. | ||
* | ||
* @param {proc} cmp - The comparison proc to use. Default: Numeric ascending. | ||
* | ||
* @param {boolean} associative - Whether the list is associative. Default: FALSE. | ||
* | ||
* @param {int} fromIndex - The index to start sorting from. Default: 1. | ||
* | ||
* @param {int} toIndex - The index to stop sorting at. Default: 0. | ||
*/ | ||
/proc/sortTim(list/to_sort, cmp = GLOBAL_PROC_REF(cmp_numeric_asc), associative = FALSE, fromIndex = 1, toIndex = 0) as /list | ||
CREATE_SORT_INSTANCE(to_sort, cmp, associative, fromIndex, toIndex) | ||
|
||
sorter.timSort(fromIndex, toIndex) | ||
|
||
return to_sort | ||
|
||
|
||
/** | ||
* ## Merge Sort | ||
* Divide and conquer sorting algorithm. | ||
* | ||
* @see | ||
* - https://en.wikipedia.org/wiki/Merge_sort | ||
*/ | ||
/proc/sortMerge(list/to_sort, cmp = GLOBAL_PROC_REF(cmp_numeric_asc), associative = FALSE, fromIndex = 1, toIndex = 0) as /list | ||
CREATE_SORT_INSTANCE(to_sort, cmp, associative, fromIndex, toIndex) | ||
|
||
sorter.mergeSort(fromIndex, toIndex) | ||
|
||
return to_sort | ||
|
||
|
||
/** | ||
* ## Insertion Sort | ||
* Simple sorting algorithm that builds the final sorted list one item at a time. | ||
* | ||
* @see | ||
* - https://en.wikipedia.org/wiki/Insertion_sort | ||
*/ | ||
/proc/sortInsert(list/to_sort, cmp = GLOBAL_PROC_REF(cmp_numeric_asc), associative = FALSE, fromIndex = 1, toIndex = 0) as /list | ||
CREATE_SORT_INSTANCE(to_sort, cmp, associative, fromIndex, toIndex) | ||
|
||
sorter.binarySort(fromIndex, toIndex) | ||
|
||
return to_sort | ||
|
||
|
||
#undef CREATE_SORT_INSTANCE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters