← Back to context

Comment by tialaramex

3 years ago

The deliberate wording expands #embed to C's initializer-list

The major performance benefit is from the as-if rule. The compiler is entitled to do whatever it wants so long as the result is as-if it worked the way the standard describes.

So a decent compiler is going to notice that you're #embed-ing this in a byte array, and conclude it should just shovel the whole file into the array here. If it actually made the bytes into integers, and then parsed them, they would of course still fit in exactly one byte, because they're bytes, so it needn't worry about actually doing that which is expensive.

Does it work if you try to #embed a small file as parameters to a printf() ? Yeah, probably, go try it on Godbolt (this is an option there) but for the small file where that's viable we don't care about the performance benefit. It's just a nice trick.

The problem with the preprocessor often is that it is a language inside another language and the preprocessor is designed to be almost completely agnostic of the language it is embedded in. So there might be subtle ways to use the preprocessor so that implementing as-if becomes very unintuitive. I don't have a good intuition about this case, if this is 100% designed in a way that it can never provoke such subtle side-effects. Basically what might end up happening is that the preprocessor has to learn some part of the C language to decide if such an as-if transformation is possible and then branch to either do it or don't.