4 ms·
This only makes the disk space smaller and saves loading the executable image from disk, which the kernel already magics away anyway. This does nothing for wha
by Brian_K_White 5y ago
This only makes the disk space smaller and saves loading the executable image from disk, which the kernel already magics away anyway.
This does nothing for what the author was talking about which is the cost of creating and destroying a process. The kernel has to do a lot of setup and management work for every process.
Busybox would only help with this IF busybox's sh had special fork handling that recognized any built in commands and did not actually spawn a new instance of busybox for every | or `` or ().
If you write an sh script using no external binaries at all, 100% built in keywords, you will still have a very slow script if it has a lot of filelist=`echo *` because even though that echo was a sh builtin (just like a busybox builtin), a new instance of sh was spawned just to run that echo, then destroyed.
And that happens a lot. It's quite a bit more difficult to avoid that, and is exactly what the author means by writing contorted code just to avoid that.
Myself I cheat a little and allow myself to use bash as the shell, and then use every possible bash feature. For instance a huge bash feature for avoiding subshells is "printf -v" Which places the oitput of a printf command into a variable instead of stdout. Not to mention that printf itself is builtin and has several very useful features for data manipulation just from converting values with the % formatters, including another biggie is onr of the % codes it supports is %()T, which gives you essentially the date command without running date.
Just a few bashisms like that above strict posix sh makes it possible to avoid nearly all subshells.
Possible, and more performant, BUT, your program is arcane and tortured. Difficult to write, difficult to read, and full of otherwise bad practises like using a sea of global variables and state simply to avoid having to pass data around by foo=`generatefoo`, abusing the language/line parser to attain array operations without an actual split(), doing piles of work all within a huge indented wile read loop, etc.
- 1vuio0pswjnm7 5y agoYet the time cost of starting UNIX userland programs, including a crunched sh, busybox or simply a static dash, is perceptively less than starting Python. There is a lot of ridiculous criticism about shell scripts on the web in recent times. The author admits that it is unlikely a scripting language will outperform sort, sed and so on. However if I share something I wrote entirely in sed that can also be done in, say, Python with fewer LOC (because the work is being done by libraries) or in some other, large, popular scripting language, virtually no one commenting will appreciate the speed benefits of using sed. I can deliberately avoid pipelines, I can store transformed data in temporary files, but most times I want to use pipes. They do not slow things down, they speed things up. That's because I do not write long, convulated shell scripts. Many people want to write larger, more complex scripts. If so, then by all means use Python or whatever. But for smaller, simpler scripts, there is nothing wrong with the shell. Pipes are incredibly useful. People make shell scripts slow and then try to blame the shell. Also, you forget to mention using {} which should not create a subshell.
- 1vuio0pswjnm7 5y agos/convulated/convoluted/
- Brian_K_White 5y agoI'm not sure what you're arguing. I was only explaining how busybox does not do what the previous commenter thought. The authors entire point about sort was to show just how little it's speed matters, because the job is rarely 90% within sort, or any other util like that (I don't count awk because awk is a fully functional language like sh, you could do the entire job in awk). How fast sort sorts 100 lines of text doesn't matter next to the cost of forking to execute sort, especially if it will happen repeatedly to do small jobs, treating it like a function. The reason it's bad is that there are so many and so small such things, and it adds up, and the shell is specifically designed to hide the cost of forking a process by making keywords and executables indistiguishable. And so you end up with a zillion forks just to get the current time or to strip a few bytes off of a few bytes long string, or even to do nothing but simply store a value in a variable (date, basename/dirname, foo=`generatefoo`) Sure, that doesn't mean don't use sh, nor does it mean do the contortions necessary to write in sh but avoid that problem, all that means is don't write non-trivial apps in sh. ... well unless you need the ubiquity more than the performance or readability. So it doesn't even necessarily mean that automatically. temp files - It would make no sense in most cases to go through pains to avoid forks only to use temp files instead. Creating and destroying a file, even in tmpfs(ram), is as bad or maybe even worse than a process. {} - I didn't forget {}, it had no special bearing on an explaination of what the author was talking about that makes shell scripts slow, and why busybox does not change that. It's just a branching structure like any other.
- deleted 5y ago[deleted]