Module

profiling

Patitas ParseAccumulator — opt-in profiling for Markdown parsing.

This module provides accumulated metrics during parsing:

  • Total parse time
  • Source length
  • Node count in resulting AST

Zero overhead when disabled (get_parse_accumulator() returns None).

Example:

from patitas import parse
from patitas.profiling import profiled_parse

# Normal parse (no overhead)
doc = parse("# Hello")

# Profiled parse (opt-in)
with profiled_parse() as metrics:
    doc = parse("# Hello **World**")

print(metrics.summary())
# {"total_ms": 1.2, "source_length": 18, "node_count": 4}

Classes

ParseAccumulator 7
Accumulated metrics during Markdown parsing. Opt-in profiling for debugging slow parsing. Zero ove…

Accumulated metrics during Markdown parsing.

Opt-in profiling for debugging slow parsing. Zero overhead when disabled (get_parse_accumulator() returns None).

Attributes

Name Type Description
start_time float

Parse start timestamp.

source_length int

Length of source being parsed.

node_count int

Number of AST nodes produced.

parse_calls int

Number of parse() calls recorded.

Methods

total_duration_ms 0 float
Total profiling duration in milliseconds.
property
def total_duration_ms(self) -> float
Returns
float
record_parse 2
Record a parse call.
def record_parse(self, source_length: int, node_count: int) -> None
Parameters
Name Type Description
source_length

Length of the source string parsed.

node_count

Number of AST nodes in the result.

summary 0 dict[str, Any]
Get summary of parse metrics.
def summary(self) -> dict[str, Any]
Returns
dict[str, Any] Dict with total_ms, source_length, node_count, parse_calls.

Functions

get_parse_accumulator 0 ParseAccumulator | None
Get current accumulator (None if profiling disabled).
def get_parse_accumulator() -> ParseAccumulator | None
Returns
ParseAccumulator | None
profiled_parse 0 Iterator[ParseAccumulato…
Context manager for profiled parsing. Creates a ParseAccumulator and makes it …
def profiled_parse() -> Iterator[ParseAccumulator]

Context manager for profiled parsing.

Creates a ParseAccumulator and makes it available via get_parse_accumulator() for the duration of the with block.

Returns
Iterator[ParseAccumulator]