From c1ba82bcc92daa8736d83f1759c4226a3c3d4cb1 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 2 Mar 2024 18:49:42 +0100 Subject: [PATCH] core: avoid pointless copying in git parser When iterating over the converted strings of a line, the first entry of the array would be popped off, leading to a full copy of the remaining array. Instead, use an index in the parser state. Signed-off-by: Berthold Stoeger --- core/load-git.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/core/load-git.cpp b/core/load-git.cpp index bbe9066fa..abc89b12b 100644 --- a/core/load-git.cpp +++ b/core/load-git.cpp @@ -53,6 +53,7 @@ struct git_parser_state { struct divelog *log = nullptr; int o2pressure_sensor = 0; std::vector converted_strings; + size_t act_converted_string = 0; }; struct keyword_action { @@ -337,14 +338,12 @@ static void parse_site_geo(char *line, struct git_parser_state *state) static std::string pop_cstring(struct git_parser_state *state, const char *err) { - if (state->converted_strings.empty()) { + if (state->act_converted_string >= state->converted_strings.size()) { report_error("git-load: string marker without any strings ('%s')", err); return std::string(); } - std::string res = std::move(state->converted_strings.front()); - // This copies the whole vector. Keep an index instead! - state->converted_strings.erase(state->converted_strings.begin()); - return res; + size_t idx = state->act_converted_string++; + return std::move(state->converted_strings[idx]); } /* Parse key=val parts of samples and cylinders etc */ @@ -1365,6 +1364,7 @@ static void for_each_line(git_blob *blob, line_fn_t *fn, struct git_parser_state while (size) { state->converted_strings.clear(); + state->act_converted_string = 0; unsigned int n = parse_one_line(content, size, fn, state); content += n; size -= n;