Why does adding one more alternative make my regex over 600 times slower?

前端 未结 1 419
时光取名叫无心
时光取名叫无心 2020-12-09 16:15

I noticed something weird while testing a simple Perl script that\'s supposed to filter out filenames beginning with certain prefixes.

Basically, I\'m constructing a

相关标签:
1条回答
  • 2020-12-09 17:05

    For a long time, perl's TRIE optimization has not been applied where the initial compilation of the regex produces longjmp instead of jmp (I think) instructions (which depends on the number of alternations and the total lengths of the strings involved and what else is (earlier?) in the regex).

    See the difference between:

    perl -we'use re "debug"; qr/@{[join"|","a".."afhd"]}/'
    

    and

    perl -we'use re "debug"; qr/@{[join"|","a".."afhe"]}/'
    

    You can break your alternation down into smaller chunks and precompile them separately and do e.g. (??{$rxa})|(??{$rxb})|(??{$rxc}).

    0 讨论(0)
提交回复
热议问题