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 <islavaivanov76@gmail.com>
This commit is contained in:
Vyacheslav Ivanov 2024-08-02 02:13:40 +03:00 committed by Pugemon
parent 1bdc9cf759
commit f3f872c7a3
No known key found for this signature in database
GPG Key ID: 472FA343B3CC3287
6 changed files with 8 additions and 8 deletions

View File

@ -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);

View File

@ -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) {

View File

@ -286,7 +286,7 @@ static std::shared_ptr<UINode> readButton(UiXmlReader& reader, const xml::xmlele
std::shared_ptr<Button> button;
auto& elements = element->getElements();
if (!elements.empty() && elements.at(0)->getTag() != "#") {
if (!elements.empty() && elements[0]->getTag() != "#") {
auto inner = reader.readUINode(element->getElements().at(0));
if (inner != nullptr) {
button = std::make_shared<Button>(inner, padding);

View File

@ -75,7 +75,7 @@ static int l_get_command_info(lua::State* L) {
lua::createtable(L, args.size(), 0);
for (size_t i = 0; i < args.size(); i++) {
auto& arg = args.at(i);
auto& arg = args[i];
lua::createtable(L, 0, 2);
lua::pushstring(L, arg.name);

View File

@ -95,7 +95,7 @@ static int l_pack_get_info(lua::State* L, const ContentPack& pack, const Content
if (!pack.dependencies.empty()) {
lua::createtable(L, pack.dependencies.size(), 0);
for (size_t i = 0; i < pack.dependencies.size(); i++) {
auto& dpack = pack.dependencies.at(i);
auto& dpack = pack.dependencies[i];
std::string prefix;
switch (dpack.level) {
case DependencyLevel::required: prefix = "!"; break;
@ -142,7 +142,7 @@ static int l_pack_get_info(lua::State* L) {
manager.scan();
auto vec = manager.getAll({packid});
if (!vec.empty()) {
return l_pack_get_info(L, vec.at(0), content);
return l_pack_get_info(L, vec[0], content);
}
return 0;
}

View File

@ -7,7 +7,7 @@ std::string util::to_string(const std::vector<std::string>& vec) {
std::stringstream ss;
ss << "[";
for (size_t i = 0; i < vec.size(); i++) {
ss << util::quote(vec.at(i));
ss << util::quote(vec[1]);
if (i < vec.size()-1) {
ss << ", ";
}