From a33b48272b20ca3ca2ae07d78c5021b5e318e559 Mon Sep 17 00:00:00 2001 From: Christian Bauer Date: Sun, 22 Jun 2003 20:22:26 +0000 Subject: [PATCH] faster unarchiving (find_registered_class_info() uses a map instead of a linear search) --- ginac/registrar.cpp | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/ginac/registrar.cpp b/ginac/registrar.cpp index 34c91118..7aee5946 100644 --- a/ginac/registrar.cpp +++ b/ginac/registrar.cpp @@ -21,6 +21,7 @@ */ #include +#include #include #include "registrar.h" @@ -30,26 +31,41 @@ namespace GiNaC { registered_class_info *first_registered_class = NULL; /** Find registered_class_info strucure by class name. */ -static inline registered_class_info *find_registered_class_info(const std::string &class_name) +static const registered_class_info *find_registered_class_info(const std::string &class_name) { - registered_class_info *p = first_registered_class; - while (p) { - if (class_name == p->name) - return p; - p = p->next; + // Use a map for faster lookup. The registered_class_info list doesn't + // change at run-time, so it's sufficient to construct the map once + // on the first trip through this function. + typedef std::map name_map_type; + static name_map_type name_map; + static bool name_map_initialized = false; + + if (!name_map_initialized) { + // Construct map + const registered_class_info *p = first_registered_class; + while (p) { + name_map[p->name] = p; + p = p->next; + } + name_map_initialized = true; } - throw (std::runtime_error("class '" + class_name + "' not registered")); + + name_map_type::const_iterator it = name_map.find(class_name); + if (it == name_map.end()) + throw (std::runtime_error("class '" + class_name + "' not registered")); + else + return it->second; } unsigned int find_tinfo_key(const std::string &class_name) { - registered_class_info *p = find_registered_class_info(class_name); + const registered_class_info *p = find_registered_class_info(class_name); return p->tinfo_key; } unarch_func find_unarch_func(const std::string &class_name) { - registered_class_info *p = find_registered_class_info(class_name); + const registered_class_info *p = find_registered_class_info(class_name); return p->unarchive; } -- 2.47.0