Skip to content

fix(core): sets of many elements convert and compare in linear time - #117

Merged
kalaspuff merged 2 commits into
issues-stack/2-contains-element-codecsfrom
issues-stack/3-fast-set-conversion
Oct 31, 2026
Merged

kalaspuff merged 2 commits into
issues-stack/2-contains-element-codecsfrom
issues-stack/3-fast-set-conversion

Conversation

@fctl-sh

@fctl-sh fctl-sh Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

What and why

Writing a large number set stalled the event loop. The binding deduplicated number sets with a linear search for each element (1 and 1.0 are the same DynamoDB number), so converting set(range(60_000)), an item of about 210 KB that DynamoDB accepts, took 3.0 s with the GIL held on the event loop's thread, on both backends; a string set of that size took 8 ms. The core was quadratic in the same way: the duplicate check of set values, set equality (conditions and filters with ==), and the ADD and DELETE of set elements in the in-memory DynamoDB's update evaluation.

Number is canonical and hashable, so all of them now use hash sets. Measured on the in-memory backend with that set: conversion 3.0 s → 13 ms, put 9.6 s → 40 ms, ADD 3.1 s → 17 ms, DELETE 4.6 s → 21 ms, a scan filtering on set equality 2.3 s → 11 ms. Results are unchanged: numbers that are equal in DynamoDB still collapse into one element, and set order still does not matter for equality.

Verified with Rust tests of the duplicate check, equality and ADD/DELETE on sets of 50,000 to 60,000 elements, and a binding test that converts a set of 60,000 Python numbers that are 30,000 DynamoDB numbers (a float and a Decimal of each tenth, which Python keeps apart) into 30,000 elements. make check passes.

Fixes #94

Checklist

  • Tests cover the change (Rust unit tests of the core, binding test)
  • Docs updated (changelog)
  • make check passes locally (lint, types, clippy, consistency, tests)
  • Breaking change? No

fctl session


Note

Low risk. It swaps quadratic duplicate checks for hash-set ones in set conversion, set equality and the in-memory ADD/DELETE, with the same results; Number is already canonical and hashable, so only performance changes.

What it does. Number sets converted from Python, validated, compared and updated (ADD/DELETE) in the in-memory DynamoDB took time quadratic in their size, so a 60,000-element set stalled the event loop for seconds with the GIL held. The binding's number-set deduplication, the core's duplicate check, set equality and the set ADD/DELETE now use hash sets and run in linear time. It adds Rust tests with large sets, a binding test that converts 60,000 Python numbers that are 30,000 DynamoDB numbers, and a changelog entry.

fctl review's first look at dce049a · 5 files, +84 −29 · Review agent settings

The binding deduplicated number sets with Vec::contains for each
element, so set(range(60_000)) took 3 s to convert, with the GIL held
on the event loop's thread. The core's duplicate check of sets, set
equality and the ADD and DELETE of set elements in the in-memory
DynamoDB were quadratic too: a put of that set took 10 s on the memory
backend, an ADD 3 s and a DELETE 4.6 s.

Number is canonical and hashable, so all of them now use hash sets:
13 ms for the conversion, 40 ms for the put, about 20 ms for ADD and
DELETE.

Fixes #94

Co-authored-by: Carl Oscar Aaro <[email protected]>
@fctl-review

This comment has been minimized.

Comment thread tests/unit/test_wire.py Resolved
…amoDB

Python merged 59_999.0 with 59_999 and Decimal(0) with 0 before the
conversion, so the 60,000-element set never reached the deduplication
with a duplicate. Floats and Decimals of the same tenths stay apart in
Python and are one number in DynamoDB.

Co-authored-by: Carl Oscar Aaro <[email protected]>
@kalaspuff
kalaspuff added this pull request to stack #124 October 31, 2026 01:30
@kalaspuff
kalaspuff merged commit fd187b4 into main Oct 31, 2026
23 checks passed
@kalaspuff
kalaspuff deleted the issues-stack/3-fast-set-conversion branch October 31, 2026 01:34
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.

Binding: converting a number set of many elements takes seconds and holds the GIL

1 participant