On Thu, Mar 6, 2014 at 10:36 AM, Ingo Schwarze <schwa...@usta.de> wrote:
> back in November 2013, following an idea by guenther@,
> i cooked up another optimization for seekdir(3),
> then failed to send out the patch.  So here it is.
...
> With this patch, rewinddir(3) changes from always being the worst
> case to often being the best case (now always the best case for
> directories with less than about 500 entries, or for rewinding
> before reading less than about 500 entries), no other case becomes
> any worse, and some other, less important edge cases improve, too.
>
> What the patch does is:
>  * Let opendir(3) save the cookie of the first entry of the directory.
>  * Let seekdir(3) use that saved cookie to move the internal pointers
>    to that entry when asked to do so, for example on rewinddir(3).
>  * Let seekdir(3) update that saved cookie when the buffer is exhausted
>    and when consequently, an lseek(2) had to be done.
>  * Let seekdir(3) invalidate the buffer by setting the pointer after
>    its end, instead of to its beginning.
>  * Have readdir(3) refrain from invalidating the buffer
>    when asked to return the first element from it.
>
> This comes with only one price:  Adding another off_t to the
> opaque struct _dirdesc (= DIR), which already contains another
> off_t, two long, two int, and two pointers.  That's not much
> growth, especially since no sane code will allocate large
> numbers of DIRs.
>
> As explained last year, this patch reduced typical execution
> times of rewinddir(3) on my notebook for about 100 microseconds
> to about 0.05 microseconds.
>
> The /usr/src/regress/lib/libc/telldir/ test suite is still happy,
> and so are some other, manual tests i have done.

Looks good to me, with some nice simplifications thrown in.


> Am i right that, the type DIR being opaque, this patch doesn't
> require any libc bump?

Right.


> Comments, OKs?

ok guenther@

Reply via email to