8 ms·
Git ls-files is Faster Than Fd and Find
- njharman 5y ago> source code management system (SCM), it’s main business3 is not to help you list your files! This seems utterly bizarre statement. What does author imagine SCM dies? Stuff, but listing the files that are the source it is managing is up there. Like saying FPS is a game its not pushing triangles through GPU. The larger purpose is facilitated by and requires the lessor purpose.
- rmetzler 5y agoSmall typo occurring two times: git ls-file instead of git ls-files. Thanks for bringing it up, I didn't know about the command. I also might have a use case for it.
- c_joly 5y agoThanks for pointing this out and for your kind words. A fix for the typos should be live soon.
- filmor 5y agoWell, first doing `find > .my-index` and then measuring `cat .my-index` would give you even better results... I don't find it noteworthy that reading from an index is faster than actually recursively walking the filesystem.
- yakubin 5y agoYes. I once wrote a tool which spends a lot of time traversing the filesystem and to my surprise in one scenario most of its time is spent on-CPU (!) in kernel-mode in the readdir_r(2) syscall implementation. I still haven't dived into what it's doing on the CPU, but it sure is interesting.
- cseleborg 5y agoYeah, that's not really the interesting part. It's clever because if you're a developer editing source files, the Git index is relatively up-to-date. So, as the author says, why not take advantage of it?
- rakoo 5y agoNo, it's not surprising, so why do we still not use indexes for this ? NTFS maintains a journal of all files modification (https://en.wikipedia.org/wiki/USN_Journal https://en.wikipedia.org/wiki/USN_Journal). This is used by Everything (https://www.voidtools.com/support/everything/ https://www.voidtools.com/support/everything/) to quickly and efficiently index _all_ files and folders. Thanks to that, searching for a file is instantaneous because it's "just an index read". The feature is common: listing files. We know that indexes help solve the issue. But we still use `find` because there's no efficient files indexing strategy. Yes, there's updatedb/mlocate, but updating the db requires a full traversal of the filesytem because there's no efficient listing of changes in linux, only filesystems-specific stuff. So we will still have articles like this until the situation changes, and it will still be relevant to the discussion because it's not "solved"
- roywiggins 5y agoWizTree also uses NTFS metadata to perform shockingly fast reports on space usage. Just much, much faster than anything else I've seen, and I'm not sure there's any equivalents for other filesystems.
- rakoo 5y agoWizTree is so fast it makes me yearn for the day we have the same funcitonality on Linux. Especially not because I'm particularly interested in visualizing free disk space on the regular, but because I want to backup changed files as soon as possible and the only reliable way to do it is to have some kind of journal.
- ww520 5y agoUpdating an index adds additional time, adding a random seek to the index file location. Also requires some transaction to group the update to the file metadata and the index. It just adds more complexity with little benefit.
- ziml77 5y agoAs someone who uses Everything many times per day, I can say that there is a significant amount of benefit. I don't think I'd be able to function at work without being able to instantly search all my files. The lack of a similar solution on Linux is one of the big barriers to me using it. The best options I've seen there all refresh the index on a schedule.
- c_joly 5y ago> I don't find it noteworthy that reading from an index is faster than actually recursively walking the filesystem I agree, what I found noteworthy though is that git ls-files uses this index you already have for. (author here, “proof”: https://cj.rs/contact/ https://cj.rs/contact/ & https://keybase.io/leowzukw https://keybase.io/leowzukw)
- gorgoiler 5y agoOne thing I’ve never understood about Linux filesystems: given how small and bounded the sets of directories and directory entries are, why is filesystem traversal not instantaneous?
- spicybright 5y agoLack of caches I'd assume. Not because it's not easy, but because no one has taken the time to implement it.
- toxik 5y agoThey say there are two hard problems in computer science: cache invalidation, naming, and off-by-one errors.
- spicybright 5y agoHehe. You're right, I forgot one of the golden rules. There's probably a better reason why no one has implemented it.
- amelius 5y agoAlso, why can tab-completion in Bash hang my shell?
- chrsig 5y agoThis is actually an incredibly frustrating thing -- when doing disk i/o, the process goes into an uninterruptible sleep state[0]. So if your shell is doing some tab completion that goes into an uninterruptible sleep, there's no escape sequence you can send it to cancel that operation. Where I've seen this be the most vexing is in AWS if an EBS volume dies (surprisingly more frequent of an occurrence than you'd think!). It effectively prevents the machine from cleanly shutting down, and you wind up having to wait for the instance to be forcefully terminated. [0] https://eklitzke.org/uninterruptible-sleep https://eklitzke.org/uninterruptible-sleep
- cb321 5y agoI got run times from the simplest single-threaded directory walk that are only 1.8x slower than git ls-files. (Min time of 10 runs with the git repo housed by /dev/shm on Linux 5.15.) The "simple" code is in https://github.com/c-blake/cligen/blob/master/cligen/dents.nim https://github.com/c-blake/cligen/blob/master/cligen/dents.n... (just `dents find` does not require the special kernel batch system call module to be fast. That kernel module is more about statx batching but IO uring can also do that these days. For those unfamiliar with Nim, it's a high productivity, high performance systems language.) I believe that GNU find is slow because it is specifically written to allow arbitrary filesystem depth as opposed to "open file descriptor limit-limited depth". (I personally consider this over-/mis-engineered from days of bygone systems with default ulimits of, say, only 64 open fd's. There should at least be a "fast mode" since let's be real - file hierarchies deeper than 256..1024 levels, while possible, are rare and one should optimize for the common case or at least allow manual instruction that this case prevails. AFAIK there is no such "fast mode". Maybe on some BSD?) Meanwhile, I think the Rust fd is slow because of (probably counterproductive) multi-threading (at least it does 11,000 calls to futex).
- aaaaaaaaaaab 5y ago>Meanwhile, I think the Rust fd is slow because of (probably counterproductive) multi-threading (at least it does 11,000 calls to futex). Afaik rustaceans call this “fearless concurrency”.
- R0b0t1 5y agoFrom experience benchmarking this in the past, Window's poor NTFS implementation sees speedup from multithreading (which I can't make sense of) whereas Linux usually had a penalty for multithreading directory traversals. But, as mentioned below, this is faster because git-ls is using a pregenerated index.
- cb321 5y agoNo question/disagreement that it's faster from having an index. It just need only be 1.8x faster -- not the much higher ratio reported in the article (on Linux, anyway). I was mostly just adding some performance color that the points of comparison of the article are, in some sense, not very fast.
- bee_rider 5y agoOf course scanning an index is faster than traversing the filesystem. Is locate/mlocate some obscure command? It works pretty well for this sort of thing (and has the advantage that you wouldn't need to put git repos everywhere, or something silly like that). I often forget what I've named a pdf that I've downloaded, but usually I'll put something related to the topic of a paper in the file name, so a command like: locate -i "*svd*.pdf" will have a decent chance of finding any papers about svd's that I have, and is more or less instantaneous. Although -- I think I did come across the locate command because I was searching for alternatives to 'find.' I can not for the life of me remember the correct order for the path to start searching, and the filename.
- cb321 5y agoplocate [1] is even faster (but of course with either you need to re-scan the whole FSes and rebuild indexes to have an up-to-date view). [1] https://www.linuxuprising.com/2021/09/plocate-is-much-faster-locate-drop-in.html https://www.linuxuprising.com/2021/09/plocate-is-much-faster...
- quicklime 5y agoThe problem with locate is that it requires the index db to be updated periodically (I think this happens daily by default?). For some use cases, especially those where I'm searching for files in a tree that I'm actively working in, this forces me to fall back to find (or maybe git ls-files now). I feel like it should be the job of the filesystem to maintain an index and incrementally update it whenever files are created or removed, but afaict no modern filesystem offers it.
- spicybright 5y agoIt really is strange we don't have that yet. We already pretend files are on the physical disk and sync it in the background. Adding smarter indexing to that shouldn't be a huge deal.
- ziml77 5y agoAs mentioned elsewhere in these comments, NTFS actually does do that. There are a couple tools out there that take advantage of it like Everything Search and WizTree. I don't know why no open source filesystem has done the same thing.
- cryptonector 5y agoThis is why I have a git alias `ls` for `ls-files`.
- mikewhy 5y agoFYI to anyone looking to try this out: the `--others` flag, used to include untracked files, will also print out files included in your .gitignore. So if you have eg. a node_modules/ or venv/ folder, all its contents will be listed. This is often unwanted noise. I haven't been able to find if there's some combination of flags that would get the desired behaviour, but it's been a while since I've messed around with this.
- xyzzy_plugh 5y agoWhat's the desired behaviour you want? --cached --others --exclude-standard will show tracked and untracked but not ignored.
- deleted 5y ago[deleted]
- tyingq 5y agoA warm buffer cache makes a big difference too, so if you're benchmarking things like find vs other tools, be sure to empty the cache between runs. For Linux: echo 3 > /proc/sys/vm/drop_caches In this case, the author is using hyperfine with --warmup 10, so the numbers are all using a warm buffer cache. A cold cache probably would have been more realistic for comparison, since the benchmark is traversing lots of directories.
- kelnos 5y agoPerhaps, but it depends on how it's used in the real world. The author was benchmarking tools for the purpose of finding files in via a text editor. If that's something that's done once, then sure, cold caches make sense. But if it's done frequently, presumably those caches will be warm for all but the first run, so the expected, common performance encountered would be with the warm cache.
- tyingq 5y agoSure. Just saying that benchmarking anything that recursively trawls directories is probably going to hit a cold cache often.
- deleted 5y ago[deleted]
- nmz 5y agoWalk is faster than find. https://github.com/google/walk https://github.com/google/walk
- bobbyi 5y agoThis has annoyed me in the past: https://stackoverflow.com/questions/9883851/why-git-ls-files-is-not-shown-on-tab-completion https://stackoverflow.com/questions/9883851/why-git-ls-files... It's a useful command that I consider to be relevant to end users, not just an internal "plumbing" utility
- deleted 5y ago[deleted]