Skip to content

perf(sync): skip mark-and-sweep phases on cold m3u first import - #385

Open
Kimlimp wants to merge 1 commit into
bilipp:mainfrom
Kimlimp:perf/m3u-first-import-prune
Open

Kimlimp wants to merge 1 commit into
bilipp:mainfrom
Kimlimp:perf/m3u-first-import-prune

Conversation

@Kimlimp

@Kimlimp Kimlimp commented Nov 6, 2026 •

Copy link
Copy Markdown

Summary

Eliminate unnecessary mark-and-sweep phases on a genuine first M3U import into an empty store, while preserving interrupted-import retry recovery and normal refresh pruning.

Root Cause

During a cold M3U import into an empty store, every row written was freshly inserted. Running 5 full mark-and-sweep phases (pruning live streams, movies, series, episodes, categories) walked every newly inserted record against seen ID sets only to delete 0 rows, consuming significant time and memory holding seen-sets until the end of the last sweep.

Changes

  1. Pre-import emptiness detection (ContentSyncManager+Prune.swift): Added playlistHasExistingCatalogRows(playlistId:) with bounded fetchLimit: 1 across LiveStream, Movie, Series, and Episode. This avoids relying on playlist.lastSyncDate, which is nil even after an interrupted/partial import.
  2. State propagation (ContentSyncManager+M3UImportState.swift & +M3UFields.swift): Seeded state.hadExistingContent = !state.knownCategories.isEmpty || playlistHasExistingCatalogRows(playlistId: playlistId) before writes begin.
  3. Prune bypass (ContentSyncManager+M3UPrune.swift): Guarded pruneStaleM3URows(playlistId:state:) on state.hadExistingContent. When false, immediately clears seen-sets and returns without running sweeps.
  4. Tests (ContentSyncPruneTests.swift & M3USyncTests.swift):
    • Unit tests verifying emptiness check isolation, skip behavior, and reimport pruning.
    • Integration test verifying that an interrupted import with committed rows followed by a shorter retry properly executes sweeps and prunes stale rows.
    • Benchmark test testReimportProviderShapedPlaylist in M3UColdImportBenchmarks.swift.

Validation

  • ContentSyncPruneTests: 5/5 passed (0 failures).
  • M3USyncTests: 12/12 passed (0 failures).
  • 50k fixture benchmark: monotonic clock reduced from 14.175 s to 12.671 s (-1.504 s / ~11% reduction), with all prune signpost phases completely skipped.

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.

1 participant