From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id DE5633857C45; Thu, 28 Jul 2022 20:44:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DE5633857C45 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Change registry to use less memory X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: 08b8a139c9e8adcb4ec12a8a17e5836b8b5acb63 X-Git-Newrev: 8126c055e453a494a72a44c9cce39fa8ddf14607 Message-Id: <20220728204421.DE5633857C45@sourceware.org> Date: Thu, 28 Jul 2022 20:44:21 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 28 Jul 2022 20:44:22 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D8126c055e453= a494a72a44c9cce39fa8ddf14607 commit 8126c055e453a494a72a44c9cce39fa8ddf14607 Author: Tom Tromey Date: Sat May 28 09:08:47 2022 -0600 Change registry to use less memory =20 The registry code creates "registry_data" objects that hold the free function and the index; then the registry keys refer to this object. However, only the index is really useful, and now that registries have a private implementation, just the index can be stored and we can reduce the memory use of registries a little bit. This also simplifies the code somewhat. Diff: --- gdb/registry.h | 47 ++++++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/gdb/registry.h b/gdb/registry.h index cab9f44e3ba..54aad6b7c4d 100644 --- a/gdb/registry.h +++ b/gdb/registry.h @@ -160,7 +160,7 @@ public: } =20 /* The underlying key. */ - const typename registry::registry_data *m_key; + const unsigned m_key; }; =20 /* Clear all the data associated with this container. This is @@ -168,13 +168,16 @@ public: void clear_registry () { /* Call all the free functions. */ - for (const auto &datum : get_registrations ()) + std::vector ®istrations + =3D get_registrations (); + unsigned last =3D registrations.size (); + for (unsigned i =3D 0; i < last; ++i) { - void *elt =3D m_fields[datum->index]; + void *elt =3D m_fields[i]; if (elt !=3D nullptr) { - datum->free (elt); - m_fields[datum->index] =3D nullptr; + registrations[i] (elt); + m_fields[i] =3D nullptr; } } } @@ -184,50 +187,40 @@ private: /* Registry callbacks have this type. */ typedef void (*registry_data_callback) (void *); =20 - /* The type of a key. */ - struct registry_data - { - unsigned index; - registry_data_callback free; - }; - /* Get a new key for this particular registry. FREE is a callback. When the container object is destroyed, all FREE functions are called. The data associated with the container object is passed to the callback. */ - static const registry_data *new_key (registry_data_callback free) + static unsigned new_key (registry_data_callback free) { - std::unique_ptr result (new registry_data); - std::vector> ®istrations + std::vector ®istrations =3D get_registrations (); - result->index =3D registrations.size (); - result->free =3D free; - registrations.emplace_back (std::move (result)); - return registrations.back ().get (); + unsigned result =3D registrations.size (); + registrations.push_back (free); + return result; } =20 /* Set the datum associated with KEY in this container. */ - void set (const registry_data *key, void *datum) + void set (unsigned key, void *datum) { - m_fields[key->index] =3D datum; + m_fields[key] =3D datum; } =20 /* Fetch the datum associated with KEY in this container. If 'set' has not been called for this key, nullptr is returned. */ - void *get (const registry_data *key) + void *get (unsigned key) { - return m_fields[key->index]; + return m_fields[key]; } =20 /* The data stored in this instance. */ std::vector m_fields; =20 /* Return a reference to the vector of all the registrations that - have been made. We do separate allocations here so that the - addresses are stable and can be used as keys. */ - static std::vector> &get_registrations () + have been made. */ + static std::vector &get_registrations () { - static std::vector> registrations; + static std::vector registrations; return registrations; } };