Skip to content

Quadratic time on a line of *a_ #1154

Closed

Description

@HardMax71

A long line of repeated *a_ parses in quadratic time. The mirrored _a* is linear:

$ python3 -c "print('*a_' * 400000, end='')" > star.md   # 1.2 MB
$ time cargo run --release < star.md > /dev/null
real    0m26.8s
$ python3 -c "print('_a*' * 400000, end='')" > under.md
$ time cargo run --release < under.md > /dev/null
real    0m0.06s

On main (c574292), release build, *a_ at 30, 60, 120, 240 and 480 KB takes 0.016, 0.054, 0.24, 0.93 and 3.7 s, while _a* stays under 0.04 s.

Every _ in that line can close but not open, and finds no _ opener, so find_match calls set_lowerbound. For an underscore that can't open, that raises only UNDERSCORE_NOT_BOTH. get_lowerbound for the next one takes the min of that slot and UNDERSCORE_BASE + count % 3, which is still 0, so each _ rescans the whole stack of * openers. Asterisks raise both slots. The difference came in with 71cf581, which added the % 3 slots for underscores.

This is a different pattern from #1076. The dos-fuzzer doesn't flag it: at its 32 KB size the line parses in about 2.7 ms and still looks linear.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions