3998-byte executable reduced to 45 bytes
muppetlabs.com
muppetlabs.com
wget -O - http://www.muppetlabs.com/~breadbox/software/tiny/teensy.htm... | wc
result: 41150 bytes
Writing large programs in C, I rarely see the need to optimise program size - I'm more interested in execution time. Even so, with thousands of lines of code, the executable is typically < 200kb, which would run on an Amiga, never mind a PC.
I think this article is slightly outdated as we don't run 64kb 8-bit machines anymore.
My last comment is that you could use an executable 'packer' which uncompresses at runtime, and that's far easier than his methods.
It's a clever hack that teaches you about how ELF works by forcing you to think carefully about every single byte in a customized ELF executable.
I work frequently with object file formats (especially ELF) and write tools which manipulate executables and libraries and when I first read this article about 5 years ago I learned more about ELF than from any other source.
Highly recommended!
And I thought it was an inspiring exercise to go as far down to the metal as possible.
It does make me feel guilty when programming Ruby that I sometimes create an entire array object to compare two integers because it looks prettier, without any regard for performance. It feels so wasteful.
mbp:~/test doug$ cat tiny.pl
return 42;
mbp:~/test doug$ ls -la tiny.pl
-rw-r--r-- 1 doug doug 12 Oct 15 11:31 tiny.pl
The point of theexercise is to reduce to the smallest bit of program.. Your version should count the size of the interpeter as well, since it's necessary for execution.
Under that model, I could use a program of
1
I'll run this program with my new SwankyCool intrepreter [tm] which returns 42 if it sees a program comprising only "1"; Otherwise it calls perl to exec the rest of the file.
Now, in both cases, we're accepting that the entire OS is already there, but that was stated in his example. (ELF on Linux)
This is pretty far beyond 'optimization'. The executable is 7 bytes smaller than the minimum legal size for the object file header!