Skip to content

Mask tracker: keep only the cropped previous frame - #2337

Merged
bflatastic merged 1 commit into
storytold:mainfrom
vangel76:trkx-mask-crop-frames
Oct 29, 2026
Merged

bflatastic merged 1 commit into
storytold:mainfrom
vangel76:trkx-mask-crop-frames

Conversation

@vangel76

@vangel76 vangel76 commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

What

MaskTracker stores the previous frame already cropped to the region the next step reads, instead of cloning the whole frame.

Why

step cloned the full RGBA frame on every call (33 MB at 1080p, 133 MB at 4K) and then cropped that clone; only the area around the outline is read. The engine already passes cached Arc<Image> frames, so the clone was pure overhead (allocation, page faults, memory bandwidth).

How

At the end of step (and in new) the new frame is cropped with crop_region to the region of the updated outline (outline bounds plus the same margin). The next step uses that crop and its offset directly, so it no longer re-crops the previous frame. crop_region is the former inline crop closure with the same rounding, and the crop of the current frame uses it too, so both crops are the pixels they were before. Outline updates only on success, as before, so a failed step keeps the region.

MaskTracker::outline is a public field; the stored crop follows the outline the tracker itself produced (the engine never edits it between steps).

The optional rect-luma helper from the issue is not part of this PR; the crop is a single copy of the region.

Verification

  • Bench output identical before and after: hash over all 64 outline vertices' to_bits (1080p fef233e1b21e813c, 4K 5db43a434a6121f4) and with non-zero frame offsets (37.5,-12.25 and -100,80, 12 steps each: e179d36236686119, d3a7715bf4575004).
  • New test follows_the_disk_through_a_frame_offset covers tracking with a non-zero layer offset.
  • cargo fmt --all --check, cargo clippy -p effectcraft-track --all-targets -- -D warnings, cargo test --release -p effectcraft-track pass; cargo check -p effectcraft-engine passes.

Numbers

One thread, synthetic footage, base and branch alternated, 3 rounds. Instruction counts barely change (the saving is memory traffic and page faults), so wall time is the figure here; instructions and cycles include frame generation.

Base This PR
1080p, 39 steps, work time 1.61 s (1.605-1.618) 1.53 s (1.526-1.541)
1080p cycles 13.3 G 13.0 G
4K, 19 steps, work time 3.42 s (3.41-3.44) 2.19 s (2.14-2.39)
4K cycles 20.1 G 18.7-19.0 G

Fixes #2320

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved mask tracking across frames with nonzero offsets and different crop sizes.
    • Mask outlines and movement now update only when tracking succeeds, reducing incorrect changes after unsuccessful tracking steps.
    • Very small image crops are rejected, helping prevent unreliable tracking near image boundaries.

step() cloned the whole RGBA frame to remember it (33 MB at 1080p,
133 MB at 4K) and then cropped that clone, although only the region
around the outline is ever read. The tracker now stores the previous
frame already cropped to the region the next step will use, computed
from the updated outline with the same margin and rounding as before,
so the crops, and therefore the tracking result, are unchanged.

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

coderabbitai Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 9919048 and 1c87133.

Files selected for processing: 1
  • crates/track/src/mask.rs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.

Walkthrough

MaskTracker now stores a crop around the mask with its layer offset instead of retaining the full previous frame. Tracking uses cropped regions, updates the outline and velocity only after success, and refreshes the stored crop after each step.

Changes: Mask tracking crops

Layer / File(s) Summary
Crop-based mask tracking
crates/track/src/mask.rs
MaskTracker calculates a region around the mask, clips it to image bounds, and rejects crops that are too small. It stores the previous crop with its layer offset and uses crops during tracking. A new test checks tracking across frames with a nonzero frame offset.

Priority: Low

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Refactor

Possibly related PRs

Suggested reviewers: echelon

Merge Risk: Minimal · up to 1c871

No identified issue needs resolution before merging, subject to normal checks.

Security architecture risk: Low · up to 1c871

The change does not introduce permissions or external access in the inspected workflow. It does change recovery behavior when a usable crop cannot be produced, although the application already stops tracking on failure.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The demonstrated behavioral impact is confined to tracking results and recovery for a tracker instance. The inspected consumer uses motion to update project mask paths, not authorization or credential state; no broader privilege or cross-service exposure was identified along this path.

