Why GNU grep is fast
lists.freebsd.org
lists.freebsd.org
Never to be discussed without bringing up The Treacherous Optimization - http://ridiculousfish.com/blog/archives/2006/05/30/old-age-a...
Edit: and TTO's discussion - http://news.ycombinator.com/item?id=1627367
Nice to see you register to make a contribution - it's an important question to ask, and I appreciate that you've taken the effort to do so (and I've upvoted you).
I had not read it for a while and enjoyed reading it again. There were definitely somethings I forgot but I am wondering if something has changed/happened that should change my reading of the thread.
time LANG=C grep asdf < les_misérables.txt > /dev/null
versus time LANG=en_US.UTF-8 grep asdf < les_misérables.txt > /dev/nullMy guess is that p9idf has it right, and grep is just converting everything to wchar_t first, rather than trying to do any sort of clever searching directly on the UTF-8 byte stream.
Or if your input data is sufficiently ASCII-ish, and so is your search pattern, then why not just force the process locale to C and avoid the whole mess to begin with.
I'm suddenly left wondering how the "." regex syntax functions in the face of surrogates when handling UTF-8.
However, if you find four bytes 'asdf' in the input, you still have to check whether a combining mark follows the 'f'. For this example, that is simple, but I guess things get hairy for many regexes found in real life, such as ones containing even a single period.
The UTF standard used to allow non-conformant representations of ASCII characters. As http://www.schneier.com/crypto-gram-0008.html notes, this lead to security problems. Now the standard says that you can't allow non-conformant representations of ASCII characters. And if you look at http://www.unicode.org/versions/Unicode6.0.0/ch03.pdf and scroll to page 94 you'll find that you can't be said to be conformant unless you explicitly reject non-conforming input.
Therefore UTF-8 decoders cannot be considered conformant unless they actually examine each and every byte to verify that there is nothing dodgy.
I no longer notice this behavior:
dfc@motherjones:~$ grep --version
grep (GNU grep) 2.9
dfc@motherjones:~$ time LANG=C grep asdf < pg135.txt > /dev/null
real 0m0.017s
user 0m0.008s
sys 0m0.004s
dfc@motherjones:~$ time LANG=UTF8 grep asdf < pg135.txt > /dev/null
real 0m0.017s
user 0m0.012s
sys 0m0.004s
dfc@motherjones:~$ time LANG=en_us.UTF8 grep asdf < pg135.txt > /dev/null
real 0m0.012s
user 0m0.004s
sys 0m0.004s
There is not a lot of info about this in debian bug 604408http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=604408
If memory serves me correctly this upstream fixed this sometime after 2.7.1 or 2.7.3
Funner fact: GNU grep used to be slow with UTF.
; grep --version
GNU grep 2.5.3
; time LANG=C grep asdf < lesms10.txt > /dev/null
real 0m0.025s
user 0m0.011s
sys 0m0.014s
; time /usr/local/plan9/bin/grep asdf < lesms10.txt > /dev/null
real 0m0.082s
user 0m0.043s
sys 0m0.013s
; time LANG=en_US.UTF-8 grep adsf < lesms10.txt > /dev/null
real 0m1.209s
user 0m0.818s
sys 0m0.018s
Those are the only two grep implementations I have handy. GNU grep 2.6.3 takes the same amount of time searching for 'asdf' in both locales, but searching for '.' is still slow. Thanks for pointing that out.Priceless...
In the second case, you are doing less (computation) to go faster.
The less expensive operations you have to do (whatever they may be), the faster your program is. That has been true for the past 70 years, and will continue to be true as long as computing remains in the slightest bit recognizable to men of our field as it currently stands.
b : the human race : humankind
http://mw1.merriam-webster.com/dictionary/manIf this means that male gendered individuals will have to be pluralized as "males" and never "men", then that's something we should live with.
Imagine changing all of the restroom signs from "men" to "males", because "men" would signify "bathroom for everyone".
Or seeing in the newspaper "The blast killed 22 men, including 3 that were pregnant".
https://secure.wikimedia.org/wiktionary/en/wiki/men
Now take your self-righteous offtopicness and shoveoff. I contribute nothing to the problems of society by exercising freedom of vocabulary. You want to see real problems? Go look at pictures of the shelled remains of children in Syria. You're suggesting that I am contributing to a "real-world problem" is insulting.
Even if we want to consider the furthering of systemic sexism through benign use of vocabulary a "real-world problem", you are more to blame than I by trying to convert an innocent phrase into an example of hatred. Words only have the power that you allow them to. I accept no responsibility for your oversensitivity.
This is not exactly a response to you, BTW (since I can't imagine you'd be convinced) but for other readers.
In the other two cases (doing extra work to tolerate communication latency and node failures) I have searched long and hard for a way that your statement could be correct, but I cannot imagine what it could be.
You are very likely correct that it continued to be true as long as computing remained recognizable to the men (and women) of earlier generations. But in a world of multiprocessor microcontrollers, it is no longer true.
FWIW: I've upvoted the coments about earlier submissions - I'd like to reward such behavior. Not least, searching for previous discussions often unearths genuinely useful material and should be encouraged.
Just a friendly reminder, you may have forgotten, here's the profile blurb you have up right now:
> I'm tired of the repetition, [...] old material being recycled,
> as I write this, Lockhart's Lament has been posted again.
EDIT: formatting error
... banal comments, old material being recycled,
and general lack of interesting stuff.
In short, the vast majority of repeated material is drivel. Fluff. Content-free. I'm trying to restore the balance a little with more technical, challenging and deep material.This is probably controversial, but it's my last ditch effort to actually get some technical material back into Hacker News.
I honestly don't expect it will succeed, but then I can finally depart with a completely clear conscience that I tried my best and found that the so-called "Hacker News" no longer contains much I care about.
I hope I'm wrong, but I had to try the experiment.
PS: And FWIW - I've upvoted you.
It would've been nice to drill down why mmap makes a difference in particular, why not use something like lseek to skip characters?
Also, if you're reading from a file with lseek() and read(), you have to marshall data into the struct the kernel expects, make the syscall, and the kernel copies the data into the user-space buffer you provide. If you're reading from a file with mmap(), the page cache just appears in your address-space, no copying necessary.
On the other hand, I was curious why the default had changed from "use mmap" to "don't use mmap". Turns out, the grep 2.6.3 documentation states:
--mmap: This option is ignored for backwards compatibility. It used to read input with the `mmap' system call, instead of the default `read' system call. On modern systems, `--mmap' rarely if ever yields better performance.
Then again, hard drives return minimum sized blocks of data anyway, which is sized in kilobytes. So I'm guessing the "skipping" is a performance benefit only as so far as user space goes.
One would really have to grep a long string to be able to skip more than a block/page!