How do demomakers attain ultra small filesizes?
Asked Answered
G

5

6

When I watch demoscene videos on youtube the author's often boast of how their filesizes are 64kb or less, some as few as just 4kb. When I compile even a very basic program in C++ the executable is always at least 90kb or so. Are these demos written entirely in assembly? it was my understanding that demomakers used c/c++ as well.

Goudy answered 9/2, 2014 at 20:42 Comment(5)
Yeah, you've got it right mostly. The secret ingredient though are advanced .exe compressors, as well tricks they pick up along the way to keep the file size down. Many (all?) of them are tied to a particular compiler (mostly MSVC). No templates here.Verdha
It is all in what you link in, you can use C/C++ just fine for this work.Particia
If you don't use any library functions, you can also achieve rather small file sizes with a compiler. However, as soon as you link in your first printf you are toasted. :) Of ocurse, getting below a certain minimum requires assembly.Commensurate
possible duplicate of What languages or methods allow graphics & music demos to fit in 64kb EXEs?Hashish
You call that ultra small? I remember seeing 64-byte demos (.com files, exited via a single-byte RET, not even the traditional 2-byte INT 20). Granted, they didn't do much.Segura
A
12

I'm one of the coder of Felix's Workshop and Immersion (64k intros by Ctrl-Alt-Test). Most 64k intros nowadays use C++ (exception: Logicoma uses Rust). Assembly may make sense for 4k intros (although most of them actually use C++), but not for 64k intros.

Here are the two most important things:

  • Compile without the standard library (in particular, the STL could make the binary quite large).
  • Compress your binary (kkrunchy for 64k intros on Windows, Crinkler for 4k intros on Windows).

Now, you can write a ton of code before filling the 64kB. How to use them? Procedural generation.

  • For music, music sheet is compressed. Instruments are generated with a soft synth. A popular option, although a bit outdated, is to use v2 by Farbrausch.
  • If you need textures, generate them.
  • If you need 3d models, generate them.
  • Animations and effects are procedural.
  • For the camera, save some key positions and interpolate.
  • Shaders are heavily used in modern graphics. Minifying the shaders can save quite a lot of space.

Want to hear more about procedural generation and other techniques? Check IQ's articles.

If you want to further optimise your code, here are some additional tricks:

  • You probably use lots of floats. Try to truncate the mantissa of your floats (it can save many kB).
  • Disable function inlining (it saved me 2kB).
  • Try the fastcall calling convention (it saved me 0.7kB).
  • Disable support for exceptions. You don't need them.
  • If you use classes, avoid inheritance.
  • Be careful if you use templates.

In a typical 4k intro, the C++ code is used for the music and the initialisation. Graphics are done in a shader.

Agenesis answered 24/4, 2017 at 21:16 Comment(0)
I
11

Those demos do not use the standard library (not C++ and not even the C standard lib), nor do they link with standard libraries (to avoid import table sizes). They dynamically link only the absolute minimum necessary.
The demo's "main function" is usually identical with the entry point (unlike in a normal program where the entry point is a CRT init function which does some OS-specific setup, initializes globals, runs constructors, and eventually calls main).

Usually the demo executables are not compliant with the specifications (omitting minimum section sizes and alignments) of the executable format and are compressed with an exe-packer. Technically, these are "broken" programs, but they are just "broken" so much that they still run successfully.

Also, such demos rely heavily on procedurally generated content.

Isometric answered 9/2, 2014 at 21:5 Comment(3)
"They dynamically link only the absolute minimum necessary.": I think you meant "statically".Sheela
@JulienGuertault: No, they do not link anything statically at all, that would be forbidding. They link dynamically, but the absolute bare minimum. IATs/thunks take space, so you want to have as few as absolutely possible, even if that means the program is not standards-compliant (as long as it "works" in the sense of not crashing and producing an image/animation, that is fine).Isometric
You don't really have a choice: if you want to make a win32 program, you need to statically link a few libraries, if you want to use OpenGL too. The trick is to link as few of them as possible (by rewriting what you need, like the CRT for example), and use as little as possible of the ones linked, while aggressively remove code that is not used. Some of those libraries do dynamic link system DLLs behind the scene though, but that's about the extent allowed by competition rules anyway.Sheela
P
3

These ultra-small programs typically don't depend on any libraries or frameworks, as is typical with traditional application development. These programs typically accesses graphics/io, etc. directly.

Pasqualepasqueflower answered 9/2, 2014 at 20:50 Comment(8)
Well, you've got that wrong. Windows demos use OpenGL or DirectX.Verdha
@user1095108: Some do, some do not.Playpen
@Verdha But the OpenGl/DirectX code resides in a DLL instead of a library that has to be linked in (and therefore would add to the file size)Pasqualepasqueflower
you wrote "typically don't depend on any libraries or frameworks" and DLL stands for Dynamic Link Library. Check this if you don't believe me en.wikipedia.org/wiki/Dynamic-link_libraryVerdha
@user1095108: Ok, ok, when I wrote "library" I meant a statically linked .lib file. Apart from the code to invoke its functions, a DLL does not add to the size of a program that uses it.Pasqualepasqueflower
Even this is not true, the program needs to specify the symbols it imports in some way (by name/ordinal).Verdha
@Verdha That's part of the "code to invoke its functions" -- a trivial number of bytes compared to the size of the DLLPasqualepasqueflower
DOS .com demos typically directly access VGA memory in the 16-bit virtual machine they run in. But otherwise no, Windows .exe or Linux executable demos would use DLL or shared library calls, because there is no portable hardware present on every system, and Windows wouldn't let you access it anyway. The only kind of "directly" possible would be manually inlining syscall or Windows int 2Eh / Linux int 0x80 system calls, but that probably takes more code size than calling into a shared library. (Especially under Linux to implement the X11 protocol over a socket.)Spidery
L
1

I can't comment yet because I don't have 50 rep points, so I'm answering.

One way to create a smaller program is to use an older compiler, such as Microsoft Visual C/C++ 4.0, which produces a smaller .exe file than say Microsoft Visual Studio 2005.

Lipography answered 9/2, 2014 at 21:13 Comment(0)
B
1

It really depends on your environment, but if you don't instantiate any templates, and you link everything dynamically, it's fairly easy to achieve a very small size for your executable, since none of the code you actually execute will be in the executable.

Broomstick answered 9/2, 2014 at 22:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.