by CoolGuySteve · 2y ago · 0 comments
I really wish the std would drop this pretense and focus on C++'s strong point: Continue being the fastest systems language possible.
Everywhere in the std lib you can see compromises that require rewriting substantial portions for any real time application.
Things like: shared_ptr eagerly using atomics whenever possible; std::string allocating; the lack of built-in faster std::allocator replacements like bump allocators, memory pools, etc; no lockless and wait free concurrency primitives; no architecture-aware thread pools or even architecture descriptions; no IPC primitives; etc.
Considering how many C++ developers are working on things like games, high performance server applications, databases, and operating systems, it's just bizarre how inappropriate the standard headers are for these tasks.
Even something trivial like casting bytes off the wire to a packed struct is an exercise in frustration due to aliasing rules that should have been encoded in the type system and invisible to the user.
> Continue being the fastest systems language possible.
If you want to be pedantic -- in theory C++ can never be the fastest systems language possible because of the language's rules about aliasing.You'd need to smatter "_restrict" everywhere to skirt this.
True but the only practical competitor is Rust, and they gain some alias information (mut) and lose other aliasing information (type punning is fully allowed in unsafe code all the time).
That's separate from what I'm referring to. In C++ a float* and an int* can never alias. In Rust f32* and u32* are allowed to. Meaning in a situation where whether they can alias can't be established by provenance (e.g. in a separately compiled function where the compiler at compilation time can't tell where the float and int came from) then C++ is able to use types to rule out aliasing, but Rust cannot.
> I really wish the std would drop this pretense and focus on C++'s strong point: Continue being the fastest systems language possible.
You've essentially described C, not C++. C++ has a different philosophy and makes different trade-offs.
C++ is at least still pretty committed to the you only pay for what you use principle. As far as I know RTTI is the only real exception (corrections welcome), but even RTTI can be disabled in many compilers.
Some of your gripes with the standard library can be addressed with libraries, perhaps from the Boost project.
I'd disagree pretty strongly with that. C is more focused on being relatively simple to implement and backwards compatibility with the past 50 years. (I read a blog post by a C committee member talking about that recently, wish I could find the link)
Just look at the garbage fire which is the standard library. qsort. strtok. rand.
C++ should (in theory at least) be able to match or surpass C for _any_ performance benchmark, because it simply gives you more tools in your toolbox. For example, C is never going to be able to beat std::sort because it can't monomorphize in the compare function.
I'm not saying C++ is perfect either (looking at you unordered_map and regex). But I am saying people look at C with rose colored glasses.
To achieve that, you need stuff like monomorphized templates, actual arrays and slices being distinguished from pointers, well-defined rules on pointers (like Rust's rules about references and mutable references, or strict aliasing in C++), and the freedom to let the compiler reorder structure fields for better alignment and space usage. Runtime polymorphism, like virtual functions in C++, are better as a core language feature rather than something implemented as a struct of function pointers, because that way the compiler can devirtualize some calls.
All of these things, I think most C programmers would be against adding to C. And pointer rules are historically controversial; for example, Dennis Ritchie was against adding noalias to C in 1988[1].
[1]: https://www.yodaiken.com/2021/03/19/dennis-ritchie-on-alias-...
C++ templates allow for declarative nested inlining into a single compilation unit that is extremely difficult to achieve with C macros.
See elsewhere in this thread for discussion of boost, it's not applicable.
C macros are way simpler as they are only text. I still use them alot in C++.
"Zero cost abstraction" is a big fantasy. C++ is the king of hidden control flow and the costs are everywhere.
If the target code is full of virtual function calls, yeah it gets gross. But if you need dynamic dispatch, C isn't going to be any better, you'll just be reinventing vtables by hand. Similarly, resource acquisition and release happens in C too, you just have to do it by hand instead of letting the destructor do it for you.
One ultra gross thing I see all the time in C++ disassembly though: constantly creating copies of a std::string, using them once for a comparison or something trivial, and then throwing them away. Multiple times in the same function. Its the developers fault, they shouldn't be creating new objects, they should be passing a pointer or a string_view or something. Unfortunately, C++ is copy rather than move by default, so its too easy to do this by accident.
Another gross thing? (Not C++ specific) 20 functions in a row that are just a single return instruction. Since functions have to have distinct addresses (that's my understanding at least) the compiler/linker can't easily fold identical functions together. Implementations can optimize as long as everything still works (the "as-if" rule). And I know some compilers do. But in practice, it seems they suck at it. So I get to look at 20 lines of assembly in a row that are just `bl`.
That tool is called Godbolt at gcc.godbolt.org. This is common enough that Godbolt is a verb with a lot of C++ programmers.
Compile time would also be an exception.
For me it mostly looks like a reject pile for a stdlib that I must also reject most of the time.
The alignment of incentives between these library authors and people that use C++ for high performance applications is just off.
It’s also the opposite: a place where possible candidates for the standard get tried out first.
But like you, I rarely use it.
https://www.youtube.com/watch?v=LIb3L4vKZ7U
std::allocator is to allocation what std::vector is to vexation (or: designing allocators that don't not-work)
You and I must have a different definition of "easily "
template<typename T>
struct Foo {
using value_type = T;
Foo() = default;
template<typename U>
constexpr Foo(const Foo <U>&) noexcept {}
T* allocate(std::size_t n) {
if (n > std::numeric_limits<std::size_t>::max() / sizeof(T))
throw std::bad_array_new_length();
auto p = static_cast<T*>(std::malloc(n * sizeof(T)));
if (!p) throw std::bad_alloc();
return p;
}
void deallocate(T* p, std::size_t n) noexcept {
std::free(p);
}
};
And then you go and use it as std::vector<int, Foo<int>> v;
So the complexity is not really in writing the "C++ allocator" but in writing a sufficiently complex memory management logic that will actually make your application/algorithm run faster. And that only you can know given that you're familiar with the memory allocation patterns in your code. C++ allocator is only an interface that allows you to capture that logic and makes it feasible to apply through the code.There's also a lack of traits that can describe what's required of a specialized allocator. For example, std::map only needs to allocate 1 object at a time in practice while std::vector needs to allocate a contiguous count of objects.
You mean you want string_view? It still calls strlen when you instantiate it with a literal though.