Trust Boundaries and Controls

  • observed — The engine continues to own frame progression and project-path updates. Tracking receives borrowed frames and returns motion; the crop-storage change does not transfer project-write authority into the tracking library.
Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 62.50% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: storing only the cropped previous frame in the mask tracker.
Linked Issues check Passed Issue [#2320] requires avoiding the full-frame clone by storing a crop for the next tracking step. crates/track/src/mask.rs adds crop_region, uses it in MaskTracker::new, and refreshes the crop …
Out of Scope Changes check Passed The changes are limited to cropped previous-frame storage, crop-coordinate handling, and a focused tracking test in crates/track/src/mask.rs. These changes directly support issue [#2320]. No unrelat…
Full details: Linked Issues check

Explanation

Issue [#2320] requires avoiding the full-frame clone by storing a crop for the next tracking step. crates/track/src/mask.rs adds crop_region, uses it in MaskTracker::new, and refreshes the crop after each step. The crop preserves the prior rounding, margin, frame offset, and minimum-size checks. The new offset test covers the changed coordinate path. The optional rect-luma optimization is not required by [#2320] and remains outside the stated objective.

Full details: Out of Scope Changes check

Explanation

The changes are limited to cropped previous-frame storage, crop-coordinate handling, and a focused tracking test in crates/track/src/mask.rs. These changes directly support issue [#2320]. No unrelated production behavior or unrelated files are shown in the reviewed changes.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@vangel76

vangel76 commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor Author

This PR is one of 17 from a speed audit of crates/track and its callers (issues #2317-#2330, #2322, #2349). Overview of what each one measured, so the effect is visible in one place:

PR Area Measured (single thread, before -> after) Output
#2332 Subspace Warp plan() 22.3 G -> 4.8 G instructions (4.7x, Automatic); 33.7 G -> 5.2 G (6.5x, Enhanced); 24x on clean low-rank trajectories rounding level, at most 2.7e-6 px
#2331 KLT lk_point (Warp Stabilizer analysis, mask tracking) analysis 3.84 s -> 1.44 s (2.6x), mask tracking 1.57 s -> 0.95 s bit-identical
#2339 Point tracker (NCC taps, gradient window) 54.6 G -> 31.0 G instructions (1.76x) bit-identical
#2334 Face tracker polygon test 12.5 G -> 2.2 G instructions (5.7x) bit-identical
#2337 Mask tracker frame clone 4K: 3.42 s -> 2.19 s (-36 %); 1080p -5 % identical outline
#2335 Roto Brush propagate band crop 208.5 G -> 172.0 G instructions per 20 frames (-17.5 %) bit-identical
#2333 Roto Brush distance transform 3.04 G -> 2.80 G instructions (-8 %) bit-identical
#2346 Camera solve, long tracks (matrix-free Schur) 44.6 G -> 4.2 G instructions (10.6x), timed loop 1966 ms -> 216 ms practically identical (reprojection < 1e-9 px), direct fallback
#2342 Camera solve, short tracks (band Cholesky) 8.34 G -> 3.40 G instructions (2.46x) bit-identical
#2338 triangulate allocations 2.38 G -> 1.32 G instructions (1.81x) bit-identical
#2340 BA Jacobian on the stack 13.4 G -> 12.6 G instructions (-6.2 %) bit-identical
#2345 null_vector without unused SVD output -0.96 % instructions bit-identical
#2351 Point tracker, 3x3 refine when the motion was small (stacked on #2339) another 31.0 G -> 20.2 G instructions (1.54x) on top of #2339 not bit-identical by design, accuracy unchanged in all 8 ground-truth cases
#2348 Warp Stabilizer / Camera Tracker analysis lookup per render 0.73 ms -> 0.014 ms per call for 300 frames (about 50x) identical
#2347 Warp Stabilizer default plan computed twice first render 2313 ms -> 1156 ms (-50 % instructions) identical pixels
#2350 Warp analysis shares one luma pyramid 87.7 G -> 81.8 G instructions (-6.8 %) identical analysis JSON
#2341 Input validation (panics, NaN, allocation budget) correctness fix, +6.1 % instructions on valid input valid input unchanged

Notes:

@bflatastic

Copy link
Copy Markdown
Member

Merged, thank you @vangel76! It landed on main with a batch of community PRs, after a full workspace build and test run on Windows against the refactored main (fmt, strict clippy, about 4,100 tests, layers, assets and wasm).

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.

track: Mask tracker clones the whole frame on every step (-36 % at 4K)

2 participants