tint/resolver: Pre-allocate containers

Reserve the memory for the dependency graph containers.
Reduces expensive heap allocations.

Change-Id: Ia650fcb9f9e461afb8b240c9472a9a478a9968d1
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/98081
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
Ben Clayton 2022-08-03 08:43:15 +00:00 committed by Dawn LUCI CQ
parent 6d66921053
commit 7d04cedce3
2 changed files with 11 additions and 0 deletions

View File

@ -489,6 +489,10 @@ struct DependencyAnalysis {
/// #diagnostics.
/// @returns true if analysis found no errors, otherwise false.
bool Run(const ast::Module& module) {
// Reserve container memory
graph_.resolved_symbols.reserve(module.GlobalDeclarations().Length());
sorted_.reserve(module.GlobalDeclarations().Length());
// Collect all the named globals from the AST module
GatherGlobals(module);

View File

@ -93,6 +93,13 @@ struct UniqueVector {
/// @returns a const reference to the internal vector
operator const std::vector<T>&() const { return vector; }
/// Pre-allocates `count` elements in the vector and set
/// @param count the number of elements to pre-allocate
void reserve(size_t count) {
vector.reserve(count);
set.reserve(count);
}
/// Removes the last element from the vector
/// @returns the popped element
T pop_back() {