From 3ebb0b0b0397bf8e5bfdfc1c3f194926662cdb16 Mon Sep 17 00:00:00 2001
From: patrick96
Date: Fri, 28 Dec 2018 17:10:52 +0100
Subject: [PATCH] builder: Simplify open tag tracking
Using a bit vector to track the active attributes does not really give a
significant speed increase, especially for only two attributes
Checking if a syntaxtag or an attribute exists in the map just adds
unnecessary code
---
include/components/builder.hpp | 2 +-
src/components/builder.cpp | 26 +++++++++++++++-----------
2 files changed, 16 insertions(+), 12 deletions(-)
diff --git a/include/components/builder.hpp b/include/components/builder.hpp
index 39d8f0e0..830e0e14 100644
--- a/include/components/builder.hpp
+++ b/include/components/builder.hpp
@@ -70,8 +70,8 @@ class builder {
map m_tags{};
map m_colors{};
+ map m_attrs{};
- int m_attributes{0};
int m_fontindex{0};
string m_background{};
diff --git a/src/components/builder.cpp b/src/components/builder.cpp
index e30e024f..fa21463f 100644
--- a/src/components/builder.cpp
+++ b/src/components/builder.cpp
@@ -9,6 +9,10 @@
POLYBAR_NS
builder::builder(const bar_settings& bar) : m_bar(bar) {
+ /* Add all values as keys so that we never have to check if a key exists in
+ * the map
+ */
+ m_tags[syntaxtag::NONE] = 0;
m_tags[syntaxtag::A] = 0;
m_tags[syntaxtag::B] = 0;
m_tags[syntaxtag::F] = 0;
@@ -20,6 +24,10 @@ builder::builder(const bar_settings& bar) : m_bar(bar) {
m_colors[syntaxtag::F] = string();
m_colors[syntaxtag::o] = string();
m_colors[syntaxtag::u] = string();
+
+ m_attrs[attribute::NONE] = false;
+ m_attrs[attribute::UNDERLINE] = false;
+ m_attrs[attribute::OVERLINE] = false;
}
/**
@@ -43,10 +51,10 @@ string builder::flush() {
if (m_tags[syntaxtag::u]) {
underline_color_close();
}
- if ((m_attributes >> static_cast(attribute::UNDERLINE)) & 1) {
+ if (m_attrs[attribute::UNDERLINE]) {
underline_close();
}
- if ((m_attributes >> static_cast(attribute::OVERLINE)) & 1) {
+ if (m_attrs[attribute::OVERLINE]) {
overline_close();
}
@@ -561,10 +569,6 @@ string builder::get_label_text(const label_t& label) {
* Insert directive to change value of given tag
*/
void builder::tag_open(syntaxtag tag, const string& value) {
- if (m_tags.find(tag) == m_tags.end()) {
- m_tags[tag] = 0;
- }
-
m_tags[tag]++;
switch (tag) {
@@ -601,11 +605,11 @@ void builder::tag_open(syntaxtag tag, const string& value) {
* Insert directive to use given attribute unless already set
*/
void builder::tag_open(attribute attr) {
- if ((m_attributes >> static_cast(attr)) & 1) {
+ if (m_attrs[attr]) {
return;
}
- m_attributes |= 1 << static_cast(attr);
+ m_attrs[attr] = true;
switch (attr) {
case attribute::NONE:
@@ -623,7 +627,7 @@ void builder::tag_open(attribute attr) {
* Insert directive to reset given tag if it's open and closable
*/
void builder::tag_close(syntaxtag tag) {
- if (m_tags.find(tag) == m_tags.end() || !m_tags[tag]) {
+ if (!m_tags[tag]) {
return;
}
@@ -661,11 +665,11 @@ void builder::tag_close(syntaxtag tag) {
* Insert directive to remove given attribute if set
*/
void builder::tag_close(attribute attr) {
- if (!((m_attributes >> static_cast(attr)) & 1)) {
+ if (!m_attrs[attr]) {
return;
}
- m_attributes &= ~(1 << static_cast(attr));
+ m_attrs[attr] = false;
switch (attr) {
case attribute::NONE: