diff mbox series

Let std::unordered_map::operator[] insert keys.

Message ID 20200505181618.238776-1-gprocida@google.com
State Committed
Headers show
Series Let std::unordered_map::operator[] insert keys. | expand

Commit Message

Giuliano Procida May 5, 2020, 6:16 p.m. UTC
This patch replaces some verbose code with one-liners that rely on
std::unordered_map::operator[] to insert a missing key and return a
reference to the default-constructed inserted value.

	* src/abg-reader.cc (read_context::key_type_decl): Rely on
	std::unordered_map::operator[] to create map entries if they
	are missing. (build_elf_symbol_db): Ditto.

Signed-off-by: Giuliano Procida <gprocida@google.com>
---
 src/abg-reader.cc | 22 ++--------------------
 1 file changed, 2 insertions(+), 20 deletions(-)

Comments

Matthias Maennich May 13, 2020, 8:07 a.m. UTC | #1
On Tue, May 05, 2020 at 07:16:18PM +0100, Android Kernel Team wrote:
>This patch replaces some verbose code with one-liners that rely on
>std::unordered_map::operator[] to insert a missing key and return a
>reference to the default-constructed inserted value.
>
>	* src/abg-reader.cc (read_context::key_type_decl): Rely on
>	std::unordered_map::operator[] to create map entries if they
>	are missing. (build_elf_symbol_db): Ditto.
>
>Signed-off-by: Giuliano Procida <gprocida@google.com>

Reviewed-by: Matthias Maennich <maennich@google.com>

Cheers,
Matthias

>---
> src/abg-reader.cc | 22 ++--------------------
> 1 file changed, 2 insertions(+), 20 deletions(-)
>
>diff --git a/src/abg-reader.cc b/src/abg-reader.cc
>index 5583a31c..69f7c311 100644
>--- a/src/abg-reader.cc
>+++ b/src/abg-reader.cc
>@@ -556,17 +556,7 @@ public:
>     if (!type)
>       return false;
>
>-    type_base_sptr t = type;
>-
>-    types_map_it i = m_types_map.find(id);
>-    if (i != m_types_map.end())
>-      i->second.push_back(type);
>-    else
>-      {
>-	vector<type_base_sptr> types;
>-	types.push_back(type);
>-	m_types_map[id] = types;
>-      }
>+    m_types_map[id].push_back(type);
>
>     return true;
>   }
>@@ -2882,15 +2872,7 @@ build_elf_symbol_db(read_context& ctxt,
>   for (string_elf_symbol_sptr_map_type::const_iterator i = id_sym_map.begin();
>        i != id_sym_map.end();
>        ++i)
>-    {
>-      it = map->find(i->second->get_name());
>-      if (it == map->end())
>-	{
>-	  (*map)[i->second->get_name()] = elf_symbols();
>-	  it = map->find(i->second->get_name());
>-	}
>-      it->second.push_back(i->second);
>-    }
>+    (*map)[i->second->get_name()].push_back(i->second);
>
>   // Now build the alias relations
>   for (xml_node_ptr_elf_symbol_sptr_map_type::const_iterator x =
>-- 
>2.26.2.526.g744177e7f7-goog
>
>
Dodji Seketeli May 13, 2020, 8:54 a.m. UTC | #2
Giuliano Procida <gprocida@google.com> a écrit:

[...]

> diff --git a/src/abg-reader.cc b/src/abg-reader.cc
> index 5583a31c..69f7c311 100644
> --- a/src/abg-reader.cc
> +++ b/src/abg-reader.cc
> @@ -556,17 +556,7 @@ public:
>      if (!type)
>        return false;
>  
> -    type_base_sptr t = type;
> -
> -    types_map_it i = m_types_map.find(id);
> -    if (i != m_types_map.end())
> -      i->second.push_back(type);
> -    else
> -      {
> -	vector<type_base_sptr> types;
> -	types.push_back(type);
> -	m_types_map[id] = types;
> -      }
> +    m_types_map[id].push_back(type);

Pfff.  These dates from when I was busily adding traces to the code to
profile things to understand types volumetry and whatnot.  I guess I
just forgot to clean that up when I was done.

Thanks for catching that!

[...]

> This patch replaces some verbose code with one-liners that rely on
> std::unordered_map::operator[] to insert a missing key and return a
> reference to the default-constructed inserted value.
>
> 	* src/abg-reader.cc (read_context::key_type_decl): Rely on
> 	std::unordered_map::operator[] to create map entries if they
> 	are missing. (build_elf_symbol_db): Ditto.
>
> Signed-off-by: Giuliano Procida <gprocida@google.com>
Acked-by: Dodji Seketeli <dodji@seketeli.org>

Applied to master, thanks.

Cheers,
diff mbox series

Patch

diff --git a/src/abg-reader.cc b/src/abg-reader.cc
index 5583a31c..69f7c311 100644
--- a/src/abg-reader.cc
+++ b/src/abg-reader.cc
@@ -556,17 +556,7 @@  public:
     if (!type)
       return false;
 
-    type_base_sptr t = type;
-
-    types_map_it i = m_types_map.find(id);
-    if (i != m_types_map.end())
-      i->second.push_back(type);
-    else
-      {
-	vector<type_base_sptr> types;
-	types.push_back(type);
-	m_types_map[id] = types;
-      }
+    m_types_map[id].push_back(type);
 
     return true;
   }
@@ -2882,15 +2872,7 @@  build_elf_symbol_db(read_context& ctxt,
   for (string_elf_symbol_sptr_map_type::const_iterator i = id_sym_map.begin();
        i != id_sym_map.end();
        ++i)
-    {
-      it = map->find(i->second->get_name());
-      if (it == map->end())
-	{
-	  (*map)[i->second->get_name()] = elf_symbols();
-	  it = map->find(i->second->get_name());
-	}
-      it->second.push_back(i->second);
-    }
+    (*map)[i->second->get_name()].push_back(i->second);
 
   // Now build the alias relations
   for (xml_node_ptr_elf_symbol_sptr_map_type::const_iterator x =