Skip to content

Forward tl heap and nth_element to the standard library - #2475

Closed
nikosavola wants to merge 2 commits into
KLayout:masterfrom
nikosavola:pr-tl-algorithm
Closed

nikosavola wants to merge 2 commits into
KLayout:masterfrom
nikosavola:pr-tl-algorithm

Conversation

@nikosavola

Copy link
Copy Markdown
Contributor
  • tl::nth_element and the heap functions forward to the standard library, the copied nth_element had no introselect fallback
  • tl::sort and tl::partial_sort stay because their tie order is observable, but the copied insertion sort helpers move instead of assign
case before ms after ms speedup
std_sort_boxes/100000 9.78 10.9 0.90x
std_sort_boxes/1000000 112 122 0.92x
std_sort_ints/100000 5.67 5.26 1.08x
std_sort_ints/1000000 67.9 64.2 1.06x
tl_nth_element_dups/100000 0.524 0.535 0.98x
tl_nth_element_dups/1000000 7.37 7.28 1.01x
tl_sort_boxes/100000 9.57 10.1 0.95x
tl_sort_boxes/1000000 106 112 0.95x
tl_sort_ints/100000 5.73 5.22 1.10x
tl_sort_ints/1000000 67.8 61.9 1.10x

Measured on Intel(R) Xeon(R) Gold 6148 CPU @ 2.40GHz, g++ -O2 (qmake default), median of 7 repetitions, before and after builds interleaved.

@klayoutmatthias

Copy link
Copy Markdown
Collaborator

why?

I don't recall the exact reason I introduced that, but I think it was some MSVC STL implementation.

It did not employ std::swap, so optimized overloads where not considered.

Matthias

- `tl::nth_element` and the heap functions forward to the standard library, the copied `nth_element` had no introselect fallback
- `tl::sort` and `tl::partial_sort` stay because their tie order is observable, but the copied insertion sort helpers move instead of assign
| case | before ms | after ms | speedup |
|---|---:|---:|---:|
| std_sort_boxes/100000 | 9.78 | 10.9 | 0.90x |
| std_sort_boxes/1000000 | 112 | 122 | 0.92x |
| std_sort_ints/100000 | 5.67 | 5.26 | 1.08x |
| std_sort_ints/1000000 | 67.9 | 64.2 | 1.06x |
| tl_nth_element_dups/100000 | 0.524 | 0.535 | 0.98x |
| tl_nth_element_dups/1000000 | 7.37 | 7.28 | 1.01x |
| tl_sort_boxes/100000 | 9.57 | 10.1 | 0.95x |
| tl_sort_boxes/1000000 | 106 | 112 | 0.95x |
| tl_sort_ints/100000 | 5.73 | 5.22 | 1.10x |
| tl_sort_ints/1000000 | 67.8 | 61.9 | 1.10x |

Measured on Intel(R) Xeon(R) Gold 6148 CPU @ 2.40GHz, g++ -O2 (qmake default), median of 7 repetitions, before and after builds interleaved.
@nikosavola

Copy link
Copy Markdown
Contributor Author

Thanks, that makes sense. tl::sort still uses its own partition with tl_iter_swap. I did not change that part.

The parts I replaced did not use swap before either. The heap code just copied values. The standard nth_element uses iter_swap, and since C++11 that finds custom swap functions. Also, tl::nth_element is only used by tlKDTree, and that is only used in a test.

So this is mostly cleanup. If you think it is not worth it, I can close it.

@nikosavola nikosavola closed this Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants