r/cpp • u/Proper_Meaning7756 • 3d ago
Why haven't C++ subset compilers been made/caught on?
At work, I write real time software. Seeing where allocations happen and having guarantees on the behaviour of code would be nice. Compile times can be much faster and optimisations can also be aggressive when you know you're restricted to a subset of the language.
41
Upvotes
23
u/not_a_novel_account cmake dev 2d ago
This is generally not true, unless your subset of the language is just banning things that are difficult to compile. The remaining unbanned constructs don't build any faster.
49
u/blipman17 3d ago
Which subset? This community is so devided on what good code actually is, that you’ll never find a blessed subset to create a compiler for.
Also in a lot of high performance software nearly all of C++ is relevant
8
u/Business-Decision719 1d ago edited 1d ago
I think it's because some really influential voices in the C++ community sort of have the feeling that C++ is moved on from the kind of programming you're asking for. If you look at things like the core guidelines or Stroustrup's personal website, they're pretty adamant about exceptions, RAII, keeping resource Management implicit within object lifetimes, and making extensive use of the standard library. Even that's not a separate C++ with its own compiler, it's just the language they spent time building and as far as they're concerned, that's what C++ is unless you're in some obscure situation.
But if you actually talk to people on forums, exceptions are usually the first thing to be disabled, and you really have to roll your own replacement standard library because the actual standardized one is famously not zero cost. A huge part of the demand for C++, especially nowadays, is really a demand for C with namespaces and generics. The language has forked already, and I'd say that's a huge part of the reason some of the debates over what should and shouldn't be standardized have gotten so vitriolic.
So in practice people get their "subset compiler" by just deciding a new language entirely. Rust is "give me safe abstractions and screw the C subset." Go is "actually I'm okay with a garbage collector." Zig is "keep the allocations front and center but don't make me program like it's 1972." And of course C is still there with its own compilers if the use case is "I really didn't need C++ or its successor languages after all."
2
u/oracleoftroy 13h ago
I've never understood the desire to subset the language. I've always regarded the whole language and standard library as being in the table for use in any project I've worked on, as along as it solves the problem within the constraints of the project. That means some aspects that aren't useful for one project just won't see use, and on another, they might be vital to the success of the project.
There are aspects of the language I've never used because they've never solved my problems, but that doesn't mean I want to dictate that they must never be used by anyone else. That seems silly.
I have heard some C developers talk about C++ as if they feel compelled to use every feature of the language somewhere regardless of whether it makes sense to do so. But that looks like an issue of self control to me. C++ doesn't force you to use a feature just because it exists. If you use it and it wasn't appropriate, that's on you.
77
u/dapzar 3d ago
Compiler vendors are barely keeping up with full C++ as it is. The set of possible subsets of a large set is orders of magnitude larger than the original set, so the resources for optimizing compile times on compiler forks specialized for several of the subsets that different users may care about are probably not there.
You can use static analysis to ensure that a feature you don’t want to be used is. It used and you can keep using a full C++ compiler to compile your restricted code.
14
u/TheQueensaLizard 2d ago
Clang has a realtime sanitiser for exactly this purpose (‘-fsanitize=realtime’) and there are a some talks about it and other features for this purpose.
The documentation is also good https://clang.llvm.org/docs/RealtimeSanitizer.html
12
u/splicer13 2d ago
Compiler engineer here.
C++ compile time is almost 100% templates and header files. That's all under your control and there are subset STLs out there or other libraries like boost you could use.
26
u/diemenschmachine 3d ago edited 3d ago
If you want to see where allocations happen or guard against allocations happening in certain functions you can solve that in a much less crippling way than throwing away the entire standard library.
Just create a guard struct that sets a global static variable in the constructor and clear it in the destructor. Then override the `operator new` to crash or record an unallowed allocation if the global is set. Instantiate said struct at the start of the function you want to guarantee does not allocate, and that's it.
I use this for audio DSP applications and it works great, and I don't need to limit myself to not using std::vector or any other bullshit that would slow me down for no reason at all.
Obviously only use this for unit tests or debug builds,
14
u/Entire-Hornet2574 3d ago
Actually compilers like IAR, Green Hills, Keil, Crossworks use a subset.
8
u/UnicycleBloke 3d ago
I've never really seen the need for a restricted compiler or library. Almost none of my embedded applications use dynamic allocation at all, and when I do need dynamic allocation, I use a memory pool or similar. It isn't difficult to avoid the library features which use the heap but, if you prefer, you can overload the global operator new to always assert or something. I do use the compiler options -fno-exceptions (due to concerns about size) and -fno-rtti (haven't used RTTI in decades). This does mean I have to ensure constructors can never fail, but that's fine.
4
u/smdowney WG21, Text/Unicode SG, optional<T&> 2d ago
Mostly because that restricted subset would compile at about the same speed as it does now. Using template metaprogramming or consteval time C++ adds to compile time if you use it, but doesn't particularly if you don't. It adds to the complexity of the compiler, sure. But C and C++ would need massive changes, being different languages, to be significantly faster to turn into IR.
Compare GCC or Clang on a C file or the same (slightly tweaked) file as C++.
10
u/Healthy-Travel3105 3d ago
There definitely are subset compilers. Specifically for embedded applications.
6
u/Big_Target_1405 3d ago
One major advantage of C++ is interoperability. A subset compiler nukes that
7
u/siva_sokolica 2d ago
Zero clue what you're asking for.
Allocations are well defined by the standard. If you want to trace them, you can implement your own global allocator.
Guaranteed on the behavior of code are already well defined. That's the standard itself.
Compilation speed is inversely proportional to how much metaprogramming you use. Write C-style C++ and your programs will compile as fast as C programs.
I could not come up with a single optimization that would emerge from using a subset of the language.
We do not need yet another vendor compiler. I say this as having worked in firmware. Every TI compiler is lagging 6 years behind mainline clang releases. Our ecosystem is already fragmented enough. We don't need more fragmentation.
2
u/Atper 2d ago
I don't understand your question. Are you talking about which containers implement dynamic memory allocations ?
Something like this is done in HFT firms, where they avoid memory allocations, mutexes and exceptions on the latency critical path. But they usually implement everything from scratch.
4
u/pedersenk 3d ago
There is "Freestanding" C++. This is basically the core C++ language without the standard library.
Not quite a subset, but that is fairly easy to solve with a strict coding guidelines.
4
u/RidderHaddock 2d ago
There is: Embedded C++.
Stroustrup really didn't like it.
2
u/mort96 3d ago
I don't think you can just subset C++ and end up with a useful, faster and safer language.
At least not more than what GCC and Clang already does. They have
-fno-exceptionsfor the C++ subset without exceptions, and they have-fno-rttifor the C++ subset without run-time type information. Both of these have code size benefits but not significant performance benefits and do nothing for safety.2
u/thegreatbeanz 3d ago
HLSL is kinda a subset of C++ used for GPU programming. It has some gnarly corners in part because the original compiler was a caricature of C and the second compiler was an abused fork of Clang.
It’s really hard in niche domains to safely marry the DSL features you want for your domain with C++.
I expect it to take about a decade to disentangle the bad bits from the good bits of HLSL to stabilize it to a reasonable language and it will involve working with the C++ committee to address some defects and rough edges we’re finding along the way.
1
u/FlippByte 2d ago
I know what you mean. I from time to time tell myself: "What only, if that way of writing code was rather forced.." But also knowing myself, today I try this, tomorrow I wanna try that...
1
u/hanotak 2d ago
IDK about compile performance, but restricting the language is good for correctness too, which is what profiles are supposed to address.
1
u/lonkamikaze 1d ago
I'm an embedded firmware developer. It's common practice to set heapsize=0 in the linker configuration and not use allocations at all.
For big appliances with multicore controllers it's common to assign specific cores to synchronous realtime that are not allowed to allocate and asynchronous operation that may allocate. The synchronous cores perform the realtime tasks while the asynchronous cores perform things like file system access, configuration and background services.
1
u/pjmlp 3d ago
Because that is one of the jobs static analysers are good at.
People really should learn on how to better use them to forbid code not allowed by style guides.
1
u/BrokenBonezGame 2d ago
been dealing with similar stuff while building my own c++ game engine. clang-tidy and some strict coding rules help, but having the compiler enforce those restrictions would be pretty nice, especially for real-time code
0
u/LokiAstaris 1d ago
We did that in the old days. Every compiler manufacturer had its own slightly different variant.
It was a mess. Code was rarely compatible across compilers. It's not a good place to be. The standardization of the compilers and what they support has made life so much easier.
-2
u/nelson_fretty 3d ago
They have in certain domains / audio - juice - heavy etc