141 points · by jandeboevrie · 3y ago · 32 comments
Yep. One reason why giving out pointers/references all willynilly to items in something like a std::vector can be fraught with peril. If you push to that vector, it could trigger a realloc of all of the previous items, leaving you with dangling pointers to everything you previously took references of.
It is much better to use indices or keys instead of pointers since that is the interface to the data structure.
One way we tried to isolate the bug was creating a replacement for std::vector. This vector used an array for the elements, but capitalized on the x86 page-protection system:
- Between every pair of adjacent elements, this vector allocated a dummy page, marked as un-readable and un-writeable. Any attempt to access it would immediately trigger a segfault. This was to detect erroneous attempts to access some vector element; e.g. punning it to a type larger than the element's actual type.
- Any time the vector needed to replace (or just retire) its internal array, it would not return the array to the free pool. Instead, it would leave it allocated, but mark all of its elements as un-readable and un-writeable. This was to detect usage of stale pointers into the vector.
I think the team discovered the bug by other means, but I still think this approach has some merit.
TL;DR:
- The basic idea was inspired by Electric Fence.
- This vector wasn't exactly a drop-in replacement for std::vector, because (at the time) it seemed like that would require a lot of additional code.
- Obviously this wastes a lot of memory. It's only something one would use in certain debugging situations.
(Sorry about the suboptimal formatting. It appears that web technologies aren't up-to-par when it comes to this type of content. I intend to rewrite this post in TeX and add some graphs in TikZ when I find a spare moment.)
I'm struggling to figure this one out. Essentially every two elements would be stored at the start and end of a page, respectively? Followed by a dummy page?
8KB per pair of elements I guess, hope your vectors weren't too large :p
We are trying a database like approach for retrieving references to things in our current project. It adds a small performance penalty, but it’s far from a bottleneck and it’s been nice to work with
Would you want to work in a way where performance didn't matter?
How could you tell the difference between the following two scenarios:
- A specific kind of higher performance was important.
- Higher performance wasn't important: you'd get the same reviews, sell the same copies, whatever graphics improvements you got didn't matter, and maybe you even finish your game 2x sooner because you weren't writing an engine, all else being equal. But because it was intellectually stimulating to you to deal with performance, you committed code every day, which is better than nothing.
It’s frankly amazing to work with a production grade engine that compiles in three minutes. I wouldn’t trade it for all the complexity in unity, regardless of how many extra copies I’d sell. But I realize I’m in the minority.
I think it matters to love the tech stack you use, and gamedev has gone out of fashion mostly because no one loves any of the stacks. They deal with them, which is different.
Come to think of it, std::shared_ptr has a control block that points to the actual data. I think implementations allocate the control block and the data contiguously for performance reasons, but you don’t really have to.
I don't pay attention much the process or politics, but allocator_api feels like it is stuck in permanent limbo. https://github.com/rust-lang/rust/issues/32838 -- opened 6 years ago, still pending.
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p26...
There are some issues with PMR usage that it hopes to resolve, for example:
struct Aggregate {
std::pmr::string data1;
std::pmr::string data2;
std::pmr::string data3;
};
std::pmr::polymorphic_allocator a1;
Aggregate ag = {{"Hello", a1}, {"World", a1}, {"!", a1}};
std::pmr::vector<Aggregate> va(a1);
va.emplace_back(std::move(ag)); // Correct allocator retained by moves
va.emplace_back(ag); // Error, copied lvalue has wrong allocator
va.resize(5); // Error, new values have wrong allocator
va.resize(1); // OK, remove all objects with bad allocators
The proposed syntax is via a "using" keyword: std::pmr::polymorphic_allocator a4;
ScopeAggregate s1 using a4 {"Hello"};When I worked in the group writing the Bloomberg Enterprise backbone we would go out of our way to avoid using the BDE/BSL library. OP is right about it not working the same way as ordinary C++. :-(
You need only disable the "bsl overrides std" build option. Whether you can do that is another question.