1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-11-14 15:46:44 +00:00
Ryujinx/Ryujinx.Tests/TreeDictionaryTests.cs
sharmander 06aa8a7578
GPU - Improve Memory Allocation (#1722)
* Implement TreeMap from scratch.

Begin implementation of MemoryBlockManager

* Implement GetFreePosition using MemoryBlocks

* Implementation of Memory Management using a Tree.

Still some issues to work around, but promising thus far.

* Resolved invalid mapping issue.

Performance appears promising.

* Add tick metrics

* Use the logger instead

* Use debug loggin instead of info.

* Remove unnecessary code. Add descriptions of added functions.

* Improve memory allocation even further. As well as improve speed of position fetching.

* Add TreeDictionary to Ryujinx Commons

Removed Unnecessary  Usigns

* Add a Performance Profiler + Improve ReserveFixed

* Begin transition to allocation in nvdrv

* Create singleton nvmemallocator

* Moved Allocation into Nv Related Files

As requested by gdkchan, any allocation of memory has been moved into the driver files.

Mapping remains in the GPU MemoryManager.

* Remove unnecessary usings

* Add missing descriptions

* Correct descriptions

* Fix formatting.

* Remove unnecessary whitespace

* Formatting / Convention Updates

* Changes / Fixes

Made syntax and convention changes as requested by gdkchan.

Fixed an issue where IsRegionUsed would return the wrong boolean.

Fixed an issue where GetFreePosition was asked for an address instead of a size.

* Undo commenting of Assert in shader cache

* Update Ryujinx.Common/Collections/TreeDictionary.cs

Co-authored-by: gdkchan <gab.dark.100@gmail.com>

* Resolved many suggestions

* Implement Improved TreeDictionary

Based off of Pseudo code and custom implementations.

* Rename _set to _dictionary

* Remove unused code

* Remove unused code.

* Remove unnecessary MapLow function.

* Resolve data-structure based issues

* Make adjustments to memory management.

Deactive de-allocation for now, it causes more harm than good.

* Minor refactorings + Re-implement deallocation

Also cleaned up unnecessary code.

* Add Tests for TreeDictionary

* Update data structure to properly balance the tree

* Experimental Implementation:

1. Reduce Time to Next Node to O(1) Runtime
2. Reduce While Loop Ct To 2 (In Most Cases)

* Address issues w/ Deallocating Memory

* Final Build

+ Fully Implement Dictionary Interface for new Data Structure
+ Cover All Memory Allocation Edge Cases, particularly w/ Games that De-Allocate a lot.

* Minor Corrections

Give TreeDictionary its own count (do not depend on inner dictionary)

Properly remove adjacent allocations

* Add AsList

* Fix bug where internal dictionary wasn't being updated w/ new node for overwritten key.

* Address comments in review.

* Fix issue where block wouldn't break out (Fixes UE4 issues)

* Update descriptions

* Update descriptions

* Reduce Node visibility to protect TreeDictionary Integrity + Remove usage of struct.

* Update tests to use new TreeDictionary implementation.

* Remove usage of dictionary in TreeDictionary

* Refactoring / Renaming

* Remove unneeded memoryblock class.

* Add space for while

* Add space for if

* Formatting / descriptions

* Clarified some descriptions

* Reduce visibility of memory allocator

* Edit method names to make more sense as memory blocks are no longer in use.

* Make names consistent.

* Protect against npe when sucessorof is called against keys that don't exist. (Not in use by memory manager, this is for other prs that might use this data structure)

* Possible edge-case resolve

* Update Ryujinx.Common/Collections/TreeDictionary.cs

Co-authored-by: gdkchan <gab.dark.100@gmail.com>

* Update Ryujinx.HLE/HOS/Services/Nv/NvMemoryAllocator.cs

Co-authored-by: gdkchan <gab.dark.100@gmail.com>

* Reduce # of unnecessary duplicate variables / Reduce visibility of variables only internally used.

* Rename count to _count

* Update Description of Add method.

* Fix copypasta

* Address comments

* Address comments

* Remove whitespace

* Address comments, condense variables.

* Consolidate vars

* Fix whitespace.

* Nit

* Fix exception msg

* Fix arrayIndex check

* Fix arrayIndex check + indexer

* Remove whitespace from cast

Co-authored-by: gdkchan <gab.dark.100@gmail.com>
2020-12-09 19:26:05 -03:00

244 lines
8 KiB
C#

using NUnit.Framework;
using Ryujinx.Common.Collections;
using System;
using System.Collections.Generic;
namespace Ryujinx.Tests.Collections
{
class TreeDictionaryTests
{
[Test]
public void EnsureAddIntegrity()
{
TreeDictionary<int, int> dictionary = new TreeDictionary<int, int>();
Assert.AreEqual(dictionary.Count, 0);
dictionary.Add(2, 7);
dictionary.Add(1, 4);
dictionary.Add(10, 2);
dictionary.Add(4, 1);
dictionary.Add(3, 2);
dictionary.Add(11, 2);
dictionary.Add(5, 2);
Assert.AreEqual(dictionary.Count, 7);
List<KeyValuePair<int, int>> list = dictionary.AsLevelOrderList();
/*
* Tree Should Look as Follows After Rotations
*
* 2
* 1 4
* 3 10
* 5 11
*
*/
Assert.AreEqual(list.Count, dictionary.Count);
Assert.AreEqual(list[0].Key, 2);
Assert.AreEqual(list[1].Key, 1);
Assert.AreEqual(list[2].Key, 4);
Assert.AreEqual(list[3].Key, 3);
Assert.AreEqual(list[4].Key, 10);
Assert.AreEqual(list[5].Key, 5);
Assert.AreEqual(list[6].Key, 11);
}
[Test]
public void EnsureRemoveIntegrity()
{
TreeDictionary<int, int> dictionary = new TreeDictionary<int, int>();
Assert.AreEqual(dictionary.Count, 0);
dictionary.Add(2, 7);
dictionary.Add(1, 4);
dictionary.Add(10, 2);
dictionary.Add(4, 1);
dictionary.Add(3, 2);
dictionary.Add(11, 2);
dictionary.Add(5, 2);
dictionary.Add(7, 2);
dictionary.Add(9, 2);
dictionary.Add(8, 2);
dictionary.Add(13, 2);
dictionary.Add(24, 2);
dictionary.Add(6, 2);
Assert.AreEqual(dictionary.Count, 13);
List<KeyValuePair<int, int>> list = dictionary.AsLevelOrderList();
/*
* Tree Should Look as Follows After Rotations
*
* 4
* 2 10
* 1 3 7 13
* 5 9 11 24
* 6 8
*/
foreach (KeyValuePair<int, int> node in list)
{
Console.WriteLine($"{node.Key} -> {node.Value}");
}
Assert.AreEqual(list.Count, dictionary.Count);
Assert.AreEqual(list[0].Key, 4);
Assert.AreEqual(list[1].Key, 2);
Assert.AreEqual(list[2].Key, 10);
Assert.AreEqual(list[3].Key, 1);
Assert.AreEqual(list[4].Key, 3);
Assert.AreEqual(list[5].Key, 7);
Assert.AreEqual(list[6].Key, 13);
Assert.AreEqual(list[7].Key, 5);
Assert.AreEqual(list[8].Key, 9);
Assert.AreEqual(list[9].Key, 11);
Assert.AreEqual(list[10].Key, 24);
Assert.AreEqual(list[11].Key, 6);
Assert.AreEqual(list[12].Key, 8);
list.Clear();
dictionary.Remove(7);
/*
* Tree Should Look as Follows After Removal
*
* 4
* 2 10
* 1 3 6 13
* 5 9 11 24
* 8
*/
list = dictionary.AsLevelOrderList();
foreach (KeyValuePair<int, int> node in list)
{
Console.WriteLine($"{node.Key} -> {node.Value}");
}
Assert.AreEqual(list[0].Key, 4);
Assert.AreEqual(list[1].Key, 2);
Assert.AreEqual(list[2].Key, 10);
Assert.AreEqual(list[3].Key, 1);
Assert.AreEqual(list[4].Key, 3);
Assert.AreEqual(list[5].Key, 6);
Assert.AreEqual(list[6].Key, 13);
Assert.AreEqual(list[7].Key, 5);
Assert.AreEqual(list[8].Key, 9);
Assert.AreEqual(list[9].Key, 11);
Assert.AreEqual(list[10].Key, 24);
Assert.AreEqual(list[11].Key, 8);
list.Clear();
dictionary.Remove(10);
list = dictionary.AsLevelOrderList();
/*
* Tree Should Look as Follows After Removal
*
* 4
* 2 9
* 1 3 6 13
* 5 8 11 24
*
*/
foreach (KeyValuePair<int, int> node in list)
{
Console.WriteLine($"{node.Key} -> {node.Value}");
}
Assert.AreEqual(list[0].Key, 4);
Assert.AreEqual(list[1].Key, 2);
Assert.AreEqual(list[2].Key, 9);
Assert.AreEqual(list[3].Key, 1);
Assert.AreEqual(list[4].Key, 3);
Assert.AreEqual(list[5].Key, 6);
Assert.AreEqual(list[6].Key, 13);
Assert.AreEqual(list[7].Key, 5);
Assert.AreEqual(list[8].Key, 8);
Assert.AreEqual(list[9].Key, 11);
Assert.AreEqual(list[10].Key, 24);
}
[Test]
public void EnsureOverwriteIntegrity()
{
TreeDictionary<int, int> dictionary = new TreeDictionary<int, int>();
Assert.AreEqual(dictionary.Count, 0);
dictionary.Add(2, 7);
dictionary.Add(1, 4);
dictionary.Add(10, 2);
dictionary.Add(4, 1);
dictionary.Add(3, 2);
dictionary.Add(11, 2);
dictionary.Add(5, 2);
dictionary.Add(7, 2);
dictionary.Add(9, 2);
dictionary.Add(8, 2);
dictionary.Add(13, 2);
dictionary.Add(24, 2);
dictionary.Add(6, 2);
Assert.AreEqual(dictionary.Count, 13);
List<KeyValuePair<int, int>> list = dictionary.AsLevelOrderList();
foreach (KeyValuePair<int, int> node in list)
{
Console.WriteLine($"{node.Key} -> {node.Value}");
}
/*
* Tree Should Look as Follows After Rotations
*
* 4
* 2 10
* 1 3 7 13
* 5 9 11 24
* 6 8
*/
Assert.AreEqual(list.Count, dictionary.Count);
Assert.AreEqual(list[0].Key, 4);
Assert.AreEqual(list[1].Key, 2);
Assert.AreEqual(list[2].Key, 10);
Assert.AreEqual(list[3].Key, 1);
Assert.AreEqual(list[4].Key, 3);
Assert.AreEqual(list[5].Key, 7);
Assert.AreEqual(list[6].Key, 13);
Assert.AreEqual(list[7].Key, 5);
Assert.AreEqual(list[8].Key, 9);
Assert.AreEqual(list[9].Key, 11);
Assert.AreEqual(list[10].Key, 24);
Assert.AreEqual(list[11].Key, 6);
Assert.AreEqual(list[12].Key, 8);
Assert.AreEqual(list[4].Value, 2);
dictionary.Add(3, 4);
list = dictionary.AsLevelOrderList();
Assert.AreEqual(list[4].Value, 4);
// Assure that none of the nodes locations have been modified.
Assert.AreEqual(list[0].Key, 4);
Assert.AreEqual(list[1].Key, 2);
Assert.AreEqual(list[2].Key, 10);
Assert.AreEqual(list[3].Key, 1);
Assert.AreEqual(list[4].Key, 3);
Assert.AreEqual(list[5].Key, 7);
Assert.AreEqual(list[6].Key, 13);
Assert.AreEqual(list[7].Key, 5);
Assert.AreEqual(list[8].Key, 9);
Assert.AreEqual(list[9].Key, 11);
Assert.AreEqual(list[10].Key, 24);
Assert.AreEqual(list[11].Key, 6);
Assert.AreEqual(list[12].Key, 8);
}
}
}