wryun / es-shell

es: a shell with higher-order functions
http://wryun.github.io/es-shell/
Other
307 stars 25 forks source link

`%read` in chunks for lseek()able files. #107

Open jpco opened 1 month ago

jpco commented 1 month ago

This is a performance optimization that's normally insignificant; occasionally good, in tight %read loops; and very rarely negative, when those tight %read loops work on a file with mostly zero-to-one character lines. Generally the performance improvement increases as line lengths increase, which shouldn't be a surprise. Files with pretty short lines, like /usr/share/dict/words, see a measurable improvement with tight %read loops, though.

In theory, it seems to me the overhead of the extra lseek() calls (which are the source of the negative effects, where they appear) could be reduced by caching the seekability of fds. Maybe just one such value, assuming performance-sensitive calls to %read are typically done repeatedly in a loop (and on a single fd).

This introduces an awkward performance asymmetry, sadly, between the cases of cat foo | the_script and the_script < foo , since stdin in the former case is non-seekable from a pipe, while in the latter case it's seekable.

I'm not married to this proposal. Just wanted to put it out there. I'm not very good at speedy code so there's very possibly a better way to do this than I've got here :)