Optimize inserts in vector and refactor const reference objects (#659)

Signed-off-by: German Semenov <GermanAizek@yandex.ru>
Co-authored-by: ArthurSonzogni <sonzogniarthur@gmail.com>
This commit is contained in:
Herman Semenov
2023-05-31 20:24:08 +03:00
committed by GitHub
parent 8bea9261bc
commit d464a071da
10 changed files with 21 additions and 4 deletions

View File

@@ -98,6 +98,7 @@ class VerticalContainer : public ContainerBase {
Element Render() override {
Elements elements;
elements.reserve(children_.size());
for (auto& it : children_) {
elements.push_back(it->Render());
}
@@ -180,6 +181,7 @@ class HorizontalContainer : public ContainerBase {
Element Render() override {
Elements elements;
elements.reserve(children_.size());
for (auto& it : children_) {
elements.push_back(it->Render());
}

View File

@@ -117,7 +117,7 @@ class InputBase : public ComponentBase {
}
Elements elements;
std::vector<std::string> lines = Split(*content_);
const std::vector<std::string> lines = Split(*content_);
int& cursor_position = option_->cursor_position();
cursor_position = util::clamp(cursor_position, 0, (int)content_->size());
@@ -138,6 +138,7 @@ class InputBase : public ComponentBase {
elements.push_back(text("") | focused);
}
elements.reserve(lines.size());
for (size_t i = 0; i < lines.size(); ++i) {
const std::string& line = lines[i];

View File

@@ -27,7 +27,7 @@ namespace {
Element DefaultOptionTransform(const EntryState& state) {
std::string label = (state.active ? "> " : " ") + state.label; // NOLINT
Element e = text(label);
Element e = text(std::move(label));
if (state.focused) {
e = e | inverted;
}
@@ -114,6 +114,7 @@ class MenuBase : public ComponentBase {
if (option_->elements_prefix) {
elements.push_back(option_->elements_prefix());
}
elements.reserve(size());
for (int i = 0; i < size(); ++i) {
if (i != 0 && option_->elements_infix) {
elements.push_back(option_->elements_infix());
@@ -362,7 +363,10 @@ class MenuBase : public ComponentBase {
animator_background_.clear();
animator_foreground_.clear();
for (int i = 0; i < size(); ++i) {
const int len = size();
animator_background_.reserve(len);
animator_foreground_.reserve(len);
for (int i = 0; i < len; ++i) {
animation_background_[i] = 0.F;
animation_foreground_[i] = 0.F;
animator_background_.emplace_back(&animation_background_[i], 0.F,

View File

@@ -33,6 +33,7 @@ class RadioboxBase : public ComponentBase {
Clamp();
Elements elements;
const bool is_menu_focused = Focused();
elements.reserve(size());
for (int i = 0; i < size(); ++i) {
const bool is_focused = (focused_entry() == i) && is_menu_focused;
const bool is_selected = (hovered_ == i);