3 ms·
I find the FreeBSD one more embarassing, to be honest. The GNU version just does a ton of annex stuff (hex handling, usage(), etc). I have no idea what the hell
by Wilya 14y ago
I find the FreeBSD one more embarassing, to be honest. The GNU version just does a ton of annex stuff (hex handling, usage(), etc). I have no idea what the hell the FreeBSD one is trying to do.
- Camillo 14y agoIf you have multiple programs writing to the same terminal, their write calls can get interleaved. To ensure that a single echo command's output remains unbroken, it should be output using a single write call. GNU relies on stdio's buffering for this. Plan 9 allocates a buffer for the entire output and copies the arguments there. FreeBSD wants to avoid linking to stdio, and wants to avoid copying memory (both for reasons of efficiency, presumably), so it uses the writev call instead, which lets you pass the kernel an array of buffers to write, without having to build up a single buffer in advance.
- lcampbell 14y agoThe FreeBSD version is building up an iovec (which is effectively an array of (pointer, length) tuples) to pass to writev. I believe this is done for two reasons -- 1) It reduces the number of syscalls -- a single call to writev outputs everything, rather than multiple fputs calls which may or may not be buffered (I haven't checked the impl details of fputs). 2) It reduces the number of allocations. Since an iovec is just an array of pointers, we can reused the data passed in via argv; the length of the iovec is capped at argc, so we can get away with one allocation without scanning the input. For a comparison, here's a brief overview of each of the implementations (assuming fprint/fputs is not buffered): * Unix V5: 0 allocations, N syscalls, no argv scan * OpenBSD: 0 allocations, N syscalls, no argv scan * Plan 9: 1 allocation, 1 syscall, scans argv * FreeBSD: 1 allocation, 1 syscall, no argv scan * GNU (without -e): 0 allocations, N syscalls, no argv scan "argv scan" basically means calling strlen on every passed argument, or otherwise iterating over them. Naturally, "which one is faster" depends on how fast your allocations/syscalls/strlen's are, and how fprint/fputs is buffered.