Replace SemaphoreSlim with Parallel.ForEachAsync, rename MaxConcurrentPageFetches#47
Merged
Conversation
Replaces the manual SemaphoreSlim + Task.WhenAll fan-out with Parallel.ForEachAsync, consolidating three per-format fetch methods into a single FetchPageAsync. Results are written to a pre-allocated array to preserve canvas order for TextBuilder. Renames MaxConcurrentAltoFetches → MaxConcurrentPageFetches since the limit applies to all text formats (ALTO, VTT, AnnotationPage), not just ALTO. Updates docs, decisions log, and adds a Builder API configuration reference table to the README. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
9ef8d8b to
9c0c4dc
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SemaphoreSlim+Task.WhenAllconcurrency pattern inTextBuildJob.ProcessPagesAsyncwithParallel.ForEachAsyncFetchXmlWithSemaphoreAsync,FetchVttWithSemaphoreAsync,FetchAnnotationPageWithSemaphoreAsync) into a singleFetchPageAsyncMaxConcurrentAltoFetches→MaxConcurrentPageFetchessince the limit applies to all text formats (ALTO, VTT, AnnotationPage), not just ALTOREADME.mdWhy Parallel.ForEachAsync over SemaphoreSlim
Less boilerplate — the concurrency limit is a single
ParallelOptionsproperty rather than aSemaphoreSlimthat must be created, passed into every method, and released in afinallyblock.Lazy task scheduling —
Parallel.ForEachAsynconly starts a new task when a slot is free, so it never allocates more thanMaxDegreeOfParallelismconcurrent tasks at once. The previous approach created aTaskobject for every page upfront (all queued on the semaphore), which wastes memory on large manifests.Clearer intent — "process this collection with bounded parallelism" is exactly what
Parallel.ForEachAsyncwas designed to express. A semaphore is a lower-level primitive that requires the reader to mentally reconstruct that intent.What is unchanged
MaxConcurrentPageFetchesdefault remains 8; behaviour is identicalShutdownTokenTextBuilderstill receives canvases in original canvas sequence🤖 Generated with Claude Code