Reduce unnecessary uses of std::list

This commit is contained in:
Michael Hansen
2022-12-01 11:35:14 -08:00
parent 863e09e9e7
commit 93495c3bfb
7 changed files with 54 additions and 78 deletions

View File

@@ -3,10 +3,11 @@
#include "pyc_sequence.h" #include "pyc_sequence.h"
#include "pyc_string.h" #include "pyc_string.h"
#include <set>
class PycCode : public PycObject { class PycCode : public PycObject {
public: public:
typedef std::list<PycRef<PycString> > globals_t; typedef std::set<PycRef<PycString>> globals_t;
enum CodeFlags { enum CodeFlags {
CO_OPTIMIZED = 0x1, CO_OPTIMIZED = 0x1,
CO_NEWLOCALS = 0x2, CO_NEWLOCALS = 0x2,
@@ -76,8 +77,7 @@ public:
void markGlobal(PycRef<PycString> varname) void markGlobal(PycRef<PycString> varname)
{ {
m_globalsUsed.push_back(varname); m_globalsUsed.emplace(std::move(varname));
m_globalsUsed.unique();
} }
private: private:

View File

@@ -235,26 +235,14 @@ void PycModule::loadFromMarshalledFile(const char* filename, int major, int mino
PycRef<PycString> PycModule::getIntern(int ref) const PycRef<PycString> PycModule::getIntern(int ref) const
{ {
if (ref < 0) if (ref < 0 || (size_t)ref >= m_interns.size())
throw std::out_of_range("Intern index out of range"); throw std::out_of_range("Intern index out of range");
return m_interns[(size_t)ref];
auto it = m_interns.cbegin();
while (ref-- && it != m_interns.cend())
++it;
if (it == m_interns.cend())
throw std::out_of_range("Intern index out of range");
return *it;
} }
PycRef<PycObject> PycModule::getRef(int ref) const PycRef<PycObject> PycModule::getRef(int ref) const
{ {
if (ref < 0) if (ref < 0 || (size_t)ref >= m_refs.size())
throw std::out_of_range("Ref index out of range"); throw std::out_of_range("Ref index out of range");
return m_refs[(size_t)ref];
auto it = m_refs.cbegin();
while (ref-- && it != m_refs.cend())
++it;
if (it == m_refs.cend())
throw std::out_of_range("Ref index out of range");
return *it;
} }

View File

@@ -2,7 +2,7 @@
#define _PYC_MODULE_H #define _PYC_MODULE_H
#include "pyc_code.h" #include "pyc_code.h"
#include <list> #include <vector>
enum PycMagic { enum PycMagic {
MAGIC_1_0 = 0x00999902, MAGIC_1_0 = 0x00999902,
@@ -64,10 +64,10 @@ public:
PycRef<PycCode> code() const { return m_code; } PycRef<PycCode> code() const { return m_code; }
void intern(PycRef<PycString> str) { m_interns.push_back(str); } void intern(PycRef<PycString> str) { m_interns.emplace_back(std::move(str)); }
PycRef<PycString> getIntern(int ref) const; PycRef<PycString> getIntern(int ref) const;
void refObject(PycRef<PycObject> str) { m_refs.push_back(str); } void refObject(PycRef<PycObject> obj) { m_refs.emplace_back(std::move(obj)); }
PycRef<PycObject> getRef(int ref) const; PycRef<PycObject> getRef(int ref) const;
static bool isSupportedVersion(int major, int minor); static bool isSupportedVersion(int major, int minor);
@@ -80,8 +80,8 @@ private:
bool m_unicode; bool m_unicode;
PycRef<PycCode> m_code; PycRef<PycCode> m_code;
std::list<PycRef<PycString> > m_interns; std::vector<PycRef<PycString>> m_interns;
std::list<PycRef<PycObject> > m_refs; std::vector<PycRef<PycObject>> m_refs;
}; };
#endif #endif

View File

@@ -18,6 +18,7 @@ void PycInt::load(PycData* stream, PycModule*)
void PycLong::load(PycData* stream, PycModule*) void PycLong::load(PycData* stream, PycModule*)
{ {
if (type() == TYPE_INT64) { if (type() == TYPE_INT64) {
m_value.reserve(4);
int lo = stream->get32(); int lo = stream->get32();
int hi = stream->get32(); int hi = stream->get32();
m_value.push_back((lo ) & 0xFFFF); m_value.push_back((lo ) & 0xFFFF);
@@ -28,6 +29,7 @@ void PycLong::load(PycData* stream, PycModule*)
} else { } else {
m_size = stream->get32(); m_size = stream->get32();
int actualSize = m_size >= 0 ? m_size : -m_size; int actualSize = m_size >= 0 ? m_size : -m_size;
m_value.reserve(actualSize);
for (int i=0; i<actualSize; i++) for (int i=0; i<actualSize; i++)
m_value.push_back(stream->get16()); m_value.push_back(stream->get16());
} }
@@ -41,9 +43,9 @@ bool PycLong::isEqual(PycRef<PycObject> obj) const
PycRef<PycLong> longObj = obj.cast<PycLong>(); PycRef<PycLong> longObj = obj.cast<PycLong>();
if (m_size != longObj->m_size) if (m_size != longObj->m_size)
return false; return false;
std::list<int>::const_iterator it1 = m_value.begin(); auto it1 = m_value.cbegin();
std::list<int>::const_iterator it2 = longObj->m_value.begin(); auto it2 = longObj->m_value.cbegin();
while (it1 != m_value.end()) { while (it1 != m_value.cend()) {
if (*it1 != *it2) if (*it1 != *it2)
return false; return false;
++it1, ++it2; ++it1, ++it2;
@@ -60,7 +62,8 @@ std::string PycLong::repr() const
return "0x0L"; return "0x0L";
// Realign to 32 bits, since Python uses only 15 // Realign to 32 bits, since Python uses only 15
std::list<unsigned> bits; std::vector<unsigned> bits;
bits.reserve((m_value.size() + 1) / 2);
int shift = 0, temp = 0; int shift = 0, temp = 0;
for (auto bit : m_value) { for (auto bit : m_value) {
temp |= unsigned(bit & 0xFFFF) << shift; temp |= unsigned(bit & 0xFFFF) << shift;
@@ -83,7 +86,7 @@ std::string PycLong::repr() const
*aptr++ = '0'; *aptr++ = '0';
*aptr++ = 'x'; *aptr++ = 'x';
std::list<unsigned>::const_reverse_iterator iter = bits.rbegin(); auto iter = bits.crbegin();
aptr += snprintf(aptr, 9, "%X", *iter++); aptr += snprintf(aptr, 9, "%X", *iter++);
while (iter != bits.rend()) while (iter != bits.rend())
aptr += snprintf(aptr, 9, "%08X", *iter++); aptr += snprintf(aptr, 9, "%08X", *iter++);

View File

@@ -3,7 +3,7 @@
#include "pyc_object.h" #include "pyc_object.h"
#include "data.h" #include "data.h"
#include <list> #include <vector>
#include <string> #include <string>
class PycInt : public PycObject { class PycInt : public PycObject {
@@ -35,13 +35,13 @@ public:
void load(class PycData* stream, class PycModule* mod) override; void load(class PycData* stream, class PycModule* mod) override;
int size() const { return m_size; } int size() const { return m_size; }
const std::list<int>& value() const { return m_value; } const std::vector<int>& value() const { return m_value; }
std::string repr() const; std::string repr() const;
private: private:
int m_size; int m_size;
std::list<int> m_value; std::vector<int> m_value;
}; };
class PycFloat : public PycObject { class PycFloat : public PycObject {

View File

@@ -24,9 +24,9 @@ bool PycTuple::isEqual(PycRef<PycObject> obj) const
PycRef<PycTuple> tupleObj = obj.cast<PycTuple>(); PycRef<PycTuple> tupleObj = obj.cast<PycTuple>();
if (m_size != tupleObj->m_size) if (m_size != tupleObj->m_size)
return false; return false;
value_t::const_iterator it1 = m_values.begin(); auto it1 = m_values.cbegin();
value_t::const_iterator it2 = tupleObj->m_values.begin(); auto it2 = tupleObj->m_values.cbegin();
while (it1 != m_values.end()) { while (it1 != m_values.cend()) {
if (!(*it1)->isEqual(*it2)) if (!(*it1)->isEqual(*it2))
return false; return false;
++it1, ++it2; ++it1, ++it2;
@@ -51,9 +51,9 @@ bool PycList::isEqual(PycRef<PycObject> obj) const
PycRef<PycList> listObj = obj.cast<PycList>(); PycRef<PycList> listObj = obj.cast<PycList>();
if (m_size != listObj->m_size) if (m_size != listObj->m_size)
return false; return false;
value_t::const_iterator it1 = m_values.begin(); auto it1 = m_values.cbegin();
value_t::const_iterator it2 = listObj->m_values.begin(); auto it2 = listObj->m_values.cbegin();
while (it1 != m_values.end()) { while (it1 != m_values.cend()) {
if (!(*it1)->isEqual(*it2)) if (!(*it1)->isEqual(*it2))
return false; return false;
++it1, ++it2; ++it1, ++it2;
@@ -61,20 +61,6 @@ bool PycList::isEqual(PycRef<PycObject> obj) const
return true; return true;
} }
PycRef<PycObject> PycList::get(int idx) const
{
if (idx < 0)
throw std::out_of_range("List index out of range");
value_t::const_iterator it = m_values.begin();
while (idx-- && it != m_values.end())
++it;
if (it == m_values.end())
throw std::out_of_range("List index out of range");
return *it;
}
/* PycDict */ /* PycDict */
void PycDict::load(PycData* stream, PycModule* mod) void PycDict::load(PycData* stream, PycModule* mod)
@@ -99,17 +85,17 @@ bool PycDict::isEqual(PycRef<PycObject> obj) const
if (m_size != dictObj->m_size) if (m_size != dictObj->m_size)
return false; return false;
key_t::const_iterator ki1 = m_keys.begin(); auto ki1 = m_keys.cbegin();
key_t::const_iterator ki2 = dictObj->m_keys.begin(); auto ki2 = dictObj->m_keys.cbegin();
while (ki1 != m_keys.end()) { while (ki1 != m_keys.cend()) {
if (!(*ki1)->isEqual(*ki2)) if (!(*ki1)->isEqual(*ki2))
return false; return false;
++ki1, ++ki2; ++ki1, ++ki2;
} }
value_t::const_iterator vi1 = m_values.begin(); auto vi1 = m_values.cbegin();
value_t::const_iterator vi2 = dictObj->m_values.begin(); auto vi2 = dictObj->m_values.cbegin();
while (vi1 != m_values.end()) { while (vi1 != m_values.cend()) {
if (!(*vi1)->isEqual(*vi2)) if (!(*vi1)->isEqual(*vi2))
return false; return false;
++vi1, ++vi2; ++vi1, ++vi2;
@@ -119,14 +105,14 @@ bool PycDict::isEqual(PycRef<PycObject> obj) const
PycRef<PycObject> PycDict::get(PycRef<PycObject> key) const PycRef<PycObject> PycDict::get(PycRef<PycObject> key) const
{ {
key_t::const_iterator ki = m_keys.begin(); auto ki = m_keys.cbegin();
value_t::const_iterator vi = m_values.begin(); auto vi = m_values.cbegin();
while (ki != m_keys.end()) { while (ki != m_keys.cend()) {
if ((*ki)->isEqual(key)) if ((*ki)->isEqual(key))
return *vi; return *vi;
++ki, ++vi; ++ki, ++vi;
} }
return NULL; // Disassembly shouldn't get non-existant keys return NULL; // Disassembly shouldn't get non-existent keys
} }
PycRef<PycObject> PycDict::get(int idx) const PycRef<PycObject> PycDict::get(int idx) const
@@ -134,10 +120,10 @@ PycRef<PycObject> PycDict::get(int idx) const
if (idx < 0) if (idx < 0)
throw std::out_of_range("Dict index out of range"); throw std::out_of_range("Dict index out of range");
value_t::const_iterator it = m_values.begin(); auto it = m_values.cbegin();
while (idx-- && it != m_values.end()) while (idx-- && it != m_values.cend())
++it; ++it;
if (it == m_values.end()) if (it == m_values.cend())
throw std::out_of_range("Dict index out of range"); throw std::out_of_range("Dict index out of range");
return *it; return *it;
} }
@@ -159,9 +145,9 @@ bool PycSet::isEqual(PycRef<PycObject> obj) const
PycRef<PycSet> setObj = obj.cast<PycSet>(); PycRef<PycSet> setObj = obj.cast<PycSet>();
if (m_size != setObj->m_size) if (m_size != setObj->m_size)
return false; return false;
value_t::const_iterator it1 = m_values.begin(); auto it1 = m_values.cbegin();
value_t::const_iterator it2 = setObj->m_values.begin(); auto it2 = setObj->m_values.cbegin();
while (it1 != m_values.end()) { while (it1 != m_values.cend()) {
if (!(*it1)->isEqual(*it2)) if (!(*it1)->isEqual(*it2))
return false; return false;
++it1, ++it2; ++it1, ++it2;
@@ -174,10 +160,10 @@ PycRef<PycObject> PycSet::get(int idx) const
if (idx < 0) if (idx < 0)
throw std::out_of_range("Set index out of range"); throw std::out_of_range("Set index out of range");
value_t::const_iterator it = m_values.begin(); auto it = m_values.cbegin();
while (idx-- && it != m_values.end()) while (idx-- && it != m_values.cend())
++it; ++it;
if (it == m_values.end()) if (it == m_values.cend())
throw std::out_of_range("Set index out of range"); throw std::out_of_range("Set index out of range");
return *it; return *it;
} }

View File

@@ -3,7 +3,6 @@
#include "pyc_object.h" #include "pyc_object.h"
#include <vector> #include <vector>
#include <list>
#include <set> #include <set>
class PycSequence : public PycObject { class PycSequence : public PycObject {
@@ -19,7 +18,7 @@ protected:
class PycTuple : public PycSequence { class PycTuple : public PycSequence {
public: public:
typedef std::vector<PycRef<PycObject> > value_t; typedef std::vector<PycRef<PycObject>> value_t;
PycTuple(int type = TYPE_TUPLE) : PycSequence(type) { } PycTuple(int type = TYPE_TUPLE) : PycSequence(type) { }
@@ -36,7 +35,7 @@ private:
class PycList : public PycSequence { class PycList : public PycSequence {
public: public:
typedef std::list<PycRef<PycObject> > value_t; typedef std::vector<PycRef<PycObject>> value_t;
PycList(int type = TYPE_LIST) : PycSequence(type) { } PycList(int type = TYPE_LIST) : PycSequence(type) { }
@@ -45,7 +44,7 @@ public:
void load(class PycData* stream, class PycModule* mod) override; void load(class PycData* stream, class PycModule* mod) override;
const value_t& values() const { return m_values; } const value_t& values() const { return m_values; }
PycRef<PycObject> get(int idx) const override; PycRef<PycObject> get(int idx) const override { return m_values.at(idx); }
private: private:
value_t m_values; value_t m_values;
@@ -53,8 +52,8 @@ private:
class PycDict : public PycSequence { class PycDict : public PycSequence {
public: public:
typedef std::list<PycRef<PycObject> > key_t; typedef std::vector<PycRef<PycObject>> key_t;
typedef std::list<PycRef<PycObject> > value_t; typedef std::vector<PycRef<PycObject>> value_t;
PycDict(int type = TYPE_DICT) : PycSequence(type) { } PycDict(int type = TYPE_DICT) : PycSequence(type) { }
@@ -75,7 +74,7 @@ private:
class PycSet : public PycSequence { class PycSet : public PycSequence {
public: public:
typedef std::set<PycRef<PycObject> > value_t; typedef std::set<PycRef<PycObject>> value_t;
PycSet(int type = TYPE_SET) : PycSequence(type) { } PycSet(int type = TYPE_SET) : PycSequence(type) { }