Skip to content

Commit 38ef32e

Browse files
committed
Optimize extra component existence checks by using a set for lookups in streaming parser
1 parent 6f59059 commit 38ef32e

1 file changed

Lines changed: 14 additions & 24 deletions

File tree

agent_sdks/cpp/src/parser/streaming_impl.cc

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,13 @@ void A2uiStreamParserImpl::process_component_topology(nlohmann::json& comp, std:
626626
return "loading_" + id;
627627
};
628628

629+
std::set<std::string> extra_component_ids;
630+
for (const auto& ec : extra_components) {
631+
if (ec.contains("id")) {
632+
extra_component_ids.insert(ec["id"].get<std::string>());
633+
}
634+
}
635+
629636
std::function<void(nlohmann::json&)> traverse = [&](nlohmann::json& obj) {
630637
if (obj.is_object()) {
631638
if (version_ == VERSION_0_8 && obj.contains("path")) {
@@ -656,15 +663,9 @@ void A2uiStreamParserImpl::process_component_topology(nlohmann::json& comp, std:
656663

657664
nlohmann::json placeholder_comp = create_placeholder_component(placeholder_id);
658665

659-
bool exists = false;
660-
for (const auto& ec : extra_components) {
661-
if (ec.contains("id") && ec["id"] == placeholder_id) {
662-
exists = true;
663-
break;
664-
}
665-
}
666-
if (!exists) {
666+
if (extra_component_ids.find(placeholder_id) == extra_component_ids.end()) {
667667
extra_components.push_back(placeholder_comp);
668+
extra_component_ids.insert(placeholder_id);
668669
}
669670
}
670671
}
@@ -680,15 +681,9 @@ void A2uiStreamParserImpl::process_component_topology(nlohmann::json& comp, std:
680681

681682
nlohmann::json placeholder_comp = create_placeholder_component(pid);
682683

683-
bool exists = false;
684-
for (const auto& ec : extra_components) {
685-
if (ec.contains("id") && ec["id"] == pid) {
686-
exists = true;
687-
break;
688-
}
689-
}
690-
if (!exists) {
684+
if (extra_component_ids.find(pid) == extra_component_ids.end()) {
691685
extra_components.push_back(placeholder_comp);
686+
extra_component_ids.insert(pid);
692687
}
693688
}
694689
}
@@ -703,15 +698,9 @@ void A2uiStreamParserImpl::process_component_topology(nlohmann::json& comp, std:
703698

704699
nlohmann::json placeholder_comp = create_placeholder_component(placeholder_id);
705700

706-
bool exists = false;
707-
for (const auto& ec : extra_components) {
708-
if (ec.contains("id") && ec["id"] == placeholder_id) {
709-
exists = true;
710-
break;
711-
}
712-
}
713-
if (!exists) {
701+
if (extra_component_ids.find(placeholder_id) == extra_component_ids.end()) {
714702
extra_components.push_back(placeholder_comp);
703+
extra_component_ids.insert(placeholder_id);
715704
}
716705
}
717706
}
@@ -731,6 +720,7 @@ void A2uiStreamParserImpl::process_component_topology(nlohmann::json& comp, std:
731720
traverse(comp);
732721
}
733722

723+
734724
void A2uiStreamParserImpl::yield_messages(const std::vector<nlohmann::json>& messages_to_yield, std::vector<ResponsePart>& messages, bool strict_integrity) {
735725
for (const auto& m : messages_to_yield) {
736726
if (!deduplicate_data_model(m, strict_integrity)) {

0 commit comments

Comments
 (0)