From 62b79b52b11275dbd83ba0e5b39094591f5810bf Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Thu, 4 Jun 2026 00:28:42 -0400 Subject: [PATCH 1/2] perf: apply spaghetti inspired labeling procedure to 6-connected binary --- cc3d/cc3d_binary.hpp | 148 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 132 insertions(+), 16 deletions(-) diff --git a/cc3d/cc3d_binary.hpp b/cc3d/cc3d_binary.hpp index ada23ed..0f58b71 100644 --- a/cc3d/cc3d_binary.hpp +++ b/cc3d/cc3d_binary.hpp @@ -574,43 +574,159 @@ OUT* connected_components3d_6_binary( const int64_t xstart = runs[row << 1]; const int64_t xend = runs[(row << 1) + 1]; - for (int64_t x = xstart; x < xend; x++) { - loc = x + sx * (y + sy * z); + loc = xstart + sx * (y + sy * z); + const int64_t loc_end = loc + (xend - xstart); - if (in_labels[loc] == 0) { + if (in_labels[loc] == 0) { + goto BLACK; + } + else if (y > 0 && in_labels[loc + K]) { + out_labels[loc] = out_labels[loc + K]; + + if (z > 0 && in_labels[loc + E] && !in_labels[loc + B]) { + equivalences.unify(out_labels[loc], out_labels[loc + E]); + goto SIMPLE; + } + goto SIMPLE_K; + } + else if (z > 0 && in_labels[loc + E]) { + out_labels[loc] = out_labels[loc + E]; + goto SIMPLE_E; + } + else { + next_label++; + out_labels[loc] = next_label; + equivalences.add(out_labels[loc]); + goto STANDARD; + } + + STANDARD: + loc++; + if (loc >= loc_end) { continue; } - else if (x > 0 && in_labels[loc + M]) { - out_labels[loc] = out_labels[loc + M]; - if (y > 0 && in_labels[loc + K] && !in_labels[loc + J]) { - equivalences.unify(out_labels[loc], out_labels[loc + K]); - if (z > 0 && in_labels[loc + E]) { - if (!in_labels[loc + D] && !in_labels[loc + B]) { - equivalences.unify(out_labels[loc], out_labels[loc + E]); - } + if (in_labels[loc] == 0) { + goto BLACK; + } + + out_labels[loc] = out_labels[loc + M]; + + if (y > 0 && in_labels[loc + K] && !in_labels[loc + J]) { + equivalences.unify(out_labels[loc], out_labels[loc + K]); + if (z > 0 && in_labels[loc + E]) { + if (!in_labels[loc + D] && !in_labels[loc + B]) { + equivalences.unify(out_labels[loc], out_labels[loc + E]); } + goto SIMPLE; } - else if (z > 0 && in_labels[loc + E] && !in_labels[loc + D]) { - equivalences.unify(out_labels[loc], out_labels[loc + E]); + goto SIMPLE_K; + } + else if (z > 0 && in_labels[loc + E] && !in_labels[loc + D]) { + equivalences.unify(out_labels[loc], out_labels[loc + E]); + goto SIMPLE_E; + } + + goto STANDARD; + + SIMPLE: + loc++; + if (loc >= loc_end) { + continue; + } + + if (in_labels[loc] == 0) { + goto BLACK; + } + + out_labels[loc] = out_labels[loc + M]; + + if (y > 0 && in_labels[loc + K]) { + if (z > 0 && in_labels[loc + E]) { + goto SIMPLE; } + goto SIMPLE_K; + } + else if (z > 0 && in_labels[loc + E]) { + goto SIMPLE_E; + } + goto STANDARD; + + SIMPLE_E: + loc++; + if (loc >= loc_end) { + continue; + } + + if (in_labels[loc] == 0) { + goto BLACK; + } + + out_labels[loc] = out_labels[loc + M]; + + if (y > 0 && in_labels[loc + K] && !in_labels[loc + J]) { + equivalences.unify(out_labels[loc], out_labels[loc + K]); + goto SIMPLE_K; + } + goto STANDARD; + + SIMPLE_K: + loc++; + if (loc >= loc_end) { + continue; + } + + if (in_labels[loc] == 0) { + goto BLACK; + } + + out_labels[loc] = out_labels[loc + M]; + + if (y > 0 && in_labels[loc + K]) { + if (z > 0 && in_labels[loc + E]) { + if (!in_labels[loc + B]) { + equivalences.unify(out_labels[loc], out_labels[loc + E]); + } + goto SIMPLE; + } + goto SIMPLE_K; + } + else if (z > 0 && in_labels[loc + E]) { + equivalences.unify(out_labels[loc], out_labels[loc + E]); + goto SIMPLE_E; + } + goto STANDARD; + + BLACK: + loc++; + if (loc >= loc_end) { + continue; + } + + if (in_labels[loc] == 0) { + goto BLACK; } else if (y > 0 && in_labels[loc + K]) { out_labels[loc] = out_labels[loc + K]; - if (z > 0 && in_labels[loc + E] && !in_labels[loc + B]) { - equivalences.unify(out_labels[loc], out_labels[loc + E]); + if (z > 0 && in_labels[loc + E]) { + if (!in_labels[loc + B]) { + equivalences.unify(out_labels[loc], out_labels[loc + E]); + } + goto SIMPLE; } + goto SIMPLE_K; } else if (z > 0 && in_labels[loc + E]) { out_labels[loc] = out_labels[loc + E]; + goto SIMPLE_E; } else { next_label++; out_labels[loc] = next_label; equivalences.add(out_labels[loc]); + goto STANDARD; } - } } } From 46917a13f04924e3879084cde462fc1d2cbe1533 Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Tue, 16 Jun 2026 20:06:59 -0400 Subject: [PATCH 2/2] perf: disable EPL for all binary images --- cc3d/fastcc3d.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cc3d/fastcc3d.pyx b/cc3d/fastcc3d.pyx index 0f0699a..248b264 100644 --- a/cc3d/fastcc3d.pyx +++ b/cc3d/fastcc3d.pyx @@ -374,11 +374,11 @@ def connected_components( binary_image = binary_image or (dtype == bool) # EPL saves time for multilabel because it helps us reduce the size of - # the union-find allocation. However, for 2D binary images, we can use + # the union-find allocation. However, for binary images, we can use # a static calculation to save 1/2 to 1/8 of the allocation. We unfortunately # lose hyperfast calculation of extremely sparse images (e.g. 1 pixel or line of pixels) # but reducing passes on the image improves times significantly for typical images. - if binary_image and connectivity in (4,8): + if binary_image: epl = voxels first_foreground_row = 0 last_foreground_row = sy