r/cpp • • Feb 03 '19

A criticism of std::embed (D1040)

http://stonecode.ca/posts/std_embed/
1 Upvotes

10 comments sorted by

39

u/[deleted] Feb 03 '19 edited Feb 03 '19

I'm the person who wrote std::embed and the paper seems a lot fatter than I had ever originally intended ("This will be a good first paper, since it should be so easy to do..." HAAAAAAAAAH famous last words), and as the author it was my job to make sure I took everyone's feedback into account and all the corners of the industry.

There are several problems with the criticism:

  1. C++ compilers are trash at processing large binary files as array literals. Start here, work through the thread. https://twitter.com/oe1cxw/status/1014330034142654464 Because of VC++'s string literal limit, portability is also ruined despite the person finding the "fast" way to write it for C++. Another library's example -- noninus, the benchmarking framework -- is also linked from the paper, demonstrating this, but maybe I should just add this tweet directly and the Article's own admission that they get larger compile times for linear increases in file sizes. g++, clang++, edg, and vc++ all chew through 5+ MB files no problem in seconds that are actual C++ code (just run the preprocessor on some simple C++ programs and look at the massiveness of what comes out: why are we paying 2+ minutes for asset files when compilers eat much bigger files as appetizers??).

  2. That simple makefile is entirely deceptive: anything more complicated than the most basic processing is much more involved than that. Also, portability: write the same thing in CMake, without relying on file2c or some non-portable, non-globally-installed software utility. (Admittedly, python would probably be simpler, which is what MongoDB settled on.)

  3. The paper already discussed the "ld" solution that is referenced in the article, which creates an object file (thanks again to Arvid Gerstmann for pointing this out to me). It doesn't require 200+ lines of LLVM-ish code, but do look at the Appendix for it and behold the sheer ugliness required to make it cross-platform. Also, it removes the ability to optimize algorithms with constexpr data, and depends solely on Link Time Optimization / Link-Time Code Generation. I am not interested in depending on High Compiler Arcanery to know my algorithm has been optimized, when constexpr and consteval gives me in-language, non-compiler-specific guarantees based on the simplest optimization ever: constant folding.

  4. incbin is not cross platform and requires an additional build-step for VC++.

Why is this status quo acceptable, exactly? Nothing in the article shows me that assets can be handled in a proper, cross-platform manner without giving up either constexpr-processing, losing optimization opportunities, or thrashing compile-times.

I appreciate the criticism, but it's wholly unconvincing. I will continue to push for std::embed in the C++ Standard.

13

u/[deleted] Feb 03 '19

Oh, right. I almost forgot about dependencies:

it's not that big of an issue to start with, and I already have a paper to solve that problem in a cross-platform and scalable manner that clues the build system in on what is happening: https://thephd.github.io/vendor/future_cxx/papers/d1130.html

Unfortunately, there's no way this is getting into C++20. The chances of p1130 surviving EWG -- right now -- are pretty slim, so I need to actually implement it before anything moves forward on that front, unless the Modules Gurus™ bless my paper.

I don't see that happening...!

1

u/siplasma Feb 03 '19

(Saw you talk on sol at cppcon, and really enjoyed it)

  1. Agreed on the file size limit. I measured similar numbers, which were included in the article. However, there still isn't an answer about whether this is a matter of optimizing the compilers, or if C++ compilers are the best tool for compiling binary files into an object file.

  2. Not clear why implementing the build chain in CMake will be more difficult than the example. You are right that whatever the approach, it will require addressing portability. It is worth noting that the solutions cited are fairly small, so you can see how they adressed portability. I suspect that you would need to be very familiar with the internals of gcc or clang to get the same appreciation. That said, the compilers already have abstractions to deal with these issues, and std::embed could leverage that. 3 + 4. I feel like you are being selective about where you draw your boundaries. As an example, there is some equivocation between ld the tool and ld the approach. I will just refer back to the previous approach.

3

u/BadlyCamouflagedKiwi Feb 03 '19

