Prefer std::map::insert over find and operator[] (#1653)

This avoids a duplicate lookups.
This commit is contained in:
Andrew Gaul 2021-05-09 14:11:35 +09:00 committed by GitHub
parent 4d81a4bf68
commit d904d91252
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -418,12 +418,11 @@ bool StatCache::AddStat(const std::string& key, headers_t& meta, bool forcedir,
// add // add
AutoLock lock(&StatCache::stat_cache_lock); AutoLock lock(&StatCache::stat_cache_lock);
stat_cache_t::iterator iter = stat_cache.find(key); // recheck for same key exists std::pair<stat_cache_t::iterator, bool> pair = stat_cache.insert(std::make_pair(key, ent));
if(stat_cache.end() != iter){ if(!pair.second){
delete iter->second; delete pair.first->second;
stat_cache.erase(iter); pair.first->second = ent;
} }
stat_cache[key] = ent;
// check symbolic link cache // check symbolic link cache
if(!S_ISLNK(ent->stbuf.st_mode)){ if(!S_ISLNK(ent->stbuf.st_mode)){
@ -476,12 +475,11 @@ bool StatCache::AddNoObjectCache(const std::string& key)
// add // add
AutoLock lock(&StatCache::stat_cache_lock); AutoLock lock(&StatCache::stat_cache_lock);
stat_cache_t::iterator iter = stat_cache.find(key); // recheck for same key exists std::pair<stat_cache_t::iterator, bool> pair = stat_cache.insert(std::make_pair(key, ent));
if(stat_cache.end() != iter){ if(!pair.second){
delete iter->second; delete pair.first->second;
stat_cache.erase(iter); pair.first->second = ent;
} }
stat_cache[key] = ent;
// check symbolic link cache // check symbolic link cache
if(symlink_cache.end() != symlink_cache.find(key)){ if(symlink_cache.end() != symlink_cache.find(key)){
@ -671,12 +669,11 @@ bool StatCache::AddSymlink(const std::string& key, const std::string& value)
// add // add
AutoLock lock(&StatCache::stat_cache_lock); AutoLock lock(&StatCache::stat_cache_lock);
symlink_cache_t::iterator iter = symlink_cache.find(key); // recheck for same key exists std::pair<symlink_cache_t::iterator, bool> pair = symlink_cache.insert(std::make_pair(key, ent));
if(symlink_cache.end() != iter){ if(!pair.second){
delete iter->second; delete pair.first->second;
symlink_cache.erase(iter); pair.first->second = ent;
} }
symlink_cache[key] = ent;
return true; return true;
} }