Repository navigation
Closed
Labels
bugObserved behavior contradicts documented or intended behaviorstandard libraryThis issue involves writing Zig code for the standard library.
Milestone
Description
Zig Version
0.10.0-dev.2822+b79884eaf
Steps to Reproduce
Zig (zig build-exe -OReleaseFast rm.zig)
const std = @import("std");
pub fn main() anyerror!void {
try std.fs.cwd().deleteTree(std.mem.span(std.os.argv[std.mem.span(std.os.argv).len - 1]));
}C (clang -O3 rm.c) using removefile (which turns out to be slower or the same as rm)
#include <errno.h>
#include <removefile.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "usage: rm path");
return 1;
}
if (removefile(argv[argc - 1], NULL, REMOVEFILE_RECURSIVE)) {
fprintf(stderr, "removefile failed: %s", strerror(errno));
return 1;
}
return 0;
}Assuming node_modules is a folder with lots and lots of files and directories (~700 MB, xxx,xxx files)
hyperfine "./rm node_modules_copy" "./a.out node_modules_copy" "rm -rf node_modules_copy" --prepare="cp -r node_modules node_modules_copy"Expected Behavior
Zig should perform similarly or better
Actual Behavior
❯ hyperfine "./rm node_modules_copy" "./a.out node_modules_copy" "rm -rf node_modules_copy" --prepare="cp -r node_modules node_modules_copy"
Benchmark 1: ./rm node_modules_copy
Time (mean ± σ): 5.796 s ± 0.681 s [User: 0.041 s, System: 5.363 s]
Range (min … max): 4.457 s … 6.761 s 10 runs
Benchmark 2: ./a.out node_modules_copy
Time (mean ± σ): 4.614 s ± 0.536 s [User: 0.040 s, System: 4.226 s]
Range (min … max): 4.170 s … 6.090 s 10 runs
Warning: The first benchmarking run for this command was significantly slower than the rest (6.090 s). This could be caused by (filesystem) caches that were not filled until after the first run. You should consider using the '--warmup' option to fill those caches before the actual benchmark. Alternatively, use the '--prepare' option to clear the caches before each timing run.
Benchmark 3: rm -rf node_modules_copy
Time (mean ± σ): 4.145 s ± 0.198 s [User: 0.046 s, System: 3.750 s]
Range (min … max): 3.894 s … 4.420 s 10 runs
Summary
'rm -rf node_modules_copy' ran
1.11 ± 0.14 times faster than './a.out node_modules_copy'
1.40 ± 0.18 times faster than './rm node_modules_copy'Metadata
Metadata
Assignees
Labels
bugObserved behavior contradicts documented or intended behaviorstandard libraryThis issue involves writing Zig code for the standard library.

Activity
squeek502 commentedon Oct 3, 2022
I get similar results on Linux:
(tested with the folder that resulted from
npx create-react-app my-app; 41209 files, 344MiB)strace -cresults:Zig:
rm -rf:Worth noting that
find my-app -type d | wc -lgives 4863 so it seems likely that Zig is opening/iterating directories much more than necessary. Also worth noting that if the 24159 erroringunlinkatcalls were removed from Zig's implementation then the number ofunlinkatcalls would match withrm -rf.squeek502 commentedon Oct 3, 2022
As far as I can tell, the extra syscalls come from two sources:
deleteTreeis non-allocating and only has one directory open at a time. This means that a directory structure likea/b/cwill open/close a, b, and c, then delete c, then re-open/close a and b, then delete b, then re-open/close a and then finally delete a. An allocating API could use a stack a laWalkerwhich would remove these redundant open/closes.start_overloop, there is adeleteFilecall. This tries to delete the sub_path as a file over and over and most of the time this call will just fail over and over witherror.IsDirsincedeleteTreeis usually called on directories. For example, in thea/b/cstructure, it will calldeleteFile("a")3 times and they will all fail witherror.IsDirerror.IsDir), but I think that'd introduce an edge case where we start callingdeleteFileon a directory and then while trying to delete stuff it gets changed into a file, whichdeleteTreewould then fail to delete.error.IsDiran additional 3 times in thea/b/cexample due to thedeleteFilewithin the scan_dir loop as well. This could possibly be addressed by checkingentry.kindwhich isn't done currently.I'm not totally sure if these are addressable given the non-allocating, single-open-fd API of
deleteTree, but an allocating version could address them, so maybe introducing that as an option might be worth looking into.nektro commentedon Oct 4, 2022
why allocate when the function can be made recursive? (without reading the source) I would assume its a recursive function given the nature of the operation. was it to save on open file descriptors? stack overflow?
going with recursion the non-allocating aspect can definitely be preserved
Jarred-Sumner commentedon Oct 4, 2022
An interesting note from
removefile.h:I wonder what the perf impact here is if we go depth-first instead of breadth-first
squeek502 commentedon Oct 4, 2022
Not totally sure, but there is this comment:
zig/lib/std/fs.zig
Lines 2099 to 2101 in ff534d2
I would assume it's to avoid stack overflow
ifreund commentedon Oct 4, 2022
recursion is just a different form of allocation. It allocates on the stack instead of the heap.
nektro commentedon Oct 4, 2022
having
deleteTreeRecursiveas an option would solve the performance issue though for those that arent concerned about it not allocating at allsqueek502 commentedon Oct 5, 2022
My attempt at a recursive implementation ended up being slightly faster than my system's
rm -rf.PR here: #13070
EDIT:
Just-as-fast but non-recursive implementation here: #13073