Skip to content

Commit ba673a8

Browse files
perf: cap size of binary UF allocations
Note: 26 and 8 were having issues at maximum (8, 4) reduction so I temporarily set them to be 4 and 2.
1 parent 4e8a9fe commit ba673a8

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

cc3d/cc3d_binary.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ OUT* connected_components3d_26_binary(
4747
}
4848

4949
max_labels++; // corrects Cython estimation
50-
max_labels = std::min(max_labels, static_cast<size_t>(voxels) + 1); // + 1L for an array with no zeros
50+
max_labels = std::min(max_labels, static_cast<size_t>(voxels >> 2) + 1); // + 1L for an array with no zeros
51+
max_labels++; // if voxels is odd, then you need an extra one
5152
max_labels = std::min(max_labels, static_cast<size_t>(std::numeric_limits<OUT>::max()));
52-
53+
5354
DisjointSet<OUT> equivalences(max_labels);
5455

5556
const std::unique_ptr<uint32_t[]> runs(
@@ -462,7 +463,8 @@ OUT* connected_components3d_6_binary(
462463
}
463464

464465
max_labels++; // corrects Cython estimation
465-
max_labels = std::min(max_labels, static_cast<size_t>(voxels) + 1); // + 1L for an array with no zeros
466+
max_labels = std::min(max_labels, static_cast<size_t>(voxels >> 1) + 1); // + 1L for an array with no zeros
467+
max_labels++; // if voxels is odd, then you need an extra one
466468
max_labels = std::min(max_labels, static_cast<size_t>(std::numeric_limits<OUT>::max()));
467469

468470
DisjointSet<OUT> equivalences(max_labels);
@@ -609,6 +611,7 @@ OUT* connected_components2d_4_binary(
609611

610612
max_labels++; // corrects Cython estimation
611613
max_labels = std::min(max_labels, static_cast<size_t>(voxels >> 1) + 1); // + 1L for an array with no zeros
614+
max_labels++; // if voxels is odd, then you need an extra one
612615
max_labels = std::min(max_labels, static_cast<size_t>(std::numeric_limits<OUT>::max()));
613616

614617
DisjointSet<OUT> equivalences(max_labels);
@@ -778,7 +781,8 @@ OUT* connected_components2d_8_binary(
778781
const int64_t osx = (sx + 1) >> 1;
779782

780783
max_labels++; // corrects Cython estimation
781-
max_labels = std::max(std::min(max_labels, static_cast<size_t>(voxels) + 1), static_cast<size_t>(1L)); // can't allocate 0 arrays
784+
max_labels = std::max(std::min(max_labels, static_cast<size_t>(voxels >> 1) + 1), static_cast<size_t>(1L)); // can't allocate 0 arrays
785+
max_labels++; // if voxels is odd, then you need an extra one
782786
max_labels = std::min(max_labels, static_cast<size_t>(std::numeric_limits<OUT>::max()));
783787

784788
DisjointSet<uint32_t> equivalences(max_labels);

0 commit comments

Comments
 (0)