Skip to content

Improve package import performance - #1671

Open
refi64 wants to merge 2 commits into
aptly-dev:masterfrom
refi64:import-perf
Open

refi64 wants to merge 2 commits into
aptly-dev:masterfrom
refi64:import-perf

Conversation

@refi64

@refi64 refi64 commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Description of the Change

This is a set of two optimizations to package importing:

  • Avoiding creating a one-off transaction for every single package imported (since goleveldb transactions are rather expensive).
  • Computing checksums in parallel, with largest first to improve utilization.

The individual commits have further descriptions of the changes.

In a test where I took a random sample of 2k packages from the apertis target component, the import time across these two changes from ~1m48s to ~28s, nearly 4x faster.

Checklist

  • allow Maintainers to edit PR (rebase, run coverage, help with tests, ...)
  • unit-test added (if change is algorithm)
  • functional test added/updated (if change is functional)
  • man page updated (if applicable)
  • bash completion updated (if applicable)
  • documentation updated
  • author name in AUTHORS

Opening and closing transactions constantly is rather expensive, and
transaction semantics don't actually matter for bare Update() calls, so
just use a more efficient Batch there instead. In a test of importing
2k packages, this cut the runtime from ~1m48s to ~1m6s, an improvement
of nearly 40%.
When importing a large number of files, most of the time can just end up
spent computing checksums, as all of MD5/SHA1/SHA256/SHA512 are computed
in sequential order. However, parallelizing the hashing algorithms
themselves is difficult, because they all take very different lengths of
time, and trying an inner layer of parallelism if the caller is already
using its own parallelism won't actually make anything faster.

Instead, we can attack this from multiple angles:

- The process of computing the checksums as a whole can be run in
  parallel across different files.
  - Since the checksums themselves still run in sequence, processing
    large files towards the front gives better performance, because
    if they end up towards the end, then the large ones will be stuck
    continuing to be processed while the other goroutines sit idle.
- ChecksumsForReader and its callers can now optionally take a custom
  buffer, allowing the use of a larger read buffer than the 32KiB
  io.Copy default.
  - This can't be created in ChecksumsForReader itself, because then Go
    will spill the buffer to the heap on every invocation, whereas the
    caller can create the buffer once and reuse it for all invocations.

To avoid a confusing idle period before any progress is printed, an
additional progress bar is shown with the checksum progress.

In a test of importing 2k packages, this cut the runtime from ~1m6s to
~28s, an improvement of over 56%.
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