Cons of including everything into executable?

#1

I am writing a tool that produces files. The templates I use to generate those files are fixed (don’t need to be loaded at runtime or changed after compiling).

Rust has many convenient utilities like include_str!, so I’ve just been using those to load the templates and store them in a &'static str. This is significantly easier than bundling multiple files with the executable and figuring out where to store them when distributing.

Is there any downside to this approach? Will I run into problems with executable size or something? I don’t mind if the executable is somewhat big, but I don’t know if this can cause other issues. The templates aren’t larger than 2KB.

1 Like

#2

I think the only drawback of including files into your executable is that they will always be loaded as part of the executable. If not all the files are always used, this can result in a slower startup time or more memory use compared to external files. In the case of virtual memory paging in a modern OS even these are (partially) avoided.

For 2kB templates, I don’t think there’s any reason to be concerned, it’s probably negligible compared to existing executable size.

1 Like

#3

Thanks!

Could you say more about this? What makes the startup time slow/memory use high? How much (approximately) would I have to include to start to worry about running into this?

0 Likes

#4

Executables are demand-paged on most platforms, so the effect will be negligible. There might be a little more due to prefetch and alignment, but not relevant.

If the template text is large and repetitive, you could compress them and decompress in ram. This uses more memory, but again, only a little if they’re highly compressible.

4 Likes