diff --git a/src/core/ldebug.cpp b/src/core/ldebug.cpp index 761cbd35..6f5e6e98 100644 --- a/src/core/ldebug.cpp +++ b/src/core/ldebug.cpp @@ -10,6 +10,7 @@ #include "lprefix.h" +#include #include #include #include @@ -66,14 +67,18 @@ static int getbaseline (const Proto *f, int pc, int *basepc) { return f->getLineDefined(); } else { - int i = pc / MAXIWTHABS - 1; /* get an estimate */ - /* estimate must be a lower bound of the correct base */ - lua_assert(i < 0 || - (i < f->getAbsLineInfoSize() && f->getAbsLineInfo()[i].getPC() <= pc)); - while (i + 1 < f->getAbsLineInfoSize() && pc >= f->getAbsLineInfo()[i + 1].getPC()) - i++; /* low estimate; adjust it */ - *basepc = f->getAbsLineInfo()[i].getPC(); - return f->getAbsLineInfo()[i].getLine(); + /* Binary search for the last AbsLineInfo with PC <= pc */ + const AbsLineInfo* absLineInfo = f->getAbsLineInfo(); + int size = f->getAbsLineInfoSize(); + /* std::upper_bound finds first element with PC > pc, so we go back one */ + auto it = std::upper_bound(absLineInfo, absLineInfo + size, pc, + [](int target_pc, const AbsLineInfo& info) { + return target_pc < info.getPC(); + }); + lua_assert(it != absLineInfo); /* we know there's at least one element with PC <= pc */ + --it; /* go back to last element with PC <= pc */ + *basepc = it->getPC(); + return it->getLine(); } } @@ -690,13 +695,12 @@ static const char *funcnamefromcall (lua_State *L, CallInfo *ci, ** region boundaries (undefined behavior in ISO C). */ static int instack (CallInfo *ci, const TValue *o) { - int pos; StkId base = ci->funcRef().p + 1; - for (pos = 0; base + pos < ci->topRef().p; pos++) { - if (o == s2v(base + pos)) - return pos; - } - return -1; /* not found */ + StkId end = ci->topRef().p; + auto it = std::find_if(base, end, [o](const StackValue& sv) { + return o == s2v(&sv); + }); + return (it != end) ? static_cast(it - base) : -1; /* return position or -1 if not found */ } diff --git a/src/objects/lstring.cpp b/src/objects/lstring.cpp index caa6d3f3..6fb77dfd 100644 --- a/src/objects/lstring.cpp +++ b/src/objects/lstring.cpp @@ -67,8 +67,9 @@ unsigned luaS_hashlongstr (TString *ts) { static void tablerehash (TString **vect, unsigned int osize, unsigned int nsize) { unsigned int i; - for (i = osize; i < nsize; i++) /* clear new elements */ - vect[i] = NULL; + /* clear new elements (only when growing) */ + if (nsize > osize) + std::fill_n(vect + osize, nsize - osize, nullptr); for (i = 0; i < osize; i++) { /* rehash old part of the array */ TString *p = vect[i]; vect[i] = NULL; diff --git a/src/objects/ltable.cpp b/src/objects/ltable.cpp index de724dd4..70fb0c96 100644 --- a/src/objects/ltable.cpp +++ b/src/objects/ltable.cpp @@ -726,7 +726,6 @@ static void setnodevector (lua_State *L, Table *t, unsigned size) { t->setDummy(); /* signal that it is using dummy node */ } else { - unsigned int i; unsigned int lsize = luaO_ceillog2(size); if (lsize > MAXHBITS || (1u << lsize) > MAXHSIZE) luaG_runerror(L, "table overflow"); @@ -736,7 +735,7 @@ static void setnodevector (lua_State *L, Table *t, unsigned size) { t->setNodeArray(nodes); t->setLsizenode(cast_byte(lsize)); t->setNoDummy(); - for (i = 0; i < size; i++) { + for (unsigned int i = 0; i < size; i++) { Node *n = gnode(t, i); gnext(n) = 0; n->setKeyNil();