Skip to content

Commit

Permalink
performance: avoid inefficient vector reallocations
Browse files Browse the repository at this point in the history
  • Loading branch information
jesec committed Jul 24, 2021
1 parent 4cfa4b3 commit eae3a84
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions inou/slang/slang_driver.cpp
Expand Up @@ -151,6 +151,7 @@ bool loadAllSources(Compilation& compilation, SourceManager& sourceManager, cons
return ok;

std::vector<fs::path> directories;
directories.reserve(libDirs.size());
for (auto& dir : libDirs) directories.emplace_back(widen(dir));

flat_hash_set<string_view> uniqueExtensions;
Expand Down
11 changes: 11 additions & 0 deletions third_party/misc/ezsat/lezsat.cpp
Expand Up @@ -374,6 +374,7 @@ void lezSAT::assume(int id) {
}
if (op == OpOr) {
std::vector<int> clause;
clause.reserve(args.size());
for (int arg : args) clause.push_back(bind(arg));
cnfClauses.push_back(clause);
cnfClausesCount++;
Expand Down Expand Up @@ -404,6 +405,7 @@ void lezSAT::add_clause(const std::vector<int> &args) {

void lezSAT::add_clause(const std::vector<int> &args, bool argsPolarity, int a, int b, int c) {
std::vector<int> clause;
clause.reserve(args.size());
for (auto arg : args) clause.push_back(argsPolarity ? +arg : -arg);
if (a != 0)
clause.push_back(a);
Expand Down Expand Up @@ -536,6 +538,7 @@ int lezSAT::bind(int id, bool auto_freeze) {

if (op == OpIFF) {
std::vector<int> invArgs;
invArgs.reserve(args.size());
for (auto arg : args) invArgs.push_back(NOT(arg));
int sub1 = expression(OpAnd, args);
int sub2 = expression(OpAnd, invArgs);
Expand Down Expand Up @@ -607,30 +610,35 @@ bool lezSAT::solver(const std::vector<int> &, std::vector<bool> &, const std::ve

std::vector<int> lezSAT::vec_const(const std::vector<bool> &bits) {
std::vector<int> vec;
vec.reserve(bits.size());
for (auto bit : bits) vec.push_back(bit ? CONST_TRUE : CONST_FALSE);
return vec;
}

std::vector<int> lezSAT::vec_const_signed(int64_t value, int numBits) {
std::vector<int> vec;
vec.reserve(numBits);
for (int i = 0; i < numBits; i++) vec.push_back(((value >> i) & 1) != 0 ? CONST_TRUE : CONST_FALSE);
return vec;
}

std::vector<int> lezSAT::vec_const_unsigned(uint64_t value, int numBits) {
std::vector<int> vec;
vec.reserve(numBits);
for (int i = 0; i < numBits; i++) vec.push_back(((value >> i) & 1) != 0 ? CONST_TRUE : CONST_FALSE);
return vec;
}

std::vector<int> lezSAT::vec_var(int numBits) {
std::vector<int> vec;
vec.reserve(numBits);
for (int i = 0; i < numBits; i++) vec.push_back(literal());
return vec;
}

std::vector<int> lezSAT::vec_var(std::string name, int numBits) {
std::vector<int> vec;
vec.reserve(numBits);
for (int i = 0; i < numBits; i++) {
vec.push_back(VAR(name + my_int_to_string(i)));
}
Expand Down Expand Up @@ -658,12 +666,14 @@ std::vector<int> lezSAT::vec_pick(const std::vector<int> &vec1, int U32Const, in
std::vector<int> lezSAT::vec_pass(const std::vector<int> &vec1) {
std::vector<int> vec;

vec.reserve(vec1.size());
for (auto bit : vec1) vec.push_back(bit);
return vec;
}

std::vector<int> lezSAT::vec_not(const std::vector<int> &vec1) {
std::vector<int> vec;
vec.reserve(vec1.size());
for (auto bit : vec1) vec.push_back(NOT(bit));
return vec;
}
Expand Down Expand Up @@ -1526,6 +1536,7 @@ int lezSAT::onehot(const std::vector<int> &vec, bool max_only) {
// create binary vector
int num_bits = clog2(vec.size());
std::vector<int> bits;
bits.reserve(num_bits);
for (int k = 0; k < num_bits; k++) bits.push_back(literal());

// add at-most-one clauses using binary encoding
Expand Down

0 comments on commit eae3a84

Please sign in to comment.