mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 02:36:54 +01:00
`many_unmatched_lt_is_fast` asserted that a parse of 256,000 `<` and one
`>` took under 2 s, and `unclosed_angle_brackets_are_escaped_in_one_pass`
asserted under 5 s for 200,000 `<`. On a loaded machine the first took
2.01 s and failed, and 0.10 s when run again.
The TTML writer's tag parser now reads the caption text through a small
`Reader` type in a child module of `ttml.rs`. Its only accessors are
`byte(index)` and `suffix(start)`, an iterator that reads each byte
through `byte`. In test builds `byte` counts its calls. `parse_tag` and
`QuoteCache::next` take a `Reader` in place of a `&[u8]`, and the tag
name is built from the bytes as they are read. Parsing behavior and
output do not change.
The two unit tests write each text at 50,000 and 100,000 characters and
assert at most three byte reads per character of text, and that twice
the text costs less than three times as many reads. With a search for
`>` after every `<` put back in `parse_tag` through `suffix` or through
repeated `byte` calls, both fail at the first assertion: 5,000,050,000
byte reads in a text of 100,000, and 5,000,250,000 in a text of 100,001.
The same search written against the slice (`bytes.get(i..)`) does not
compile, because `Reader` has no `get` method and its field is private.
The count covers only reads through `Reader`. `inline` still holds the
caption text as a `&str`, and a search over it is not counted: with
`rest[1..].find('>')` added to the check for `<` in `inline`,
`many_unmatched_angle_brackets_before_one_gt_are_read_once` passes. A
change that hands the parser the raw slice again, through a new accessor
or parameter, is not counted either.
The integration test keeps its output check as `many_unmatched_lt_stay_text`.