Use SkTHashMap for the BuiltinMap.

Change-Id: I6f50d8020c15acf664cdfcb2918c2e547427b838
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/515321
Auto-Submit: John Stiles <johnstiles@google.com>
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
This commit is contained in:
John Stiles 2022-03-03 17:32:35 -05:00 committed by SkCQ
parent 11206c3faf
commit 7957918db3
2 changed files with 15 additions and 15 deletions

View File

@ -12,35 +12,35 @@
namespace SkSL {
void BuiltinMap::insertOrDie(std::string key, std::unique_ptr<ProgramElement> element) {
SkASSERT(fElements.find(key) == fElements.end());
fElements[key] = BuiltinElement{std::move(element), false};
SkASSERT(!fElements.find(key));
fElements.set(std::move(key), BuiltinElement{std::move(element), /*fAlreadyIncluded=*/false});
}
const ProgramElement* BuiltinMap::find(const std::string& key) {
auto iter = fElements.find(key);
if (iter == fElements.end()) {
BuiltinElement* elem = fElements.find(key);
if (!elem) {
return fParent ? fParent->find(key) : nullptr;
}
return iter->second.fElement.get();
return elem->fElement.get();
}
// Only returns a builtin element that isn't already marked as included, and then marks it.
const ProgramElement* BuiltinMap::findAndInclude(const std::string& key) {
auto iter = fElements.find(key);
if (iter == fElements.end()) {
BuiltinElement* elem = fElements.find(key);
if (!elem) {
return fParent ? fParent->findAndInclude(key) : nullptr;
}
if (iter->second.fAlreadyIncluded) {
if (elem->fAlreadyIncluded) {
return nullptr;
}
iter->second.fAlreadyIncluded = true;
return iter->second.fElement.get();
elem->fAlreadyIncluded = true;
return elem->fElement.get();
}
void BuiltinMap::resetAlreadyIncluded() {
for (auto& pair : fElements) {
pair.second.fAlreadyIncluded = false;
}
fElements.foreach([](const std::string&, BuiltinElement* elem) {
elem->fAlreadyIncluded = false;
});
if (fParent) {
fParent->resetAlreadyIncluded();
}

View File

@ -9,9 +9,9 @@
#define SKSL_BUILTINMAP
#include "include/private/SkSLString.h"
#include "include/private/SkTHash.h"
#include <memory>
#include <unordered_map>
namespace SkSL {
@ -38,7 +38,7 @@ private:
bool fAlreadyIncluded = false;
};
std::unordered_map<std::string, BuiltinElement> fElements;
SkTHashMap<std::string, BuiltinElement> fElements;
BuiltinMap* fParent = nullptr;
};