refactor: omit redundant map searches (#19929)

* refactor: don't walk maps twice to remove elements

* refactor: don't walk maps twice to read elements

* refactor: don't walk maps twice to insert elements

* refactor: don't walk map 3x on UvTaskRunner timeout

* refactor: more don't-walk-maps-twice cleanup

* fixup! refactor: don't walk maps twice to insert elements

* refactor: don't walk containers twice when erasing

* refactor: omit excess lookups in RemoteObjectFreer
This commit is contained in:
Charles Kerr
2019-08-28 09:39:21 -05:00
committed by GitHub
parent 27ce6a9cd3
commit 987300c97a
14 changed files with 88 additions and 86 deletions

View File

@@ -30,14 +30,22 @@ const base::FilePath::CharType kAsarExtension[] = FILE_PATH_LITERAL(".asar");
std::shared_ptr<Archive> GetOrCreateAsarArchive(const base::FilePath& path) {
if (!g_archive_map_tls.Pointer()->Get())
g_archive_map_tls.Pointer()->Set(new ArchiveMap);
ArchiveMap& archive_map = *g_archive_map_tls.Pointer()->Get();
if (!base::Contains(archive_map, path)) {
std::shared_ptr<Archive> archive(new Archive(path));
if (!archive->Init())
return nullptr;
archive_map[path] = archive;
ArchiveMap& map = *g_archive_map_tls.Pointer()->Get();
// if we have it, return it
const auto lower = map.lower_bound(path);
if (lower != std::end(map) && !map.key_comp()(path, lower->first))
return lower->second;
// if we can create it, return it
auto archive = std::make_shared<Archive>(path);
if (archive->Init()) {
base::TryEmplace(map, lower, path, archive);
return archive;
}
return archive_map[path];
// didn't have it, couldn't create it
return nullptr;
}
void ClearArchives() {