Skip to content

Commit 5cb91dd

Browse files
committed
src: modernize cleanup queue to use c++20
1 parent 4cf6fab commit 5cb91dd

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

src/cleanup_queue-inl.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ void CleanupQueue::Remove(Callback cb, void* arg) {
2929
cleanup_hooks_.erase(search);
3030
}
3131

32+
constexpr auto CleanupQueue::CleanupHookCallback::operator<=>(
33+
const CleanupHookCallback& other) const noexcept {
34+
if (insertion_order_counter_ > other.insertion_order_counter_) {
35+
return std::strong_ordering::greater;
36+
}
37+
38+
if (insertion_order_counter_ < other.insertion_order_counter_) {
39+
return std::strong_ordering::less;
40+
}
41+
return std::strong_ordering::equivalent;
42+
}
43+
3244
} // namespace node
3345

3446
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

src/cleanup_queue.cc

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,20 @@ namespace node {
88
std::vector<CleanupQueue::CleanupHookCallback> CleanupQueue::GetOrdered()
99
const {
1010
// Copy into a vector, since we can't sort an unordered_set in-place.
11-
std::vector<CleanupHookCallback> callbacks(cleanup_hooks_.begin(),
12-
cleanup_hooks_.end());
11+
std::vector callbacks(cleanup_hooks_.begin(), cleanup_hooks_.end());
1312
// We can't erase the copied elements from `cleanup_hooks_` yet, because we
1413
// need to be able to check whether they were un-scheduled by another hook.
1514

16-
std::sort(callbacks.begin(),
17-
callbacks.end(),
18-
[](const CleanupHookCallback& a, const CleanupHookCallback& b) {
19-
// Sort in descending order so that the most recently inserted
20-
// callbacks are run first.
21-
return a.insertion_order_counter_ > b.insertion_order_counter_;
22-
});
15+
// Sort in descending order so that the most recently inserted callbacks are
16+
// run first.
17+
std::ranges::sort(callbacks, std::greater());
2318

2419
return callbacks;
2520
}
2621

2722
void CleanupQueue::Drain() {
28-
std::vector<CleanupHookCallback> callbacks = GetOrdered();
29-
30-
for (const CleanupHookCallback& cb : callbacks) {
31-
if (cleanup_hooks_.count(cb) == 0) {
23+
for (const CleanupHookCallback& cb : GetOrdered()) {
24+
if (!cleanup_hooks_.contains(cb)) {
3225
// This hook was removed from the `cleanup_hooks_` set during another
3326
// hook that was run earlier. Nothing to do here.
3427
continue;

src/cleanup_queue.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class CleanupQueue : public MemoryRetainer {
4141
arg_(arg),
4242
insertion_order_counter_(insertion_order_counter) {}
4343

44+
constexpr auto operator<=>(const CleanupHookCallback& other) const noexcept;
45+
4446
// Only hashes `arg_`, since that is usually enough to identify the hook.
4547
struct Hash {
4648
size_t operator()(const CleanupHookCallback& cb) const;

0 commit comments

Comments
 (0)