From c43500cc517f4bc5f612bc567cf547cf6ce34ccf Mon Sep 17 00:00:00 2001 From: James Moschou Date: Thu, 3 Dec 2026 14:21:28 +0100 Subject: [PATCH 1/8] Fix whitespace formatting --- Sources/ComputeCxx/Graph/Graph.cpp | 8 ++++---- Tests/ComputeSwiftTests/Shared/MetadataTests.swift | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Sources/ComputeCxx/Graph/Graph.cpp b/Sources/ComputeCxx/Graph/Graph.cpp index 95b624b5..f3c9915f 100644 --- a/Sources/ComputeCxx/Graph/Graph.cpp +++ b/Sources/ComputeCxx/Graph/Graph.cpp @@ -735,7 +735,7 @@ void Graph::add_output_edge(data::ptr template <> void Graph::remove_output_edge(data::ptr node, AttributeID output) { auto *iter = std::find_if(node->output_edges().begin(), node->output_edges().end(), - [&output](auto iter) -> bool { return iter.attribute == output; }); + [&output](auto iter) -> bool { return iter.attribute == output; }); if (iter != node->output_edges().end()) { node->output_edges().erase(iter); } @@ -748,7 +748,7 @@ void Graph::remove_output_edge(data::ptr node, AttributeID output) { template <> void Graph::remove_output_edge(data::ptr node, AttributeID output) { auto *iter = std::find_if(node->output_edges().begin(), node->output_edges().end(), - [&output](auto iter) -> bool { return iter.attribute == output; }); + [&output](auto iter) -> bool { return iter.attribute == output; }); if (iter != node->output_edges().end()) { node->output_edges().erase(iter); } @@ -790,7 +790,7 @@ void Graph::update_main_refs(AttributeID attribute) { if (auto node = attribute.get_node()) { const auto &type = this->attribute_type(node->type_id()); // TODO: should AttributeType have deleted - // copy constructor? + // copy constructor? bool main_ref = false; if (type.value_metadata().getValueWitnesses()->isPOD() || type.flags() & IAGAttributeTypeFlagsAsyncThread) { @@ -2027,7 +2027,7 @@ void Graph::add_trace(Trace *_Nullable trace) { void Graph::remove_trace(IAGUniqueID trace_id) { auto *iter = std::remove_if(_traces.begin(), _traces.end(), - [&trace_id](auto trace) -> bool { return trace->id() == trace_id; }); + [&trace_id](auto trace) -> bool { return trace->id() == trace_id; }); if (iter) { Trace *trace = *iter; trace->end_trace(*this); diff --git a/Tests/ComputeSwiftTests/Shared/MetadataTests.swift b/Tests/ComputeSwiftTests/Shared/MetadataTests.swift index ddf03413..06cd510b 100644 --- a/Tests/ComputeSwiftTests/Shared/MetadataTests.swift +++ b/Tests/ComputeSwiftTests/Shared/MetadataTests.swift @@ -96,7 +96,7 @@ struct MetadataTests { @Suite struct SignatureTests { func equatableConformance() { - func areEqual(lhs: T, rhs:T) -> Bool where T: Equatable { + func areEqual(lhs: T, rhs: T) -> Bool where T: Equatable { lhs == rhs } let signature1 = Metadata(TestClass.self).signature From b2a6808044634948958159401031817c7310bc8e Mon Sep 17 00:00:00 2001 From: James Moschou Date: Thu, 3 Dec 2026 14:01:05 +0100 Subject: [PATCH 2/8] Optimize input searching in Graph::index_of_input_slow --- Sources/ComputeCxx/Graph/Graph.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Sources/ComputeCxx/Graph/Graph.cpp b/Sources/ComputeCxx/Graph/Graph.cpp index f3c9915f..ecde6d59 100644 --- a/Sources/ComputeCxx/Graph/Graph.cpp +++ b/Sources/ComputeCxx/Graph/Graph.cpp @@ -6,6 +6,7 @@ #else #include #endif +#include #include #include #include @@ -859,11 +860,12 @@ uint32_t Graph::index_of_input(Node &node, InputEdge::Comparator comparator) { uint32_t Graph::index_of_input_slow(Node &node, InputEdge::Comparator comparator) { node.sort_input_edges_if_needed(); - InputEdge *search_start = - std::find_if(node.input_edges().begin(), node.input_edges().end(), - [&comparator](InputEdge &input_edge) { return input_edge.attribute == comparator.attribute; }); + InputEdge *search_start = std::partition_point( + node.input_edges().begin(), node.input_edges().end(), + [&comparator](InputEdge &input_edge) { return input_edge.attribute < comparator.attribute; }); uint32_t index = uint32_t(search_start - node.input_edges().begin()); - for (auto iter = search_start, end = node.input_edges().end(); iter != end; ++iter) { + for (auto iter = search_start, end = node.input_edges().end(); + iter != end && iter->attribute == comparator.attribute; ++iter) { if (comparator.match(*iter)) { return index; } From 17a148e89925e7417b540e401047c2c0157535e5 Mon Sep 17 00:00:00 2001 From: James Moschou Date: Thu, 3 Dec 2026 14:05:31 +0100 Subject: [PATCH 3/8] Don't zero out the buffer when constructing InlineHeap objects --- Sources/Utilities/include/Utilities/Heap.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Utilities/include/Utilities/Heap.h b/Sources/Utilities/include/Utilities/Heap.h index c8ac457c..0ff573b5 100644 --- a/Sources/Utilities/include/Utilities/Heap.h +++ b/Sources/Utilities/include/Utilities/Heap.h @@ -63,7 +63,7 @@ class Heap { template class InlineHeap : public Heap { private: - char _inline_buffer[_inline_size] = {}; + char _inline_buffer[_inline_size]; public: InlineHeap() : Heap(_inline_buffer, _inline_size, 0) {} From bb8b6b9937884e9323c1eca318c7a179241926d1 Mon Sep 17 00:00:00 2001 From: James Moschou Date: Thu, 3 Dec 2026 14:34:56 +0100 Subject: [PATCH 4/8] Optimize Graph::remove_subgraph --- Sources/ComputeCxx/Graph/Graph.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/ComputeCxx/Graph/Graph.cpp b/Sources/ComputeCxx/Graph/Graph.cpp index ecde6d59..286ad271 100644 --- a/Sources/ComputeCxx/Graph/Graph.cpp +++ b/Sources/ComputeCxx/Graph/Graph.cpp @@ -252,8 +252,10 @@ void Graph::add_subgraph(Subgraph &subgraph) { } void Graph::remove_subgraph(Subgraph &subgraph) { - auto *iter = std::remove(_subgraphs.begin(), _subgraphs.end(), &subgraph); - _subgraphs.erase(iter, _subgraphs.end()); + auto *pos = std::lower_bound(_subgraphs.begin(), _subgraphs.end(), &subgraph); + if (pos != _subgraphs.end() && *pos == &subgraph) { + _subgraphs.erase(pos); + } if (auto *map = _tree_data_elements_by_subgraph.get()) { auto iter = map->find(&subgraph); From a835ce0c71db3de0e3bc2355ed19faeafd500e35 Mon Sep 17 00:00:00 2001 From: James Moschou Date: Thu, 3 Dec 2026 15:37:57 +0100 Subject: [PATCH 5/8] Optimize vector::erase Move-assign the elements after the erased ones down and destroy the leftover elements at the end, rather than destroying the erased elements and swapping live elements with them, which was also invalid for elements that aren't trivially copyable. --- Sources/ComputeCxx/Vector/Vector.h | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/Sources/ComputeCxx/Vector/Vector.h b/Sources/ComputeCxx/Vector/Vector.h index 4e8bf275..228ce4e4 100644 --- a/Sources/ComputeCxx/Vector/Vector.h +++ b/Sources/ComputeCxx/Vector/Vector.h @@ -285,17 +285,12 @@ template requires std::unsigned_integral vector::iterator vector::erase(iterator first, iterator last) { - auto count = last - first; - if (count == 0) { + if (first == last) { return last; } - for (auto iter = first; iter != last; iter++) { - iter->~T(); - } - for (auto iter = last, old_end = end(); iter != old_end; iter++) { - std::swap(*(iter - count), *iter); - } - _size -= count; + iterator new_end = std::move(last, end(), first); + std::destroy(new_end, end()); + _size = (size_type)(new_end - begin()); return end(); } @@ -601,17 +596,12 @@ vector::iterator vector::erase(iterator pos) { template requires std::unsigned_integral vector::iterator vector::erase(iterator first, iterator last) { - auto count = last - first; - if (count == 0) { + if (first == last) { return last; } - for (auto iter = first; iter != last; iter++) { - iter->~T(); - } - for (auto iter = last, old_end = end(); iter != old_end; iter++) { - std::swap(*(iter - count), *iter); - } - _size -= count; + iterator new_end = std::move(last, end(), first); + std::destroy(new_end, end()); + _size = (size_type)(new_end - begin()); return end(); } From 02cb5516550c3ff449234f4c9f03f323caacc52a Mon Sep 17 00:00:00 2001 From: James Moschou Date: Thu, 3 Dec 2026 14:51:31 +0100 Subject: [PATCH 6/8] Optimize data::vector::erase --- Sources/ComputeCxx/Data/Vector.h | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Sources/ComputeCxx/Data/Vector.h b/Sources/ComputeCxx/Data/Vector.h index 4e73c972..620c3533 100644 --- a/Sources/ComputeCxx/Data/Vector.h +++ b/Sources/ComputeCxx/Data/Vector.h @@ -1,6 +1,9 @@ #pragma once +#include #include +#include +#include #include "ComputeCxx/IAGBase.h" #include "Pointer.h" @@ -23,6 +26,9 @@ class vector { using const_reverse_iterator = std::reverse_iterator; using size_type = uint32_t; + // Elements are relocated by copying their bytes when the zone reallocates them. + static_assert(std::is_trivially_copyable_v); + private: struct Metadata { // capacity can be calculated via 1 << capacity_exponent @@ -184,17 +190,12 @@ vector::iterator vector::erase(iterator pos) { template vector::iterator vector::erase(iterator first, iterator last) { - auto count = last - first; - if (count == 0) { + if (first == last) { return last; } - for (auto iter = first; iter != last; iter++) { - iter->~T(); - } - for (auto iter = last, old_end = end(); iter != old_end; iter++) { - std::swap(*(iter - count), *iter); - } - _metadata.size -= count; + iterator new_end = std::move(last, end(), first); + std::destroy(new_end, end()); + _metadata.size = (size_type)(new_end - begin()); return end(); } From 66130f04c0611d65a852d6a72a1df83cf74bf344 Mon Sep 17 00:00:00 2001 From: James Moschou Date: Thu, 3 Dec 2026 14:56:08 +0100 Subject: [PATCH 7/8] Make erase compatible with std::vector::erase Take const_iterators and return the iterator following the erased elements, rather than the new end(). The unique_ptr specialization shares the same implementation as the other vectors. --- Sources/ComputeCxx/Data/Vector.h | 17 ++++---- Sources/ComputeCxx/Graph/Graph.cpp | 3 +- Sources/ComputeCxx/Vector/Vector.h | 64 +++++++++++++++--------------- 3 files changed, 42 insertions(+), 42 deletions(-) diff --git a/Sources/ComputeCxx/Data/Vector.h b/Sources/ComputeCxx/Data/Vector.h index 620c3533..69e977a5 100644 --- a/Sources/ComputeCxx/Data/Vector.h +++ b/Sources/ComputeCxx/Data/Vector.h @@ -124,8 +124,8 @@ class vector { [[nodiscard]] iterator insert(zone *zone, const_iterator pos, const T &value); [[nodiscard]] iterator insert(zone *zone, const_iterator pos, T &&value); - iterator erase(iterator pos); - iterator erase(iterator first, iterator last); + iterator erase(const_iterator pos); + iterator erase(const_iterator first, const_iterator last); void push_back(zone *zone, const T &value); void push_back(zone *zone, T &&value); @@ -181,22 +181,23 @@ vector::iterator vector::insert(zone *zone, const_iterator pos, T &&value) } template -vector::iterator vector::erase(iterator pos) { - if (pos == end()) { +vector::iterator vector::erase(const_iterator pos) { + if (pos == cend()) { return end(); } return erase(pos, pos + 1); } template -vector::iterator vector::erase(iterator first, iterator last) { +vector::iterator vector::erase(const_iterator first, const_iterator last) { + iterator mutable_first = begin() + (first - cbegin()); if (first == last) { - return last; + return mutable_first; } - iterator new_end = std::move(last, end(), first); + iterator new_end = std::move(mutable_first + (last - first), end(), mutable_first); std::destroy(new_end, end()); _metadata.size = (size_type)(new_end - begin()); - return end(); + return mutable_first; } template diff --git a/Sources/ComputeCxx/Graph/Graph.cpp b/Sources/ComputeCxx/Graph/Graph.cpp index 286ad271..555dac5b 100644 --- a/Sources/ComputeCxx/Graph/Graph.cpp +++ b/Sources/ComputeCxx/Graph/Graph.cpp @@ -299,7 +299,8 @@ void Graph::invalidate_subgraphs() { subgraph->set_graph_invalidating_subgraphs(false); if (!subgraph->has_cached_nodes()) { - end = _subgraphs_with_cached_nodes.erase(iter); + iter = _subgraphs_with_cached_nodes.erase(iter); + end = _subgraphs_with_cached_nodes.end(); } else { ++iter; } diff --git a/Sources/ComputeCxx/Vector/Vector.h b/Sources/ComputeCxx/Vector/Vector.h index 228ce4e4..eb9a7030 100644 --- a/Sources/ComputeCxx/Vector/Vector.h +++ b/Sources/ComputeCxx/Vector/Vector.h @@ -100,8 +100,8 @@ class vector { iterator insert(const_iterator pos, const T &value); iterator insert(const_iterator pos, T &&value); - iterator erase(iterator pos); - iterator erase(iterator first, iterator last); + iterator erase(const_iterator pos); + iterator erase(const_iterator first, const_iterator last); void push_back(const T &value); void push_back(T &&value); @@ -274,8 +274,8 @@ vector::iterator vector requires std::unsigned_integral -vector::iterator vector::erase(iterator pos) { - if (pos == end()) { +vector::iterator vector::erase(const_iterator pos) { + if (pos == cend()) { return end(); } return erase(pos, pos + 1); @@ -283,15 +283,16 @@ vector::iterator vector requires std::unsigned_integral -vector::iterator vector::erase(iterator first, - iterator last) { +vector::iterator vector::erase(const_iterator first, + const_iterator last) { + iterator mutable_first = begin() + (first - cbegin()); if (first == last) { - return last; + return mutable_first; } - iterator new_end = std::move(last, end(), first); + iterator new_end = std::move(mutable_first + (last - first), end(), mutable_first); std::destroy(new_end, end()); _size = (size_type)(new_end - begin()); - return end(); + return mutable_first; } template @@ -440,8 +441,8 @@ class vector { iterator insert(const_iterator pos, const T &value); iterator insert(const_iterator pos, T &&value); - iterator erase(iterator pos); - iterator erase(iterator first, iterator last); + iterator erase(const_iterator pos); + iterator erase(const_iterator first, const_iterator last); void push_back(const T &value); void push_back(T &&value); @@ -586,8 +587,8 @@ vector::iterator vector::insert(const_iterator template requires std::unsigned_integral -vector::iterator vector::erase(iterator pos) { - if (pos == end()) { +vector::iterator vector::erase(const_iterator pos) { + if (pos == cend()) { return end(); } return erase(pos, pos + 1); @@ -595,14 +596,15 @@ vector::iterator vector::erase(iterator pos) { template requires std::unsigned_integral -vector::iterator vector::erase(iterator first, iterator last) { +vector::iterator vector::erase(const_iterator first, const_iterator last) { + iterator mutable_first = begin() + (first - cbegin()); if (first == last) { - return last; + return mutable_first; } - iterator new_end = std::move(last, end(), first); + iterator new_end = std::move(mutable_first + (last - first), end(), mutable_first); std::destroy(new_end, end()); _size = (size_type)(new_end - begin()); - return end(); + return mutable_first; } template @@ -741,8 +743,8 @@ class vector, 0, _size_type> { iterator insert(const_iterator pos, const std::unique_ptr &value); iterator insert(const_iterator pos, std::unique_ptr &&value); - iterator erase(iterator pos); - iterator erase(iterator first, iterator last); + iterator erase(const_iterator pos); + iterator erase(const_iterator first, const_iterator last); void push_back(const std::unique_ptr &value) = delete; void push_back(std::unique_ptr &&value); @@ -813,8 +815,8 @@ void vector, 0, size_type>::reserve(size_type n template requires std::unsigned_integral vector, 0, size_type>::iterator -vector, 0, size_type>::erase(iterator pos) { - if (pos == end()) { +vector, 0, size_type>::erase(const_iterator pos) { + if (pos == cend()) { return end(); } return erase(pos, pos + 1); @@ -823,19 +825,15 @@ vector, 0, size_type>::erase(iterator pos) { template requires std::unsigned_integral vector, 0, size_type>::iterator -vector, 0, size_type>::erase(iterator first, iterator last) { - auto count = last - first; - if (count == 0) { - return last; - } - for (auto iter = first; iter != last; iter++) { - iter->reset(); - } - for (auto iter = last, old_end = end(); iter != old_end; iter++) { - std::swap(*(iter - count), *iter); +vector, 0, size_type>::erase(const_iterator first, const_iterator last) { + iterator mutable_first = begin() + (first - cbegin()); + if (first == last) { + return mutable_first; } - _size -= count; - return end(); + iterator new_end = std::move(mutable_first + (last - first), end(), mutable_first); + std::destroy(new_end, end()); + _size = (size_type)(new_end - begin()); + return mutable_first; } template From 9f286d71ae8bb33d4680cf44265c513b3f330ef4 Mon Sep 17 00:00:00 2001 From: James Moschou Date: Thu, 3 Dec 2026 14:54:54 +0100 Subject: [PATCH 8/8] Use binary search to find a tree element's nodes TreeDataElement::nodes() is sorted by tree element, so encoding or describing a tree no longer scans every node for each tree element. --- Sources/ComputeCxx/Graph/Graph.cpp | 3 ++- Sources/ComputeCxx/Graph/Graph.mm | 7 ++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Sources/ComputeCxx/Graph/Graph.cpp b/Sources/ComputeCxx/Graph/Graph.cpp index 555dac5b..77672c32 100644 --- a/Sources/ComputeCxx/Graph/Graph.cpp +++ b/Sources/ComputeCxx/Graph/Graph.cpp @@ -2317,7 +2317,8 @@ void Graph::encode_tree(Encoder &encoder, data::ptr tree) const { if (auto *tree_data = tree_data_element_for_subgraph(subgraph)) { auto &nodes = tree_data->nodes(); std::pair, data::ptr> *found = - std::find_if(nodes.begin(), nodes.end(), [&tree](auto node) { return node.first == tree; }); + std::lower_bound(nodes.begin(), nodes.end(), tree, + [](auto &node, auto tree) { return node.first.offset() < tree.offset(); }); for (auto *node = found; node != nodes.end(); ++node) { if (node->first != tree) { diff --git a/Sources/ComputeCxx/Graph/Graph.mm b/Sources/ComputeCxx/Graph/Graph.mm index 021cdbac..f85e08fa 100644 --- a/Sources/ComputeCxx/Graph/Graph.mm +++ b/Sources/ComputeCxx/Graph/Graph.mm @@ -700,10 +700,11 @@ int trap_cycles() { if (auto tree_data_element = graph->tree_data_element_for_subgraph(subgraph)) { auto &data_nodes = tree_data_element->nodes(); - std::pair, data::ptr> *found = std::find_if( - data_nodes.begin(), data_nodes.end(), [&tree](auto node) { return node.first == tree; }); + std::pair, data::ptr> *found = std::lower_bound( + data_nodes.begin(), data_nodes.end(), tree, + [](auto &node, auto tree) { return node.first.offset() < tree.offset(); }); - if (found != data_nodes.end()) { + if (found != data_nodes.end() && found->first == tree) { NSMutableArray *nodes = [NSMutableArray array]; for (auto node = found; node != data_nodes.end(); ++node) { if (node->first != tree) {