Skip to content

perf(answer): Cache schema parsers for table answers - #1013

Merged
dhalperi merged 1 commit into
batfish:masterfrom
progwriter:perf/answer-frame-parsing
Nov 19, 2026
Merged

dhalperi merged 1 commit into
batfish:masterfrom
progwriter:perf/answer-frame-parsing

Conversation

@progwriter

Copy link
Copy Markdown
Contributor

Speed up TableAnswer parsing

Building a TableAnswer used to run two regexes and a long if chain on
every cell to work out how to parse it. Now:

  • The check for whether a schema is a List/Set is cached (functools.cache).
    This was where most of the time went.
  • Simple schemas like Flow or Integer go through a dict of parsers instead
    of the if chain.
  • _rows_to_frame builds the DataFrame straight from per-column lists.
    Previously it built rows, transposed them, then reindexed.

Performance

Constructing a TableAnswer with 20k rows and 8 columns of mixed schemas
(Node, Interface, Ip, Integer, List, Flow, String, SelfDescribing):

Time
Before 0.298s
After 0.094s (~3.2x faster)

Behavior change

Empty tables (no rows) now have object dtype columns instead of float64.
The old float64 came from the reindex step adding the missing columns as
NaN. object is what non-empty tables already use: answer columns are never
numeric. Code that depended on empty frames being float64 would see a
difference. test_table_answer_element_deser_no_rows now asserts the new
dtype.

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

  • Unit tests pass, including a new test_parse_iterable_schema for nested
    lists, None elements and the error on non-list values.
  • tests/integration (127 passed, 14 skipped), docs (61 passed) and the
    doctests (2 passed) pass against Batfish built from HEAD. The 14 skips are
    notebook output comparisons, which only run on Python 3.14+.
  • ruff and mypy are clean on the changed files.

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.
@batfish-bot

Copy link
Copy Markdown

This change is Reviewable

@dhalperi
dhalperi enabled auto-merge (squash) November 19, 2026 01:52

@dhalperi dhalperi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dhalperi reviewed 4 files and all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on progwriter).

@dhalperi
dhalperi merged commit 9a5f109 into batfish:master Nov 19, 2026
18 checks passed
@progwriter
progwriter deleted the perf/answer-frame-parsing branch November 21, 2026 17:07
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