mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 10:46:33 +02:00
Optimize PowerRename logic on memory allocation based on path depth. (#44603)
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? --> ## Summary of the Pull Request This pull request improves the way items are grouped and processed by depth in the `s_fileOpWorkerThread` function of `PowerRenameManager.cpp`. Instead of sizing the depth matrix by the total item count, it now determines the actual maximum depth of items first, resulting in more efficient memory usage and correctness when handling items by their depth. **Improvements to depth-based processing:** * Added a first pass to determine the maximum depth among all items before allocating the depth matrix, ensuring the matrix is sized appropriately and not excessively large. * Updated the logic to iterate from the maximum depth down to zero when adding items, aligning with the new matrix sizing and ensuring correct processing order.
This commit is contained in:
@@ -754,8 +754,26 @@ DWORD WINAPI CPowerRenameManager::s_fileOpWorkerThread(_In_ void* pv)
|
||||
// We add the items to the operation in depth-first order. This allows child items to be
|
||||
// renamed before parent items.
|
||||
|
||||
// First pass: find the maximum depth to properly size the matrix
|
||||
UINT maxDepth = 0;
|
||||
for (UINT u = 0; u < itemCount; u++)
|
||||
{
|
||||
CComPtr<IPowerRenameItem> spItem;
|
||||
if (SUCCEEDED(pwtd->spsrm->GetItemByIndex(u, &spItem)))
|
||||
{
|
||||
UINT depth = 0;
|
||||
spItem->GetDepth(&depth);
|
||||
if (depth > maxDepth)
|
||||
{
|
||||
maxDepth = depth;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Creating a vector of vectors of items of the same depth
|
||||
std::vector<std::vector<UINT>> matrix(itemCount);
|
||||
// Size by maxDepth+1 (not itemCount) to avoid excessive memory allocation
|
||||
// Cast to size_t before arithmetic to avoid overflow on 32-bit UINT
|
||||
std::vector<std::vector<UINT>> matrix(static_cast<size_t>(maxDepth) + 1);
|
||||
|
||||
for (UINT u = 0; u < itemCount; u++)
|
||||
{
|
||||
@@ -769,7 +787,7 @@ DWORD WINAPI CPowerRenameManager::s_fileOpWorkerThread(_In_ void* pv)
|
||||
}
|
||||
|
||||
// From the greatest depth first, add all items of that depth to the operation
|
||||
for (LONG v = itemCount - 1; v >= 0; v--)
|
||||
for (LONG v = static_cast<LONG>(maxDepth); v >= 0; v--)
|
||||
{
|
||||
for (auto it : matrix[v])
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user