Skip to content

fillContourLeft: up to 2.8x faster on meshes without regular face order; fillContourLeftUpToArea stops at area limit - #7683

Merged
Fedr merged 4 commits into
masterfrom
fill-contour-left-faster
Dec 5, 2026
Merged

Fedr merged 4 commits into
masterfrom
fill-contour-left-faster

Conversation

@Fedr

@Fedr Fedr commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor
  • fillContourLeft is faster on meshes without a regular face order:
    • a face is marked as filled when it is discovered, so each face enters the front only once (before: about twice, and half of the popped edges led to already filled faces);
    • the filling goes front by front, and the half-edge records of the edges 16 positions ahead in the current front are prefetched (as in AABBTree construction);
    • only the newly filled contour faces are expanded, each from its first contour edge, as before;
    • the contour edges are kept in EdgeHashSet instead of ParallelHashSet, and the result is returned by value instead of being copied.
  • New fillContourLeftUpToArea( mesh, contours, maxArea ) stops filling as soon as the area of the filled faces exceeds maxArea, and returns ContourLeftFill{ region, area, exceeded }. Faces are counted when they leave the front, where their half-edge records are already prefetched; on a stop the faces not counted yet are unfilled, so area is always the area of region. The same template does both fills, so the existing function gets no extra work.
  • New test MRMesh.FillContourLeft. It covers caps, bands, a scattered region (faces with 2-3 contour edges, faces touching by a vertex), a path followed by its reverse, and maxArea = 0.

Benchmark

Each case fills the left side of the boundary of the faces with center z > threshold on a unit sphere. All results matched that region exactly. fillContourLeft measured at e47d90d against its master base (at 7a17734 it measures the same within noise), on Windows, single-threaded. Times are the minimum over 6 interleaved runs of each DLL, 10 repetitions each. clang-cl is what the Windows distributions are built with; the MSVC columns are for reference.

mesh faces z > filled faces clang-cl master, ms clang-cl PR, ms speedup MSVC master, ms MSVC PR, ms speedup
makeUVSphere( 1, 2000, 2000 ) 8.0M 0 4.0M 23.9 23.1 1.03x 27.3 26.5 1.03x
0.99 0.36M 2.19 1.74 1.26x 2.48 2.11 1.17x
-0.99 7.6M 47.4 52.6 0.90x * 50.6 50.7 1.00x
makeSphere( 2M verts ) 4.0M 0 2.0M 71.6 26.1 2.7x 93.8 27.5 3.4x
0.99 18K 0.138 0.108 1.28x 0.180 0.135 1.33x
-0.99 4.0M 148.6 52.2 2.8x 186.1 54.7 3.4x
same after packOptimally() 4.0M 0 2.0M 29.2 15.5 1.9x 38.4 17.7 2.2x
0.99 18K 0.121 0.097 1.25x 0.155 0.120 1.29x
-0.99 4.0M 58.9 31.1 1.9x 78.6 35.6 2.2x

* Noise, not a slowdown: this case moves by ±15% from process to process (its topology alone is about 384 MB, so memory placement differs per run). Over 12 interleaved processes of each, with both loops copied into one clang-cl benchmark, master took 44.0 / 50.7 / 56.7 ms (min / avg / max) and this PR 40.7 / 51.6 / 62.1 ms. The UV sphere's faces are already in fill order, so the hardware prefetcher leaves little for the software prefetch to add.

fillContourLeftUpToArea with clang-cl on the same cases, at 7a17734. Without stopping it takes 2.1-2.5x the time of fillContourLeft, which is the cost of computing the area of every triangle; with maxArea set to half of the region's area it stops halfway:

mesh z > fillContourLeft, ms UpToArea no stop, ms UpToArea half of the area, ms
UV sphere 0 22.3 55.8 18.8
ico sphere 0 26.6 67.4 34.3
packed ico sphere 0 16.1 34.1 16.6

🤖 Generated with Claude Code

Fedr and others added 2 commits December 4, 2026 23:33
…ToArea

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@Grantim Grantim left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the new traversal against master edge by edge, and on a model of both algorithms with ~12K random contours, including degenerate ones (edges passed both ways, boundary edges without a left face, faces with 2-3 contour edges). I found no input where fillContourLeft returns a different set than before. The comments below are about the stop case of fillContourLeftUpToArea, the contour passes, and tests.

Also: the benchmark tables were measured with std::vector fronts, and the description says MR::Vector costs 5-15% under MSVC, so the 1.15x in the title may be about 1.0-1.1x at the current commit. Could you re-measure the final commit?

Comment thread source/MRMesh/MRFillContour.cpp Resolved
Comment thread source/MRMesh/MRFillContour.cpp Resolved
Comment thread source/MRMesh/MRFillContour.cpp Resolved
Comment thread source/MRMesh/MRFillContour.h Resolved
Comment thread source/MRTest/MRFillContourTests.cpp Resolved
…filled contour faces; degenerate-case tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Fedr Fedr changed the title fillContourLeft: 1.15-3.7x faster; fillContourLeftUpToArea stops at area limit fillContourLeft: up to 2.8x faster on meshes without regular face order; fillContourLeftUpToArea stops at area limit Dec 5, 2026

@Grantim Grantim left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All points from the previous round are addressed, and I found no input where fillContourLeft returns a different set than master: I checked edge by edge, and on a model of both algorithms that now also matches master's fill order. Two notes on the description, not blocking:

  • The second table gives the UV sphere's fillContourLeft as 26.1 ms, while the main table has 23.1 ms for the same case and compiler; 26.1 looks like the ico sphere's value.
  • The 0.90x clang-cl row (UV sphere, z > -0.99) isn't explained by the code: z > 0 on the same mesh does the same work per face and gives 1.03x, and the 23.1 / 26.1 difference suggests the spread is of the same size. A regular grid mesh (like those from distance maps) would show whether meshes already in fill order really get slower.

Comment thread source/MRMesh/MRFillContour.cpp Resolved
…efetched, unfill uncounted faces on stop

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Fedr

Fedr commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! Both notes on the description:

  • The 26.1 ms was a slip; the second table is now from a run at 7a17734, where the UV sphere's fillContourLeft is 22.3 ms.
  • Agreed, the 0.90x is noise, and the description now says so in a footnote: on the UV sphere z > -0.99, a single variant moves by ±15% from process to process. Over 12 interleaved processes of each, master took 44.0 / 50.7 / 56.7 ms (min / avg / max) and this PR 40.7 / 51.6 / 62.1 ms.

A regular grid, as you suggested: makeRegularGridMesh( 2001, 2001 ) (8M triangles), the same two loops copied into one clang-cl benchmark, 12 interleaved processes of each, min / avg / max in ms:

region master this PR
half of the grid 96.7 / 106.7 / 131.0 45.6 / 47.5 / 50.8
all but a 0.5% strip 153.8 / 174.9 / 190.2 73.3 / 78.4 / 82.6

So the grid gets about 2.2x faster, not slower. A front crossing the grid's rows touches a new row per edge, so this order isn't fill order for the hardware prefetcher, unlike the UV sphere's rings.

🤖 Addressed by Claude Code

@Fedr
Fedr merged commit a0cfb9e into master Dec 5, 2026
63 checks passed
@Fedr
Fedr deleted the fill-contour-left-faster branch December 5, 2026 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants