[llvm-dev] Trying to use unordered_map

Paul C. Anagnostopoulos via llvm-dev llvm-dev at lists.llvm.org
Tue Dec 1 07:50:48 PST 2020


MapVector is exactly what I want, except for one feature. Is there a reason why it does not have a function to return an ArrayRef to the vector, so I can iterate over it without using the class's iterators? It does have a function takeVector() that clears the map and moves the vector.

I realize that modifying the returned vector would cause problems, but is that a reason not to have the function?

I'm asking because TableGen has a function to return the vector of record values so that the caller can iterate over them. This function is used both in TableGen itself and in backends. 


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