[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] odd/any_member-opt cdfc8d5 4/5: Allow computing Memb
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] odd/any_member-opt cdfc8d5 4/5: Allow computing MemberSymbolTable member index and reusing it |
Date: |
Sun, 28 Mar 2021 13:53:15 -0400 (EDT) |
branch: odd/any_member-opt
commit cdfc8d5054d675cdf4e562baf62cf5a4a94f3a09
Author: Vadim Zeitlin <vadim@tt-solutions.com>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Allow computing MemberSymbolTable member index and reusing it
Looking up the member by name once and then reusing the index to access
it results in a big performance gain when doing it many times in a row,
e.g. CensusView::column_value_varies_across_cells() now takes less than
30% of the time it took before for a census with ~4000 rows.
---
any_member.hpp | 25 +++++++++++++++++++++----
census_view.cpp | 20 ++++++++++++++++++--
2 files changed, 39 insertions(+), 6 deletions(-)
diff --git a/any_member.hpp b/any_member.hpp
index cdca2c9..b01f6df 100644
--- a/any_member.hpp
+++ b/any_member.hpp
@@ -63,6 +63,7 @@
#include "any_entity.hpp"
#include "assert_lmi.hpp"
+#include "bourn_cast.hpp"
#include "rtti_lmi.hpp"
#include "value_cast.hpp"
@@ -553,6 +554,9 @@ class MemberSymbolTable
bool equals(MemberSymbolTable<ClassType> const&) const;
std::vector<std::string> const& member_names() const;
+ int get_member_index(std::string const&) const;
+ any_member<ClassType> const& get_member_by_index(int) const;
+
protected:
MemberSymbolTable();
@@ -599,16 +603,29 @@ void
MemberSymbolTable<ClassType>::complain_that_no_such_member_is_ascribed
}
template<typename ClassType>
-any_member<ClassType>& MemberSymbolTable<ClassType>::operator[]
- (std::string const& s
- )
+int MemberSymbolTable<ClassType>::get_member_index(std::string const& s) const
{
auto const i = std::lower_bound(member_names_.begin(),
member_names_.end(), s);
if(i == member_names_.end() || (*i != s))
{
complain_that_no_such_member_is_ascribed(s);
}
- return member_values_[i - member_names_.begin()];
+ return bourn_cast<int>(i - member_names_.begin());
+}
+
+template<typename ClassType>
+any_member<ClassType> const&
+MemberSymbolTable<ClassType>::get_member_by_index(int n) const
+{
+ return member_values_[n];
+}
+
+template<typename ClassType>
+any_member<ClassType>& MemberSymbolTable<ClassType>::operator[]
+ (std::string const& s
+ )
+{
+ return member_values_[get_member_index(s)];
}
template<typename ClassType>
diff --git a/census_view.cpp b/census_view.cpp
index 9ee061d..f8ccd3c 100644
--- a/census_view.cpp
+++ b/census_view.cpp
@@ -1077,8 +1077,24 @@ Input*
CensusView::class_parms_from_class_name(std::string const& class_name)
bool CensusView::column_value_varies_across_cells(std::string const& header)
const
{
auto const z = case_parms()[0][header];
- for(auto const& j : class_parms()) {if(z != j[header]) return true;}
- for(auto const& j : cell_parms() ) {if(z != j[header]) return true;}
+
+ auto const class_header_index =
class_parms().begin()->get_member_index(header);
+ for(auto const& j : class_parms())
+ {
+ if(z != j.get_member_by_index(class_header_index))
+ {
+ return true;
+ }
+ }
+
+ auto const cell_header_index =
cell_parms().begin()->get_member_index(header);
+ for(auto const& j : cell_parms())
+ {
+ if(z != j.get_member_by_index(cell_header_index))
+ {
+ return true;
+ }
+ }
return false;
}