Anything which leverages RTTI (run-time type information) in Odin will result in a much larger binary. In core:flags
, I tried to keep this minimal, but it was largely unavoidable due to how the package works. It’s unfortunate, but thankfully it doesn’t incur any program-wide operation cost beyond that.
You said you’re on Linux, so you should have upx
available in some fashion. With a trivial example of an Odin program that just uses one call to fmt.println
, I can compress the resulting binary (compiled with -o:size
) down to 36% of its original size. (161KiB to 59KiB)
I’ve wondered if there might be demand for a “light” alternative to fmt
that’s merely a layer over the system-specific calls for printing without all the RTTI/formatting. Such an API would necessarily be unable to print anything but the standard types and all calls would be single-argument.
import "core:printer"
printer.append("Hellope ")
printer.append(42)
printer.append('\n')
For anything more complex such as structs, arrays, or maps, you’d have to build your own printer on top of the base calls.
Chris Wellons has a good article on this topic of print-is-append: Let's implement buffered, formatted output
Finally, I have also wondered if a non-LLVM Odin backend would be able to sidestep a lot of the binary size increase of RTTI through some custom and specific manner, but I don’t know enough about compilers at this time to say whether or not this is possible.
EDIT: As a follow-up experiment as to how much smaller a fmt
-free program can be, I wrote this:
package main
import "core:sys/linux"
main :: proc () {
str := "Hellope world!\n"
linux.write(linux.STDOUT_FILENO, transmute([]u8)str[:])
}
Compiling with -o:size
, I get a binary that is 21KiB. Using upx
reduces this down to 12KiB.
But we can go further. Let’s compile with -o:size -no-crt -default-to-nil-allocator -no-bounds-check
. No dependencies on libc, hence no default allocator because it uses malloc
. We won’t need dynamic allocation anyway.
The binary I compile is now 9.9KiB. Using upx --best
on this results in a file that is 5.5KiB large on my system.
9.9KiB is still a ton of space just to print a single static string, but it’s far smaller than what we started with.