diff options
author | stasoid <stasoid@yahoo.com> | 2022-11-07 22:11:00 +0600 |
---|---|---|
committer | stasoid <stasoid@yahoo.com> | 2022-11-07 22:11:00 +0600 |
commit | 8c7621034f2edadc46a9cf236ddef874953c3939 (patch) | |
tree | 11f9c7db16db51ae2d2bac9c338d5c48c99f3a93 /src/string_id.cpp | |
parent | f843d66fb3c43d8705512e9d8c802cdf09acc6c4 (diff) |
parse_styles optimization: use string_id for CSS property names
decreases parse_styles time by 37% (750 ms -> 468 ms on Obama wiki on my machine)
Diffstat (limited to 'src/string_id.cpp')
-rw-r--r-- | src/string_id.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/string_id.cpp b/src/string_id.cpp new file mode 100644 index 00000000..ffdb4e4e --- /dev/null +++ b/src/string_id.cpp @@ -0,0 +1,45 @@ +#include "html.h" +#include <mutex> +#include <assert.h> + +namespace litehtml +{ + +static std::map<string, string_id> map; +static std::vector<string> array; +static std::mutex mutex; + +static int init() +{ + string_vector names; + split_string(initial_string_ids, names, ","); + for (auto& name : names) + { + trim(name); + assert(name[0] == '_' && name.back() == '_'); + name = name.substr(1, name.size() - 2); // _border_color_ -> border_color + std::replace(name.begin(), name.end(), '_', '-'); // border_color -> border-color + _id(name); // this will create association _border_color_ <-> "border-color" + } + return 0; +} +static int dummy = init(); + +string_id _id(const string& str) +{ + std::lock_guard<std::mutex> lock(mutex); + auto it = map.find(str); + if (it != map.end()) return it->second; + // else: str not found, add it to the array and the map + array.push_back(str); + return map[str] = (string_id)(array.size() - 1); +} + +string _s(string_id id) +{ + std::lock_guard<std::mutex> lock(mutex); + // this may fail with "vector subscript out of range" if litehtml functions are called before main + return array[id]; +} + +} // namespace litehtml
\ No newline at end of file |