From 982e86cc87cd859adc3207818220dcd5bb843046 Mon Sep 17 00:00:00 2001 From: zawz Date: Wed, 30 Jun 2021 15:53:38 +0200 Subject: [PATCH] optimize fct minify by counting call occurences --- include/processing.hpp | 4 ++++ include/util.hpp | 11 +++++++++++ src/minify.cpp | 1 + src/processing.cpp | 20 ++++++++++++++------ 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/include/processing.hpp b/include/processing.hpp index e00c832..5f1941f 100644 --- a/include/processing.hpp +++ b/include/processing.hpp @@ -29,6 +29,10 @@ extern set_t m_excluded_var, m_excluded_fct, m_excluded_cmd; extern bool b_gotvar, b_gotfct, b_gotcmd; +// tools +countmap_t combine_maps(countmap_t const& a, countmap_t const& b); +countmap_t combine_common(countmap_t const& a, countmap_t const& b); + /** map get functions (optimizations) **/ diff --git a/include/util.hpp b/include/util.hpp index 0533bd5..bff497a 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -126,6 +126,17 @@ void concat_sets(std::set& a, std::set const& b) } } +template +void exclude_sets(std::set& a, std::set const& b) +{ + for(auto it: b) + { + auto t = a.find(it); + if(t != a.end()) + a.erase(t); + } +} + template bool is_in_vector(T el, std::vector vec) { diff --git a/src/minify.cpp b/src/minify.cpp index 57d976a..2a5e67a 100644 --- a/src/minify.cpp +++ b/src/minify.cpp @@ -315,6 +315,7 @@ void minify_fct(_obj* in, std::regex const& exclude) concat_sets(excluded, unsets); concat_sets(excluded, all_reserved_words); // create mapping + m_fcts = combine_common(m_fcts, m_cmds); fctmap=gen_minimal_map(m_fcts, excluded); // perform replace recurse(r_replace_fct, in, &fctmap); diff --git a/src/processing.cpp b/src/processing.cpp index 7a4e746..df30b57 100644 --- a/src/processing.cpp +++ b/src/processing.cpp @@ -63,12 +63,7 @@ void require_rescan_all() // type tools countmap_t combine_maps(countmap_t const& a, countmap_t const& b) { - countmap_t ret; - for(auto it: a) - { - if(!ret.insert( it ).second) - ret[it.first] += it.second; - } + countmap_t ret = a; for(auto it: b) { if(!ret.insert( it ).second) @@ -77,6 +72,19 @@ countmap_t combine_maps(countmap_t const& a, countmap_t const& b) return ret; } +// add the values of b to a only if they are already present in a +countmap_t combine_common(countmap_t const& a, countmap_t const& b) +{ + countmap_t ret = a; + for(auto it: a) + { + auto t=b.find(it.first); + if(t!=b.end()) + ret[it.first] += t->second; + } + return ret; +} + void list_map(countmap_t const& map) { uint32_t max=0;