On 06/11/2012 06:54 AM, Rich Felker wrote: >> GNU M4 (at least the master branch, although it has not yet been >> released as m4 2.0) _wants_ to use freadahead, because it provides at >> least a 10% speedup in operation. It _really is_ more efficient to peek >> at the current buffer in one function call than it is to call multiple >> fgetc_unlocked(), in order to rapidly search for the next character of >> interest.
Actually, m4 uses freadptr(), not freadahead(), and _does_ fall back to slower code when freadptr() returns NULL. But as noted in the gnulib code for the two functions, freadahead() can be as simple as whether freadptr() returns a non-NULL value. I think a 0/1 freadahead() would work, but the speedup comes from providing an freadptr() that actually reads ahead. > > Can you explain how knowing the _number_ of buffered characters helps > you find the next character of interest efficiently? It doesn't. Rather, the speedup comes from the ability to peek into that buffer in advance, using freadptr(), so that you can use strchr(), strstr(), or other search functions on the buffer, followed by an fread() of the appropriate size, all in order to minimize the number of function calls without reading past the ISO C99 1-byte ungetc() portability limit. Hence the SLOW_BUT_NO_HACKS definition that loses out on the speedup if freadptr() always returns NULL (at which point freadahead() could be treated as always returning 0, implying that there is no read-ahead buffer). > As for musl, would adding __freadahead be sufficient to make it use > __freadahead, or does gnulib hard-code knowledge about which systems > have which stdio extension functions? Depending on the function name you choose, we would have to add an m4 check to see if __freadahead() is defined; but once we know the name of the function to check for at configure time, then it is quite simple to code up gnulib's freadahead.c to call into the internal __freadahead() function of libc, when one exists. -- Eric Blake ebl...@redhat.com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature