Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/models/storm_report.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "models/from_tree.hpp"
#include "models/json.hpp"

#include <algorithm>
#include <cstddef>
#include <utility>

namespace spc {
Expand All @@ -19,8 +21,10 @@ StormReportPayload detail::storm_reports_from_tree(const Json& root) {
return payload;
}
const Json::array_t& features = features_node->get_array();
// Every IEM feature has properties, so this sizes the list exactly.
payload.reports.reserve(features.size());
// Count first so a body padded with values that aren't features can't make
// the list reserve room for reports it never gets.
payload.reports.reserve(static_cast<std::size_t>(std::ranges::count_if(
features, [](const Json& feat) { return detail::lookup(feat, "properties") != nullptr; })));
for (const Json& feat : features) {
const Json* props = detail::lookup(feat, "properties");
if (props == nullptr) {
Expand Down
12 changes: 12 additions & 0 deletions tests/test_arcgis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,18 @@ TEST(Models, WatchNumbersThatAreNotFiniteIntegersBecomeZero) {
}
}

TEST(Models, StormReportsReserveOnlyForReports) {
// Values that aren't features become no reports, so they reserve nothing.
std::string body = R"({"type":"FeatureCollection","features":[)";
for (int i = 0; i < 10000; ++i) {
body += i == 0 ? "0" : ",0";
}
body += "]}";
const StormReportPayload p = parse_storm_reports(body);
EXPECT_TRUE(p.reports.empty());
EXPECT_EQ(p.reports.capacity(), std::vector<StormReport>{}.capacity());
}

TEST(Models, StormReportsParseIemLsr) {
const StormReportPayload p = parse_storm_reports(read_fixture("iem_storm_reports.json"));
ASSERT_GT(p.reports.size(), 0u);
Expand Down
Loading