[PATCH][RFC] Make iterating over hash-map elide copying/destructing

Richard Biener rguenther@suse.de
Tue Jul 10 08:43:00 GMT 2018


The following makes the hash-map iterator dereference return a pair<Key, 
Value&> rather than a copy of Value.  This matches the hash-table iterator
behavior and avoids issues with

  hash_map<tree, auto_vec<..., 2> >

where iterating over the hash-table will call the auto_vec destructor
when dereferencing the iterator.  I note that the copy ctor of
auto_vec should probably be deleted and the hash-table/map iterators
should possibly support an alternate "reference" type to the stored
Values so we can use vec<> for "references" and auto_vec<> for
stored members.

But that's out of scope - the patch below seems to survive minimal
testing at least.

I suppose we still want to somehow hide the copy ctors of auto_vec?
How does hash-map growth work here?  (I suppose it doesn't...?)

Any further comments?

Thanks,
Richard.

2018-07-10  Richard Biener  <rguenther@suse.de>

	* hash-map.h (hash_map::iterator::operator*): Return
	a reference to Value.

diff --git a/gcc/hash-map.h b/gcc/hash-map.h
index 7861440f3b3..9d2b38a843e 100644
--- a/gcc/hash-map.h
+++ b/gcc/hash-map.h
@@ -223,10 +223,10 @@ public:
       return *this;
     }
 
-    std::pair<Key, Value> operator* ()
+    std::pair<Key, Value&> operator* ()
     {
       hash_entry &e = *m_iter;
-      return std::pair<Key, Value> (e.m_key, e.m_value);
+      return std::pair<Key, Value&> (e.m_key, e.m_value);
     }
 
     bool



More information about the Gcc-patches mailing list