r/cpp • u/siplasma • Feb 03 '19
A criticism of std::embed (D1040)
http://stonecode.ca/posts/std_embed/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.
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:
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??).
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.)
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.
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.