From 12a45c2ff12ab77a30f4d515d238308e63ea210b Mon Sep 17 00:00:00 2001 From: Dominik Charousset Date: Fri, 22 Dec 2023 11:58:14 +0100 Subject: [PATCH] Avoid extra copies in threading_field_to_data Passing the `broker::data` paramter for `threading_field_to_data` by value forces copies. However, the function only needs const access to its parameter. Hence, taking the parameter as const-ref is the logical choice and avoids unnecessary copies of individual data fields. --- src/broker/Data.cc | 8 ++++++-- src/broker/Data.h | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/broker/Data.cc b/src/broker/Data.cc index 4440bc6f5d..aa7928add8 100644 --- a/src/broker/Data.cc +++ b/src/broker/Data.cc @@ -1154,13 +1154,17 @@ broker::data threading_field_to_data(const threading::Field* f) { return broker::vector({name, secondary, type, subtype, optional}); } -threading::Field* data_to_threading_field(broker::data d) { +threading::Field* data_to_threading_field(const broker::data& d) { if ( ! holds_alternative(d) ) return nullptr; auto& v = get(d); + + if ( v.size() < 5 ) + return nullptr; + auto name = get_if(&v[0]); - auto secondary = v[1]; + const auto& secondary = v[1]; auto type = get_if(&v[2]); auto subtype = get_if(&v[3]); auto optional = get_if(&v[4]); diff --git a/src/broker/Data.h b/src/broker/Data.h index 2b91d404f6..4bdfe8e82c 100644 --- a/src/broker/Data.h +++ b/src/broker/Data.h @@ -101,7 +101,7 @@ broker::data threading_field_to_data(const threading::Field* f); * @return a pointer to a new zeek::threading::Value or a nullptr if the conversion was not * possible. */ -threading::Field* data_to_threading_field(broker::data d); +threading::Field* data_to_threading_field(const broker::data& d); /** * A Zeek value which wraps a Broker data value.