From 8c5e5559ecbdc9c14b82f80bc89474803fa01588 Mon Sep 17 00:00:00 2001 From: Vyacheslav Ivanov Date: Fri, 2 Aug 2024 02:13:40 +0300 Subject: [PATCH] fix: optimization: PVS-Studio warning V831 Replaced 'at()' method with 'operator[]' to improve performance. The 'at()' method performs bounds checking, which can introduce overhead. Using 'operator[]' bypasses this check and can improve performance when you are certain that the index is within bounds. Reported by: PVS-Studio Signed-off-by: Vyacheslav Ivanov --- src/audio/audio.cpp | 2 +- src/graphics/ui/elements/Label.cpp | 4 ++-- src/graphics/ui/gui_xml.cpp | 2 +- src/logic/scripting/lua/libconsole.cpp | 2 +- src/logic/scripting/lua/libpack.cpp | 4 ++-- src/util/listutil.cpp | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/audio/audio.cpp b/src/audio/audio.cpp index 56887994..384a54a9 100644 --- a/src/audio/audio.cpp +++ b/src/audio/audio.cpp @@ -253,7 +253,7 @@ speakerid_t audio::play( if (!sound->variants.empty()) { size_t index = rand() % (sound->variants.size() + 1); if (index < sound->variants.size()) { - sound = sound->variants.at(index).get(); + sound = sound->variants[index].get(); } } auto speaker_ptr = sound->newInstance(priority, channel); diff --git a/src/graphics/ui/elements/Label.cpp b/src/graphics/ui/elements/Label.cpp index ac31e2a9..e2fd44db 100644 --- a/src/graphics/ui/elements/Label.cpp +++ b/src/graphics/ui/elements/Label.cpp @@ -145,7 +145,7 @@ uint Label::getLineByYOffset(int offset) const { uint Label::getLineByTextIndex(size_t index) const { for (size_t i = 0; i < cache.lines.size(); i++) { - if (cache.lines.at(i).offset > index) { + if (cache.lines[i].offset > index) { return i-1; } } @@ -195,7 +195,7 @@ void Label::draw(const DrawContext* pctx, Assets* assets) { if (multiline) { for (size_t i = 0; i < cache.lines.size(); i++) { - auto& line = cache.lines.at(i); + auto& line = cache.lines[i]; size_t offset = line.offset; std::wstring_view view(text.c_str()+offset, text.length()-offset); if (i < cache.lines.size()-1) { diff --git a/src/graphics/ui/gui_xml.cpp b/src/graphics/ui/gui_xml.cpp index 297ee4ac..dc7fdc59 100644 --- a/src/graphics/ui/gui_xml.cpp +++ b/src/graphics/ui/gui_xml.cpp @@ -286,7 +286,7 @@ static std::shared_ptr readButton(UiXmlReader& reader, const xml::xmlele std::shared_ptr