Skip to content

Commit ce1c5a8

Browse files
committed
Remove overhead of TLS on river point detection
1 parent 4fd9461 commit ce1c5a8

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

src/openvic-simulation/map/MapDefinition.cpp

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -675,23 +675,14 @@ void MapDefinition::_trace_river(BMP& rivers_bmp, ivec2_t start, river_t& river)
675675
TraceSegment segment = stack.top();
676676
stack.pop();
677677

678-
thread_local memory::vector<ivec2_t> points;
679-
points.emplace_back(segment.point);
680-
678+
// Force heap initialiser ellision
679+
memory::vector<ivec2_t> points{ segment.point };
681680
uint8_t size = river_data[segment.point.x + segment.point.y * rivers_bmp.get_width()] - 1; // determine river size by colour
682681
bool river_complete = false;
683682
size_t recursion_limit = 0;
684683

685-
while(true) {
686-
++recursion_limit;
687-
if (recursion_limit == RIVER_RECURSION_LIMIT) {
688-
spdlog::error_s(
689-
"River segment starting @ ({}, {}) exceeded length limit of 4096 pixels. Check for misplaced pixel or circular river.",
690-
points.front().x, points.front().y
691-
);
692-
break;
693-
}
694-
684+
size_t limit;
685+
for(limit = 0; limit < RIVER_RECURSION_LIMIT; ++limit) {
695686
size_t idx = segment.point.x + segment.point.y * rivers_bmp.get_width();
696687

697688
ivec2_t merge_point;
@@ -706,7 +697,7 @@ void MapDefinition::_trace_river(BMP& rivers_bmp, ivec2_t start, river_t& river)
706697
direction_t old_direction;
707698
direction_t new_direction;
708699
};
709-
static constexpr std::array<Neighbour, 4> neighbours = {{
700+
alignas(64) static constexpr std::array<Neighbour, 4> neighbours = {{
710701
{ {0, -1}, UP, DOWN }, // Down
711702
{ {1, 0}, LEFT, RIGHT }, // Right
712703
{ {0, 1}, DOWN, UP }, // Up
@@ -741,18 +732,21 @@ void MapDefinition::_trace_river(BMP& rivers_bmp, ivec2_t start, river_t& river)
741732
points.emplace_back(merge_point);
742733
river_complete = true;
743734
break;
744-
}
745-
746-
if (new_segment_found) {
735+
} else if (new_segment_found) {
747736
stack.push({ new_segment_point, new_segment_direction });
748737
break;
749-
}
750-
751-
if (!new_segment_found) {
738+
} else if (!new_segment_found) {
752739
break; // no neighbours left to check, end segment
753740
}
754741
}
755742

743+
if (limit == RIVER_RECURSION_LIMIT) {
744+
spdlog::error_s(
745+
"River segment starting @ ({}, {}) exceeded limit of {} pixels. Check for a misplaced pixel or circular river.",
746+
points.front().x, points.front().y, RIVER_RECURSION_LIMIT
747+
);
748+
}
749+
756750
// simplify points to only include first, last, and corners
757751
const auto is_corner_point = [](ivec2_t previous, ivec2_t current, ivec2_t next) -> bool {
758752
return ((current.x - previous.x) * (next.y - current.y)) != ((current.y - previous.y) * (next.x - current.x)); //slope is fun!

0 commit comments

Comments
 (0)