(improvement) cache namedtuple class in named_tuple_factory to avoid repeated exec() calls#738
Closed
mykaul wants to merge 1 commit into
Closed
(improvement) cache namedtuple class in named_tuple_factory to avoid repeated exec() calls#738mykaul wants to merge 1 commit into
mykaul wants to merge 1 commit into
Conversation
…repeated exec() calls Cache the Row namedtuple class keyed on column names so that repeated queries with the same schema skip the expensive namedtuple() call (which internally uses exec()). This reduces per-result-set overhead by 3-5x for typical column counts, and up to 157x for single-row results where class creation dominates.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Rownamedtuple class innamed_tuple_factory()keyed ontuple(colnames), so Python'snamedtuple()(which internally callsexec()) is only invoked once per unique column schemaMotivation
named_tuple_factoryis the defaultrow_factoryin the driver. Every call tonamedtuple('Row', columns)internally callsexec()to generate a new class — this is surprisingly expensive. For prepared statements executing the same query repeatedly, the column names never change, yet we pay thenamedtuple()+exec()cost on every result set.Benchmark results
100-row result sets (5 columns):
100-row result sets (20 columns):
Single-row result (5 columns, isolates cache overhead):
The single-row case shows the true per-call savings most clearly: 415 ns (dict lookup) vs 65 us (
exec()-based namedtuple creation).Design notes
dictkeyed ontuple(colnames)(the raw column names before cleaning)SyntaxError,Exception) are preserved — onSyntaxErrorthe function falls back topseudo_namedtuple_factorywithout caching; on other exceptions, sanitized identifiers are used and the result is cachedTests
All existing unit tests pass (16 row factory + resultset tests, 30 cluster tests).