Repository navigation
perf(answer): Cache schema parsers for table answers - #1013
Merged
Merged
Conversation
Parsing a TableAnswer re-matched each cell's schema string against two regexes and a chain of string comparisons. Resolve each schema to a parser function once (lru_cache) and build the DataFrame column by column instead of row-wise plus transpose and reindex. About 3.5x faster on a 20k-row, 8-column mixed-schema answer (0.298s -> 0.084s). Empty tables now get object-dtype columns instead of float64, matching the existing intent that no answer column is treated as numeric.
dhalperi
enabled auto-merge (squash)
November 19, 2026 01:52
dhalperi
reviewed
Nov 19, 2026
dhalperi
left a comment
Member
There was a problem hiding this comment.
@dhalperi reviewed 4 files and all commit messages.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on progwriter).
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.
Speed up TableAnswer parsing
Building a
TableAnswerused to run two regexes and a longifchain onevery cell to work out how to parse it. Now:
functools.cache).This was where most of the time went.
FloworIntegergo through a dict of parsers insteadof the
ifchain._rows_to_framebuilds the DataFrame straight from per-column lists.Previously it built rows, transposed them, then reindexed.
Performance
Constructing a
TableAnswerwith 20k rows and 8 columns of mixed schemas(Node, Interface, Ip, Integer, List, Flow, String, SelfDescribing):
Behavior change
Empty tables (no rows) now have
objectdtype columns instead offloat64.The old
float64came from the reindex step adding the missing columns asNaN.
objectis what non-empty tables already use: answer columns are nevernumeric. Code that depended on empty frames being
float64would see adifference.
test_table_answer_element_deser_no_rowsnow asserts the newdtype.
Everything else matches the old code: None handling, nested and lowercase
List/Set schemas, SelfDescribing, TraceTree lists, column order, duplicate or
missing columns, and the index.
Testing
test_parse_iterable_schemafor nestedlists, None elements and the error on non-list values.
tests/integration(127 passed, 14 skipped),docs(61 passed) and thedoctests (2 passed) pass against Batfish built from HEAD. The 14 skips are
notebook output comparisons, which only run on Python 3.14+.