Skip to content

Commit 3fe02f5

Browse files
committed
Optimize string pool assertions
1 parent 111885e commit 3fe02f5

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

src/scratch/string_pool.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ namespace libscratchcpp
1515
static std::unordered_set<std::unique_ptr<StringPtr>> strings;
1616
static std::multimap<size_t, StringPtr *> freeStrings; // sorted by allocated space
1717

18+
#ifndef NDEBUG
19+
static std::unordered_set<StringPtr *> allStringPtrs;
20+
static std::unordered_set<StringPtr *> freeStringPtrs;
21+
#endif
22+
1823
extern "C"
1924
{
2025
/*!
@@ -28,24 +33,31 @@ extern "C"
2833
StringPtr *ptr = str.get();
2934
assert(strings.find(str) == strings.cend());
3035
strings.insert(std::move(str));
31-
36+
#ifndef NDEBUG
37+
allStringPtrs.insert(ptr);
38+
#endif
3239
return ptr;
3340
}
3441

3542
// Optimization: pick string with the highest capacity to minimize allocs
3643
auto last = std::prev(freeStrings.end());
3744
StringPtr *ptr = last->second;
3845
freeStrings.erase(last);
39-
46+
#ifndef NDEBUG
47+
freeStringPtrs.erase(ptr);
48+
#endif
4049
return ptr;
4150
}
4251

4352
/*! Invalidates the given StringPtr so that it can be used for new strings later. */
4453
void string_pool_free(StringPtr *str)
4554
{
46-
assert(std::find_if(freeStrings.begin(), freeStrings.end(), [str](const std::pair<size_t, StringPtr *> &p) { return p.second == str; }) == freeStrings.end());
47-
assert(std::find_if(strings.begin(), strings.end(), [str](const std::unique_ptr<StringPtr> &p) { return p.get() == str; }) != strings.end());
55+
assert(freeStringPtrs.count(str) == 0);
56+
assert(allStringPtrs.count(str) != 0);
4857
freeStrings.insert(std::pair<size_t, StringPtr *>(str->allocatedSize, str));
58+
#ifndef NDEBUG
59+
freeStringPtrs.insert(str);
60+
#endif
4961
}
5062
}
5163

0 commit comments

Comments
 (0)