From 4af764d040a6352a71fb18ba3f11c9bf46edc4d4 Mon Sep 17 00:00:00 2001 From: Michael Winterberg Date: Sun, 10 Jan 2016 15:30:34 -0800 Subject: [PATCH] Changed ArgMap to be backed by a vector instead of a map. The main reason for this is to avoid a dynamic memory allocation in every format() call with Visual Studio if there are no named arguments. --- format.cc | 6 +++--- format.h | 12 +++++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/format.cc b/format.cc index dc0b7f37..8ad96c7e 100644 --- a/format.cc +++ b/format.cc @@ -616,7 +616,7 @@ void fmt::internal::ArgMap::init(const ArgList &args) { return; case internal::Arg::NAMED_ARG: named_arg = static_cast(args.values_[i].pointer); - map_.insert(Pair(named_arg->name, *named_arg)); + map_.push_back(Pair(named_arg->name, *named_arg)); break; default: /*nothing*/; @@ -628,7 +628,7 @@ void fmt::internal::ArgMap::init(const ArgList &args) { internal::Arg::Type arg_type = args.type(i); if (arg_type == internal::Arg::NAMED_ARG) { named_arg = static_cast(args.args_[i].pointer); - map_.insert(Pair(named_arg->name, *named_arg)); + map_.push_back(Pair(named_arg->name, *named_arg)); } } for (unsigned i = ArgList::MAX_PACKED_ARGS;/*nothing*/; ++i) { @@ -637,7 +637,7 @@ void fmt::internal::ArgMap::init(const ArgList &args) { return; case internal::Arg::NAMED_ARG: named_arg = static_cast(args.args_[i].pointer); - map_.insert(Pair(named_arg->name, *named_arg)); + map_.push_back(Pair(named_arg->name, *named_arg)); break; default: /*nothing*/; diff --git a/format.h b/format.h index 6aa4dbc9..2f15bbb4 100644 --- a/format.h +++ b/format.h @@ -36,7 +36,8 @@ #include #include #include -#include +#include +#include #ifndef FMT_USE_IOSTREAMS # define FMT_USE_IOSTREAMS 1 @@ -1671,7 +1672,7 @@ namespace internal { template class ArgMap { private: - typedef std::map, internal::Arg> MapType; + typedef std::vector, internal::Arg> > MapType; typedef typename MapType::value_type Pair; MapType map_; @@ -1680,7 +1681,12 @@ class ArgMap { FMT_API void init(const ArgList &args); const internal::Arg* find(const fmt::BasicStringRef &name) const { - typename MapType::const_iterator it = map_.find(name); + typename MapType::const_iterator it = map_.begin(); + // the list is unsorted, so just return the first matching name. + for (; it != map_.end(); ++it) { + if (it->first == name) + break; + } return it != map_.end() ? &it->second : 0; } };