[llvm-dev] Trying to use unordered_map

Paul C. Anagnostopoulos via llvm-dev llvm-dev at lists.llvm.org
Mon Nov 30 13:26:32 PST 2020


The RecordVals are copied into the SmallVector.  Of course! I love this crowd.

Eventually I hope to eliminate the SmallVector and just use the map. But I have both while I experiment with the map to see if it speeds up TableGen. So for now I will change the map so it maps the name Init to the vector index. Then when it's time for a real implementation, I will use the never-before-known-to-me MapVector, since I do need the the fields in order for certain applications.

Thanks, folks!


At 11/30/2020 03:35 PM, David Blaikie wrote:
>Yep, as Krzysztof mentioned - pointer invalidation when inserting elements into a SmallVector (std::vector has similar guarantees here). Adding new elements can require a reallocation - moving all the objects over into the new allocation, so any pointers pointing to the original elements would be invalid. (though, yeah, before you get to that point - you already have dangling references because you're pointing to the RV temporary, uinstead of the copy of RV in the vector)
>
>You could use indexes as Krzysztof mentioned - other options include using non-invalidating data structures (like std::list or std::deque) or indirection (a SmallVector of unique_ptr<RecordVal> - so that pointers remain stable/valid) (though both those solutions involve extra memory/allocation overhead) or putting the RV in the map and pointing to it from the vector (unordered_map has pointer stability through insertion and removal - though llvm's DenseMap does not have such a guarantee). Or maybe llvm's MapVector would abstract you from the complexities of all these options?



More information about the llvm-dev mailing list