Avoid extra look up in ScopedSetInsertion.

Store an iterator to the inserted item instead of a copy of the item.

Bug: chromium:1207425
Change-Id: I08b1d56af38ba87763bba8631212cc1bbca39506
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/80270
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/third_party/base/stl_util.h b/third_party/base/stl_util.h
index cefd0e3..17fdcd8 100644
--- a/third_party/base/stl_util.h
+++ b/third_party/base/stl_util.h
@@ -10,8 +10,10 @@
 #include <memory>
 #include <set>
 #include <type_traits>
+#include <utility>
 #include <vector>
 
+#include "third_party/base/check.h"
 #include "third_party/base/numerics/safe_conversions.h"
 #include "third_party/base/numerics/safe_math.h"
 #include "third_party/base/template_util.h"
@@ -139,15 +141,15 @@
 template <typename T>
 class ScopedSetInsertion {
  public:
-  ScopedSetInsertion(std::set<T>* org_set, T elem)
-      : m_Set(org_set), m_Entry(elem) {
-    m_Set->insert(m_Entry);
+  ScopedSetInsertion(std::set<T>* org_set, const T& elem)
+      : set_(org_set), insert_results_(set_->insert(elem)) {
+    CHECK(insert_results_.second);
   }
-  ~ScopedSetInsertion() { m_Set->erase(m_Entry); }
+  ~ScopedSetInsertion() { set_->erase(insert_results_.first); }
 
  private:
-  std::set<T>* const m_Set;
-  const T m_Entry;
+  std::set<T>* const set_;
+  const std::pair<typename std::set<T>::iterator, bool> insert_results_;
 };
 
 // std::clamp(), some day.