Attached are some proposed patches which should improve the performance of grep -P when applied to binary files, among other things. I have some other ideas for boosting performance further but thought I'd publish these first. Please give them a try if you have the time. I doubt whether this will "solve" the performance problem entirely with -P and encoding errors but at least it should be heading in the right direction.