Skip to content

Stream voters in compute_tally to avoid loading every ballot into memory - #568

Closed
vndmtrx wants to merge 1 commit into
benadida:masterfrom
helios-ptbr:fix/tally-iterator
Closed

vndmtrx wants to merge 1 commit into
benadida:masterfrom
helios-ptbr:fix/tally-iterator

Conversation

@vndmtrx

@vndmtrx vndmtrx commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Election.compute_tally iterates self.voter_set.exclude(vote=None). Django loads the whole queryset (every voter and its encrypted ballot) before the loop starts, so memory grows linearly with the number of voters. This adds .iterator(chunk_size=500). The tally is identical.

Growth rate

Peak RSS of the process running the tally loop (3 questions, 5 options each, min 0, max 1):

  • Before: about 0.19 MiB per voter, linear (0.19 MiB per voter at 10k, 20k, 30k and 50k voters). 50,000 voters need about 9.5 GiB.
  • After: constant, about 174 MiB, from 1,000 to 50,000 voters.
  • Ballot size matters. With 11 questions and 114 options in total, 10,000 voters took 15.5 GiB before and 923 MiB after.

Comparison

Voters Peak RSS before Peak RSS after Time before Time after Tally
1,000 260 MiB 175 MiB 1.39 s 1.44 s identical
10,000 1,955 MiB 174 MiB 15.0 s 14.7 s identical
50,000 9,492 MiB 175 MiB 79.8 s 78.2 s identical

Time differs by -3% to +4%, within run-to-run noise. The tally is identical in all 48 scenarios (10 to 50,000 voters, 1 to 5 questions, min 0 or 1, max 1 or all, fixed or random order, 1 to 10 trustees, 1 to 10 admins).

How to reproduce

  1. Create an election with N voters who all have a stored ballot (voter.vote). Ballots may repeat, since the cost depends on ballot size and count, not on being distinct.
  2. In a fresh process (manage.py shell), run the loop of compute_tally with and without .iterator(chunk_size=500), without saving.
  3. Read the peak with resource.getrusage(resource.RUSAGE_SELF).ru_maxrss (KiB on Linux) and compare a hash of the resulting tally.

Setup used: Docker, PostgreSQL 16, one web process, 20 CPUs, 31 GiB RAM, nothing else running.

Tests

ComputeTallyTests in helios/tests.py:

  • test_compute_tally_matches_loading_all_voters: the tally from compute_tally equals the tally built by loading all voters first (same num_tallied, same ciphertexts).
  • test_compute_tally_streams_voters: voters are read with iterator(chunk_size=...). It fails without the change.

Full suite: 339 tests OK.

vndmtrx added a commit to helios-ptbr/helios-server that referenced this pull request Nov 19, 2026
@laoumh

laoumh commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

We made this fix on our version of the system here at Unicamp. On a large election - 100 candidates ballot, 7.5k voters - getting all the ballots at once meant +4GB of memory. It was exhausting the VM resources, entering into swap regime, slowing tally time considerably.

We used chunk_size=100, but 500 seems plenty reasonable (would be about 250MB per fetch on our largest case, very acceptable).

@benadida

Copy link
Copy Markdown
Owner

thank you for this!

@benadida

Copy link
Copy Markdown
Owner

I'm going to replace this with #573 which has your commit + an atomic() fix for cases where pgbouncer is used.

@benadida benadida closed this Nov 29, 2026
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.

3 participants