Skip to content

Commit

Permalink
[dxvk] Order allocations to relocate by offset
Browse files Browse the repository at this point in the history
Potentially improves the situation where a chunk cannot be relocated
in its entirely by freeing up a larger block of memory at the end of
the chunk, which may be enough to service a subsequent defrag attempt
of a different chunk.
  • Loading branch information
doitsujin committed Nov 8, 2024
1 parent 8584fc7 commit c5bc4d1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
16 changes: 9 additions & 7 deletions src/dxvk/dxvk_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,12 +489,12 @@ namespace dxvk {
for (uint32_t i = 0; i < count; i++) {
auto iter = m_entries.begin();

if (totalSize && totalSize + iter->second.size > size)
if (totalSize && totalSize + iter->first.size > size)
break;

totalSize += iter->second.size;
totalSize += iter->first.size;

result.push_back({ iter->first, iter->second.mode });
result.push_back(std::move(iter->second));
m_entries.erase(iter);
}

Expand All @@ -504,10 +504,12 @@ namespace dxvk {

void DxvkRelocationList::addResource(
Rc<DxvkPagedResource>&& resource,
DxvkAllocationModes mode,
VkDeviceSize size) {
const DxvkResourceAllocation* allocation,
DxvkAllocationModes mode) {
std::lock_guard lock(m_mutex);
m_entries.emplace(std::move(resource), Entry { mode, size });
m_entries.emplace(std::piecewise_construct,
std::forward_as_tuple(allocation->getMemoryInfo()),
std::forward_as_tuple(std::move(resource), mode));
}


Expand Down Expand Up @@ -2175,7 +2177,7 @@ namespace dxvk {
continue;

// Acquired the resource, add it to the relocation list.
m_relocations.addResource(std::move(resource), mode, a->m_size);
m_relocations.addResource(std::move(resource), a, mode);
}
}

Expand Down
31 changes: 22 additions & 9 deletions src/dxvk/dxvk_memory.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <map>
#include <memory>

#include "dxvk_access.h"
Expand Down Expand Up @@ -985,6 +986,10 @@ namespace dxvk {
* \brief Relocation entry
*/
struct DxvkRelocationEntry {
DxvkRelocationEntry() = default;
DxvkRelocationEntry(Rc<DxvkPagedResource>&& r, DxvkAllocationModes m)
: resource(std::move(r)), mode(m) { }

/// Resource to relocate
Rc<DxvkPagedResource> resource;
/// Resource to relocate
Expand Down Expand Up @@ -1023,13 +1028,13 @@ namespace dxvk {
* \brief Adds relocation entry to the list
*
* \param [in] resource Resource to add
* \param [in] allocation Resource storage
* \param [in] mode Allocation mode
* \param [in] size Allocation size
*/
void addResource(
Rc<DxvkPagedResource>&& resource,
DxvkAllocationModes mode,
VkDeviceSize size);
const DxvkResourceAllocation* allocation,
DxvkAllocationModes mode);

/**
* \brief Clears list
Expand All @@ -1046,14 +1051,22 @@ namespace dxvk {

private:

struct Entry {
DxvkAllocationModes mode = 0u;
VkDeviceSize size = 0u;
struct RelocationOrdering {
bool operator () (const DxvkResourceMemoryInfo& a, const DxvkResourceMemoryInfo& b) const {
// Keep chunks together, then order by offset in order to increase
// the likelihood of freeing up a contiguous block of memory.
if (a.memory < b.memory) return true;
if (a.memory > b.memory) return false;
return a.offset > b.offset;
}
};

dxvk::mutex m_mutex;
std::unordered_map<Rc<DxvkPagedResource>,
Entry, RcHash> m_entries;
dxvk::mutex m_mutex;

std::map<
DxvkResourceMemoryInfo,
DxvkRelocationEntry,
RelocationOrdering> m_entries;

};

Expand Down

0 comments on commit c5bc4d1

Please sign in to comment.