Skip to content

Commit

Permalink
Reduce size of CollectionData::clear()
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Jul 24, 2023
1 parent 228c4cf commit 2fdacb1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/ArduinoJson/Collection/CollectionData.hpp
Expand Up @@ -110,10 +110,10 @@ class CollectionData {

protected:
iterator addSlot(ResourceManager*);
void releaseSlot(iterator, ResourceManager*);

private:
SlotWithId getPreviousSlot(VariantSlot*, const ResourceManager*) const;
void releaseSlot(SlotWithId, ResourceManager*);
};

inline const VariantData* collectionToVariant(
Expand Down
23 changes: 14 additions & 9 deletions src/ArduinoJson/Collection/CollectionImpl.hpp
Expand Up @@ -64,8 +64,14 @@ inline CollectionData::iterator CollectionData::addSlot(
}

inline void CollectionData::clear(ResourceManager* resources) {
for (auto it = createIterator(resources); !it.done(); it.next(resources))
releaseSlot(it, resources);
auto next = head_;
while (next != NULL_SLOT) {
auto currId = next;
auto slot = resources->getSlot(next);
next = slot->next();
releaseSlot(SlotWithId(slot, currId), resources);
}

head_ = NULL_SLOT;
tail_ = NULL_SLOT;
}
Expand Down Expand Up @@ -96,7 +102,7 @@ inline void CollectionData::remove(iterator it, ResourceManager* resources) {
head_ = next;
if (next == NULL_SLOT)
tail_ = prev.id();
releaseSlot(it, resources);
releaseSlot({it.slot_, it.currentId_}, resources);
}

inline size_t CollectionData::memoryUsage(
Expand Down Expand Up @@ -127,13 +133,12 @@ inline size_t CollectionData::size(const ResourceManager* resources) const {
return count;
}

inline void CollectionData::releaseSlot(iterator it,
inline void CollectionData::releaseSlot(SlotWithId slot,
ResourceManager* resources) {
ARDUINOJSON_ASSERT(!it.done());
if (it.ownsKey())
resources->dereferenceString(it.key());
it->setNull(resources);
resources->freeSlot(SlotWithId(it.slot_, it.currentId_));
if (slot->ownsKey())
resources->dereferenceString(slot->key());
slot->data()->setNull(resources);
resources->freeSlot(slot);
}

ARDUINOJSON_END_PRIVATE_NAMESPACE

0 comments on commit 2fdacb1

Please sign in to comment.