Skip to content

Commit a4110fe

Browse files
lakshaygpytorchmergebot
authored andcommitted
Use insert_or_assign instead of erase+emplace (pytorch#164868)
insert_or_assign does effectively the same thing as erase+emplace but more efficiently since the search does not need to be repeated Pull Request resolved: pytorch#164868 Approved by: https://github.com/eqy
1 parent 37c6087 commit a4110fe

File tree

4 files changed

+4
-8
lines changed

4 files changed

+4
-8
lines changed

aten/src/ATen/native/cudnn/Conv_v8.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,7 @@ struct BenchmarkCache {
337337
engine_cache_order.begin(), engine_cache_order, it->second.second);
338338
}
339339
} else {
340-
engine_cache.erase(key);
341-
engine_cache.emplace(
340+
engine_cache.insert_or_assign(
342341
key,
343342
std::make_pair(results, engine_cache_order.end())); // dummy iterator
344343
}

aten/src/ATen/native/cudnn/MHA.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,7 @@ struct MHAGraphCache {
371371
}
372372

373373
void update(const KeyType& key, T& results) {
374-
engine_cache.erase(key);
375-
engine_cache.emplace(key, std::move(results));
374+
engine_cache.insert_or_assign(key, std::move(results));
376375
}
377376
};
378377

torch/csrc/distributed/c10d/symm_mem/SymmetricMemory.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ static at::Tensor empty_strided_p2p_persistent(
152152
auto allocated = at::from_blob(dev_ptr, size, stride, options);
153153

154154
// Track the allocation's activeness
155-
alloc_id_to_storage.erase(alloc_id);
156-
alloc_id_to_storage.emplace(
155+
alloc_id_to_storage.insert_or_assign(
157156
alloc_id, allocated.storage().getWeakStorageImpl());
158157
return allocated;
159158
}

torch/csrc/profiler/standalone/execution_trace_observer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ struct TORCH_API ExecutionTraceObserver { // NOLINT
138138
// So we need to remove the key and insert the key with the new value.
139139
data_ptr_to_storage_id.erase(raw_data_ptr);
140140
data_ptr_to_storage_id[raw_data_ptr] = id;
141-
data_ptr_to_weak_storage_ptr.erase(raw_data_ptr);
142-
data_ptr_to_weak_storage_ptr.emplace(
141+
data_ptr_to_weak_storage_ptr.insert_or_assign(
143142
raw_data_ptr, t_storage.getWeakStorageImpl());
144143
return id;
145144
} else {

0 commit comments

Comments
 (0)