From 8929acdc50ec0e1de945c18e38886a049fc3849f Mon Sep 17 00:00:00 2001 From: 915_Nagy_Matyas Date: Wed, 8 Nov 2023 00:39:09 +0200 Subject: [PATCH] Fixed inefficient find in array.cpp and cowdata.h The array's find implementation looped through all elements, even after finding a corresponding element. On one hand this results in finding the last element (which is already available by rfind), but is also ineffient and can cause issues, when a partial element is followed by a complete element (finding the partial one can cause runtime errors) To clarify, I was using a comment indenting algorithm, that indented each comment based on their parent comment's level of indentation. It was known, that the parent comment will be positioned in the array, before any element without the indentation field (that's what I referred to as partial data) but this implementation caused an error --- core/templates/cowdata.h | 10 +++------- core/variant/array.cpp | 10 +++------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/core/templates/cowdata.h b/core/templates/cowdata.h index 46d9797d6c38..cecde4f5bfa3 100644 --- a/core/templates/cowdata.h +++ b/core/templates/cowdata.h @@ -341,20 +341,16 @@ Error CowData::resize(int p_size) { template int CowData::find(const T &p_val, int p_from) const { - int ret = -1; - if (p_from < 0 || size() == 0) { - return ret; + return -1; } for (int i = p_from; i < size(); i++) { if (get(i) == p_val) { - ret = i; - break; + return i; } } - - return ret; + return -1; } template diff --git a/core/variant/array.cpp b/core/variant/array.cpp index ab0315ae3456..e69b2840c296 100644 --- a/core/variant/array.cpp +++ b/core/variant/array.cpp @@ -337,20 +337,16 @@ int Array::find(const Variant &p_value, int p_from) const { Variant value = p_value; ERR_FAIL_COND_V(!_p->typed.validate(value, "find"), -1); - int ret = -1; - if (p_from < 0 || size() == 0) { - return ret; + return -1; } for (int i = p_from; i < size(); i++) { if (StringLikeVariantComparator::compare(_p->array[i], value)) { - ret = i; - break; + return i; } } - - return ret; + return -1; } int Array::rfind(const Variant &p_value, int p_from) const {