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

Str.global_replace goes exponential on a fairly innocuous regular expression #4011

Closed
vicuna opened this issue Apr 26, 2006 · 1 comment
Closed

Comments

@vicuna
Copy link

vicuna commented Apr 26, 2006

Original bug ID: 4011
Reporter: @dra27
Status: closed (set by @damiendoligez on 2006-08-29T15:55:31Z)
Resolution: won't fix
Priority: normal
Severity: tweak
Version: 3.09.0
Category: ~DO NOT USE (was: OCaml general)
Monitored by: @dra27 @alainfrisch

Bug description

In the toplevel (having loaded str.cma) enter the following statements:

let r = Str.regexp "^ +\(\([^ ]* [^ ]+\)\) +$";;
let s = " ^ and / from / my pe/tition.";;
Str.global_replace r "\1" s;;

On my 1.1GHz XP box, this takes several seconds to return. I then changed the string to " ^ and / from with added words / my pe/tition." and re-ran it: after an hour, I killed ocamlrun!

Additional information

The sample data has been reduced: the expression is useful in the code it comes from!

The regular expression is bad - the initial [^ ]* in the capture group is redundant and causes a lot of backtracking (I'm assuming) so of course I removed it to eliminate the problem in my actual code.

I'm not convinced that this is a bug in Str but thought it worth reporting just in case the exponential behaviour is unintentional.

@vicuna
Copy link
Author

vicuna commented Aug 29, 2006

Comment author: @damiendoligez

Backtracking regexp interpreters will always have exponential worst cases. Optimizing special cases is then a trade-off exercise. We heavily lean on the side of code simplicity.

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

No branches or pull requests

1 participant