You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ParallelQuery batchsize cannot adjust for query size #328
Generally it is fastest to make the batchsize as large as possible, and the effective limit is on the total byte size of the encoded query. For example, image uploads need smaller batchsize than descriptor uploads. It would be useful if we could make the batchsize adjust automatically to keep the encoded query under a specific byte limit.
Summary:
The ParallelQuery execution currently relies on a fixed, number-based batchsize, which can be suboptimal because queries vary significantly in byte size (e.g., large images vs. small descriptors). We need a mechanism to dynamically adjust batch sizes so that the total byte size of the encoded commands and blobs remains within a specified limit.
Implementation Approaches:
Dynamic byte-size evaluation in ParallelQuery.worker():
Instead of pre-calculating a fixed number of batches via (end - start) // self.batchsize in aperturedb/ParallelQuery.py (around line 241), we iterate through the generator and dynamically accumulate items. We commit a batch when either the item count reaches batchsize or the combined size of the serialized query JSON and blobs exceeds a new parameter (e.g., max_batchsize_bytes).
Pros: Tackles the problem accurately at the execution layer. Keeps changes localized to ParallelQuery where knowledge of 'blobs' and 'commands' exists.
Cons: Estimating byte sizes might add slight CPU overhead before sending. Needs care to handle lazy-loaded blobs correctly without loading them prematurely multiple times.
Add an estimator to Parallelizer.batched_run():
Modify aperturedb/Parallelizer.py's worker to accept a generic max_bytes limit and a size_estimator function provided by the child class. The worker advances the batch_end pointer based on the estimator rather than a fixed step.
Pros: Provides a generic solution for dynamic chunking that any Parallelizer subclass could theoretically use. Separates parallel scheduling from the query specifics.
Cons: More invasive to the generic Parallelizer base class. Requires changes to how total_actions and progress bars are tracked if batches vary drastically.
Pre-grouping via a Generator Wrapper:
Instead of changing the parallel execution logic, we create a wrapper around generator (like ByteLimitedGeneratorWrapper). The wrapper pulls items until reaching the byte limit and yields them as one unified item to ParallelQuery (which would then run with batchsize=1).
Pros: Zero changes needed in the complex threading logic of Parallelizer.py or ParallelQuery.py.
Cons: Could break the elements_per_thread division logic in Parallelizer if the wrapper hides the true item count, potentially leading to uneven thread distribution.
Recommended Path Forward:
I recommend Approach 1, modifying ParallelQuery.worker().
Because ParallelQuery already overrides the worker() method from Parallelizer, it is the most appropriate place to introduce ApertureDB-specific byte-counting logic (summing JSON command length and blob bytes). We can introduce a max_batchsize_bytes parameter alongside the existing batchsize parameter (which will act as an upper bound on item count), allowing batches to dynamically shrink when queries are large, while maximizing throughput.
Activity
ad-claw000 commentedon May 26, 2026
Summary:
The
ParallelQueryexecution currently relies on a fixed, number-basedbatchsize, which can be suboptimal because queries vary significantly in byte size (e.g., large images vs. small descriptors). We need a mechanism to dynamically adjust batch sizes so that the total byte size of the encoded commands and blobs remains within a specified limit.Implementation Approaches:
Dynamic byte-size evaluation in
ParallelQuery.worker():Instead of pre-calculating a fixed number of batches via
(end - start) // self.batchsizeinaperturedb/ParallelQuery.py(around line 241), we iterate through thegeneratorand dynamically accumulate items. We commit a batch when either the item count reachesbatchsizeor the combined size of the serialized query JSON and blobs exceeds a new parameter (e.g.,max_batchsize_bytes).ParallelQuerywhere knowledge of 'blobs' and 'commands' exists.Add an estimator to
Parallelizer.batched_run():Modify
aperturedb/Parallelizer.py'sworkerto accept a genericmax_byteslimit and asize_estimatorfunction provided by the child class. The worker advances thebatch_endpointer based on the estimator rather than a fixed step.Parallelizersubclass could theoretically use. Separates parallel scheduling from the query specifics.Parallelizerbase class. Requires changes to howtotal_actionsand progress bars are tracked if batches vary drastically.Pre-grouping via a Generator Wrapper:
Instead of changing the parallel execution logic, we create a wrapper around
generator(likeByteLimitedGeneratorWrapper). The wrapper pulls items until reaching the byte limit and yields them as one unified item toParallelQuery(which would then run withbatchsize=1).Parallelizer.pyorParallelQuery.py.elements_per_threaddivision logic inParallelizerif the wrapper hides the true item count, potentially leading to uneven thread distribution.Recommended Path Forward:
I recommend Approach 1, modifying
ParallelQuery.worker().Because
ParallelQueryalready overrides theworker()method fromParallelizer, it is the most appropriate place to introduce ApertureDB-specific byte-counting logic (summing JSON command length and blob bytes). We can introduce amax_batchsize_bytesparameter alongside the existingbatchsizeparameter (which will act as an upper bound on item count), allowing batches to dynamically shrink when queries are large, while maximizing throughput.