Repository navigation
Conversation
Optimize document retrieval by checking ID presence directly in the read table.
|
Hey @av27-lgtm, that's an interesting optimization, but it doesn't reproduce with actual file based storages like JSONStorage. I get 1.01x speedup for There's also a behavior change: when Given that, I'll close the PR unless a significant speedup can be shown for file-based storages and the Cheers! |
|
Hi Markus (@msiemens), Thank you for the detailed feedback and for taking the time to benchmark this against You are completely right:
Thanks again for the constructive review and for maintaining TinyDB! |
Summary
Optimizes$O(N)$ full-table scan to direct $O(K \log K)$ key lookups, and makes
Table.get(doc_ids=...)from anTable.contains(doc_id=...)zero-alloc.Problem
table.get(doc_ids=[...])with a small subset of IDs on a table containing 10,000+ documents, TinyDB currently iterates over every document intable.items(), yieldingtable.contains(doc_id=doc_id)delegated toself.get(doc_id=doc_id)which unnecessarily instantiates aDocumentmapping object in memory just to test for existence.Solution
len(doc_ids_set) < len(table), retrieve only existing keys directly viacontains(doc_id=doc_id), check presence directly viastr(doc_id) in self._read_table().Empirical Benchmarks (10,000 items in table)
table.get(doc_ids=[7 IDs]): ~1,200 ops/sec -> ~141,000 ops/sec (~116x speedup)table.contains(doc_id=doc_id): 586k ops/sec -> 2.24M ops/sec (~3.8x speedup, 0 bytes allocated)