First - thanks for writing the proposal! I've used binary embedding before and set it up at my current company (for Go as well as C++, which is a slightly different set of problems) and I suspect a lot of people are missing out because they don't know they can do this, or don't know how to make it work.

Fully agreed on your first point; the output of xxd is expanded by a factor of 6, which is fine for a lot of cases (embedding things like certificates or single small images), but for large files that's unacceptable. We have a binary right now with 280MB of embedded data, generating a 1.6GB C file and forcing a compiler to parse it would be unacceptably slow - even if the compiler is fast (which I suspect it won't be enough), just writing a file of that size takes time (and I am not convinced by the argument that it "needs to exist only long enough to be fed to the compiler", intermediate outputs usually do hang around).

constexpr for this stuff is interesting although not something I need right now. Definitely also sympathetic to the cross-platform argument for ld, getting that to work on Linux is OK but doing OSX as well was a pain, and I can only imagine supporting VC++ would be harder still.

The main thing I'm sympathetic to in the article is that describing this via a function call in the source seems a bit unprecedented, and it seems like it might be a bit awkward for the compiler (that it has to know that this particular member of std needs special attention). Definitely happy to see this getting some attention though!

2

u/dodheim Feb 03 '19

and it seems like it might be a bit awkward for the compiler (that it has to know that this particular member of std needs special attention)

std::launder sets precedent for that, as well as any "library" functions that are also compiler intrinsics (e.g. basic math functions).

1

u/anonymous387 Feb 03 '19

This is maybe the first function that g++ -MT has to treat specially, though!

23

u/germandiago Feb 03 '19 edited Feb 03 '19

Hello. I read your article. I think you are totally wrong. I have tried the same feature in D. It is used as import("filename")

​

I save one step when compiling, I do not need to fit in strange ways syntax that is by nature non-c++. This can happen not only with your resource, but with json files you want to embed and constexpr-process and others, for which you need additional steps just to make the syntax match for no return. This features is one of the top small features I want to see. Maybe the other is a for... syntax to expand tuples (like D's foreach/static foreach). I do not think there is a good argument to add a middle step when it can be done without it.

​

Also some data is too big and it generates errors in some compilers as far as I understand. All in all: we need this feature and it is very welcome. My experience in D with the feature is amazingly positive. As for hiding the dependency, I do not think it is an unsolvable problem and there is a first proposal already in wg21 mailing list.

​

​

1

u/siplasma Feb 03 '19

Unless you are building by hand, saving a step doesn't seem significant

I assume that "strange ways syntax" refers to the intermediate C file. Please note that you don't need to read, write, or even look at that file. We don't worry about the readability of object files (.o) either.

Some files are too big. My tests put the limit at approx 10MB and higher, but it depends on the machine. Also, I only checked gcc.

May I ask, are doing constexpr processing on your embedded assets?

1

u/germandiago Feb 03 '19 edited Feb 03 '19

I had to use resources in a small game I coded. So I started using bash with xxd. It did its job for a while, later had to port to Windows.

Because I did not want to introduce a dependency such as Python or Bash, I run my own resource maker. Written in C++, like the game. This took me an hour and a half to debug.Later I had to check also how it did in Windows. Total time of checking + building my tool: bugs + hours of script code (modulo bugs) + compile in windows (besides Mac and Linux) + check. Whole afternoon.

With std::embed it would have been a couple of minutes.

​

You are contradicting yourself about not winning anything when you say "I just checked in GCC".

My point here is that if you have it supported, you do not need to go through any of these troubles. It takes more time than it looks to just run your own resource checker (+ debug it) + check it in a multi-platform setup. I really think that you are just partial in your assessment, you are not looking at the whole picture. Standard C++ is about portability also. And some of us do need it.

I am not doing constexpr processing but I already have in mind a use-case, so I will, eventually.

0

u/siplasma Feb 05 '19

> I really think that you are just partial in your assessment, you are not looking at the whole picture.

​

Funny, I felt the same way. Everyone is pretty focused on *this* as the solution.