git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: git diff looping?

From
Paolo Bonzini <paolo.bonzini@gmail.com>
Date
Jun 17, 2009, 08:46 UTC
Message-ID
<4A38AD5D.6010404@gmail.com>
In-Reply-To
<20090616171531.GA17538@coredump.intra.peff.net>
> Really, that performance is so bad that I'm beginning to wonder if I am
> somehow measuring something wrong. How could they ship something so
> crappy through so many versions?

Because without some care in the matcher, the regex can be exponential. This happens because you can backtrack arbitrarily from [A-Za-z_0-9]* into [A-Za-z_] and ironically it also causes the regex not to work as intended; for example "catch(" can match the complex part of the regex (e.g. the first repetition can be "c" and the second can be "atch".

We can make it faster and more correct at the expense of additional complication.

Starting from:
^[ \t]*(([ \t]*[A-Za-z_][A-Za-z_0-9]*){2,}[ \t]*\([^;]*)$
we have to:
1) move [ \t] at the end of the repeated subexpression so that it 
removes the need for the [ \t] after
^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]*){2,}\([^;]*)$
2) make sure that at least one space/tab is eaten on all but the last 
occurrence of the repeated subexpression.  To this end the LHS of {2,} 
is duplicated, once with [ \t]+ and once with [ \t]*.  The repetition 
itself becomes a + since the last occurrence is now separately handled:

^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]* [ \t]*\([^;]*)$

Paolo
Previous: Brandon CaseyNext: Jeff King
Message 22 of 37 in “git diff looping?”
  1. John BitoJun 16, 2009
  2. Jeff EplerJun 16, 2009
  3. John BitoJun 16, 2009
  4. Jeff KingJun 16, 2009
  5. Jeff KingJun 16, 2009
  6. 1/2 Makefile: refactor regex compat supportJeff King, Jun 16, 2009
  7. Johannes SixtJun 16, 2009
  8. Jeff KingJun 16, 2009
  9. 1/2 Makefile: refactor regex compat supportJeff King, Jun 16, 2009
  10. 2/2 Makefile: use compat regex on SolarisJeff King, Jun 16, 2009
  11. Brandon CaseyJun 16, 2009
  12. Mike RalphsonJun 17, 2009
  13. Mike RalphsonJun 17, 2009
  14. 2/2 Makefile: use compat regex on SolarisJeff King, Jun 16, 2009
  15. John BitoJun 16, 2009
  16. Junio C HamanoJun 16, 2009
  17. Jeff KingJun 16, 2009
  18. Brandon CaseyJun 16, 2009
  19. John BitoJun 16, 2009
  20. Jeff KingJun 16, 2009
  21. Brandon CaseyJun 16, 2009
  22. Paolo BonziniJun 17, 2009
  23. Jeff KingJun 17, 2009
  24. Paolo BonziniJun 17, 2009
  25. Andreas EricssonJun 17, 2009
  26. Paolo BonziniJun 17, 2009
  27. Andreas EricssonJun 17, 2009
  28. Paolo BonziniJun 17, 2009
  29. avoid exponential regex match for java and objc function namesPaolo Bonzini, Jun 17, 2009
  30. demerphqJun 17, 2009
  31. Jeff KingJun 17, 2009
  32. demerphqJun 17, 2009
  33. Paolo BonziniJun 17, 2009
  34. Junio C HamanoJun 17, 2009
  35. Paolo BonziniJun 18, 2009
  36. John BitoJun 16, 2009
  37. Jeff KingJun 16, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.