Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed some cases of quadratic worst-case runtime #2922

Merged
merged 2 commits into from Jun 8, 2021

Conversation

@RunDevelopment
Copy link
Member

@RunDevelopment RunDevelopment commented May 29, 2021

This fixes a few of the cases I described in #2597.

Basically, the regex engine moves the regexes over the string from left to right and is typically forced to check each of the n suffixes for a string of length n. There are various optimizations employed by the regex engine that reduces the number of suffixes that have to be tested but it's usually still linear, O(n). If the regex engine then also takes O(n) steps to reject each of those suffixes, we get a worst-case runtime of O(n^2).

The fixes in this PR also have the same strategy: reject suffixes in O(1). So all rejecting suffixes will be rejected in (on average) O(1).

I have to note that this PR fixes not all of these cases, by far. I only fixed the cases that were easy to fix.


All of the changed regexes still behave the same in the context of Prism's matching algorithm.

The only exception is JS stack trace where I added an additional lookbehind group as part of the fix. Hence the changed test case.

@github-actions
Copy link

@github-actions github-actions bot commented May 29, 2021

JS File Size Changes (gzipped)

A total of 71 files have changed, with a combined diff of +182 B (+0.2%).

file master pull size diff % diff
components/prism-abnf.min.js 484 B 485 B +1 B +0.2%
components/prism-apacheconf.min.js 3.51 KB 3.52 KB +4 B +0.1%
components/prism-aql.min.js 626 B 626 B 0 Bytes 0%
components/prism-autohotkey.min.js 4.13 KB 4.13 KB +3 B +0.1%
components/prism-autoit.min.js 528 B 529 B +1 B +0.2%
components/prism-c.min.js 891 B 896 B +5 B +0.6%
components/prism-clike.min.js 446 B 447 B +1 B +0.2%
components/prism-concurnas.min.js 893 B 894 B +1 B +0.1%
components/prism-cpp.min.js 1.19 KB 1.19 KB 0 Bytes 0%
components/prism-csharp.min.js 2.34 KB 2.34 KB +2 B +0.1%
components/prism-css-extras.min.js 1.45 KB 1.46 KB +4 B +0.3%
components/prism-css.min.js 573 B 597 B +24 B +4.2%
components/prism-dataweave.min.js 507 B 509 B +2 B +0.4%
components/prism-ebnf.min.js 266 B 269 B +3 B +1.1%
components/prism-elixir.min.js 844 B 845 B +1 B +0.1%
components/prism-elm.min.js 580 B 596 B +16 B +2.8%
components/prism-erlang.min.js 453 B 453 B 0 Bytes 0%
components/prism-factor.min.js 3.54 KB 3.55 KB +5 B +0.1%
components/prism-fortran.min.js 797 B 798 B +1 B +0.1%
components/prism-fsharp.min.js 1.11 KB 1.11 KB -2 B -0.2%
components/prism-ftl.min.js 799 B 800 B +1 B +0.1%
components/prism-gdscript.min.js 586 B 586 B 0 Bytes 0%
components/prism-gedcom.min.js 236 B 239 B +3 B +1.3%
components/prism-graphql.min.js 549 B 550 B +1 B +0.2%
components/prism-haskell.min.js 1.5 KB 1.5 KB +2 B +0.1%
components/prism-icon.min.js 607 B 607 B 0 Bytes 0%
components/prism-inform7.min.js 1.49 KB 1.5 KB +2 B +0.1%
components/prism-javadoc.min.js 601 B 610 B +9 B +1.5%
components/prism-javadoclike.min.js 496 B 500 B +4 B +0.8%
components/prism-javascript.min.js 1.48 KB 1.48 KB 0 Bytes 0%
components/prism-jsdoc.min.js 639 B 643 B +4 B +0.6%
components/prism-jsstacktrace.min.js 411 B 412 B +1 B +0.2%
components/prism-julia.min.js 642 B 646 B +4 B +0.6%
components/prism-kotlin.min.js 815 B 820 B +5 B +0.6%
components/prism-markup.min.js 1.03 KB 1.03 KB +4 B +0.4%
components/prism-matlab.min.js 333 B 333 B 0 Bytes 0%
components/prism-mel.min.js 6.71 KB 6.71 KB +2 B +0.0%
components/prism-mizar.min.js 619 B 621 B +2 B +0.3%
components/prism-monkey.min.js 593 B 594 B +1 B +0.2%
components/prism-nand2tetris-hdl.min.js 231 B 231 B 0 Bytes 0%
components/prism-nsis.min.js 2.01 KB 2.02 KB +4 B +0.2%
components/prism-oz.min.js 530 B 531 B +1 B +0.2%
components/prism-parigp.min.js 500 B 502 B +2 B +0.4%
components/prism-pascaligo.min.js 677 B 679 B +2 B +0.3%
components/prism-processing.min.js 381 B 382 B +1 B +0.3%
components/prism-protobuf.min.js 501 B 502 B +1 B +0.2%
components/prism-puppet.min.js 1.08 KB 1.08 KB +2 B +0.2%
components/prism-purebasic.min.js 1.13 KB 1.14 KB +6 B +0.5%
components/prism-purescript.min.js 452 B 455 B +3 B +0.7%
components/prism-python.min.js 1.04 KB 1.04 KB +3 B +0.3%
components/prism-renpy.min.js 2.04 KB 2.04 KB +1 B +0.0%
components/prism-rest.min.js 1.07 KB 1.07 KB 0 Bytes 0%
components/prism-rust.min.js 1.16 KB 1.16 KB +1 B +0.1%
components/prism-sas.min.js 3.02 KB 3.02 KB 0 Bytes 0%
components/prism-sass.min.js 452 B 451 B -1 B -0.2%
components/prism-scss.min.js 631 B 630 B -1 B -0.2%
components/prism-smali.min.js 620 B 624 B +4 B +0.6%
components/prism-splunk-spl.min.js 910 B 910 B 0 Bytes 0%
components/prism-sqf.min.js 11.02 KB 11.02 KB +4 B +0.0%
components/prism-stylus.min.js 1.61 KB 1.61 KB 0 Bytes 0%
components/prism-t4-templating.min.js 403 B 405 B +2 B +0.5%
components/prism-tap.min.js 275 B 277 B +2 B +0.7%
components/prism-tcl.min.js 849 B 852 B +3 B +0.4%
components/prism-textile.min.js 1.16 KB 1.17 KB +8 B +0.7%
components/prism-toml.min.js 488 B 492 B +4 B +0.8%
components/prism-typoscript.min.js 707 B 708 B +1 B +0.1%
components/prism-unrealscript.min.js 934 B 936 B +2 B +0.2%
components/prism-v.min.js 967 B 977 B +10 B +1.0%
components/prism-vala.min.js 987 B 985 B -2 B -0.2%
components/prism-verilog.min.js 1.07 KB 1.08 KB +1 B +0.1%
components/prism-vim.min.js 5.79 KB 5.79 KB +1 B +0.0%

Generated by 🚫 dangerJS against b2a63e9

@RunDevelopment RunDevelopment merged commit 79d2218 into PrismJS:master Jun 8, 2021
11 checks passed
@RunDevelopment RunDevelopment deleted the move-quadratic branch Jun 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

1 participant