diff options
author | Simon Hausmann <simon.hausmann@qt.io> | 2018-08-03 15:09:20 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2018-08-05 09:56:04 +0000 |
commit | 7d183691a9ad95e1e1fafb611f83f42ba607bf5d (patch) | |
tree | fe553ffbe03d6dc8bdcae9991f01aae0ccfc02cf /src/qml/memory | |
parent | c8daa17e9f4e753537a7069ad9bc27acd32bd29c (diff) |
Simplify chunk sweeping code
Instead of collecting the empty chunks after sweeping in a separate
vector, partition the chunks into non-empty and empty ones and erase the
second half after freeing them.
Change-Id: I7896c167b8b09a01b124dcdcb1eba8ddc0eef7f4
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src/qml/memory')
-rw-r--r-- | src/qml/memory/qv4mm.cpp | 28 |
1 files changed, 11 insertions, 17 deletions
diff --git a/src/qml/memory/qv4mm.cpp b/src/qml/memory/qv4mm.cpp index 451e7c485d..258fac57af 100644 --- a/src/qml/memory/qv4mm.cpp +++ b/src/qml/memory/qv4mm.cpp @@ -617,29 +617,23 @@ void BlockAllocator::sweep() // qDebug() << "BlockAlloc: sweep"; usedSlotsAfterLastSweep = 0; - std::vector<Chunk *> chunksToFree; + auto firstEmptyChunk = std::partition(chunks.begin(), chunks.end(), [this](Chunk *c) { + return c->sweep(engine); + }); - auto isFree = [this, &chunksToFree] (Chunk *c) { - bool isUsed = c->sweep(engine); - - if (isUsed) { - c->sortIntoBins(freeBins, NumBins); - usedSlotsAfterLastSweep += c->nUsedSlots(); - } else { - chunksToFree.push_back(c); - } - return !isUsed; - }; - - auto newEnd = std::remove_if(chunks.begin(), chunks.end(), isFree); - chunks.erase(newEnd, chunks.end()); + std::for_each(chunks.begin(), firstEmptyChunk, [this](Chunk *c) { + c->sortIntoBins(freeBins, NumBins); + usedSlotsAfterLastSweep += c->nUsedSlots(); + }); // only free the chunks at the end to avoid that the sweep() calls indirectly // access freed memory - for (auto c : chunksToFree) { + std::for_each(firstEmptyChunk, chunks.end(), [this](Chunk *c) { Q_V4_PROFILE_DEALLOC(engine, Chunk::DataSize, Profiling::HeapPage); chunkAllocator->free(c); - } + }); + + chunks.erase(firstEmptyChunk, chunks.end()); } void BlockAllocator::freeAll() |