-
-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pr/various mem optimizations #85
Merged
ikpil
merged 22 commits into
ikpil:pr/wrenge-pr/various-mem-optimizations
from
wrenge:pr/various-mem-optimizations
Dec 3, 2024
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
c7c6e53
Made list in DtPathCorridor.cs readonly
bitwrenge dbc92a9
RcRentedArray AsSpan
bitwrenge 2c6f6a5
CollectionExtensions.cs List<T>.AddRange(Span<T>)
bitwrenge 49da3fb
Removed allocations from MergeCorridorStartMoved
bitwrenge 4dee6b2
typo fix
bitwrenge 05613f1
MergeCorridorStartShortcut alloc fix
bitwrenge 418a39a
MergeCorridorEndMoved alloc fix
bitwrenge 5c0ba9d
Reuse grid instead of creating new
bitwrenge b2a217d
Object pool
bitwrenge 592eceb
Proximity grid list reuse
bitwrenge 815a83e
Use precached queue instead of linked list
bitwrenge 1fa0320
precache collections
bitwrenge d2082c6
Added clear
bitwrenge 4824f29
Non alloc raycast
bitwrenge 12e0947
Replace predicate find with simple find
bitwrenge 088edcd
Intersection query alloc fix
bitwrenge 18b5448
Pass delegate
wrenge 7aeb31d
Span access
wrenge a7b9b77
Use rented array instead of allocating
bitwrenge a6db434
Queries change
bitwrenge 1315de0
Replaced arrays with spans in query. Replaced array allocation with b…
wrenge 96ffed8
Rented array struct
wrenge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace DotRecast.Core.Buffers | ||
{ | ||
// This implementation is thread unsafe | ||
public class RcObjectPool<T> where T : class | ||
{ | ||
private readonly Queue<T> _items = new Queue<T>(); | ||
private readonly Func<T> _createFunc; | ||
|
||
public RcObjectPool(Func<T> createFunc) | ||
{ | ||
_createFunc = createFunc; | ||
} | ||
|
||
public T Get() | ||
{ | ||
if (_items.TryDequeue(out var result)) | ||
return result; | ||
|
||
return _createFunc(); | ||
} | ||
|
||
public void Return(T obj) | ||
{ | ||
_items.Enqueue(obj); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,12 +27,12 @@ public class RcSortedQueue<T> | |
{ | ||
private bool _dirty; | ||
private readonly List<T> _items; | ||
private readonly Comparer<T> _comparer; | ||
private readonly Comparison<T> _comparison; | ||
|
||
public RcSortedQueue(Comparison<T> comp) | ||
{ | ||
_items = new List<T>(); | ||
_comparer = Comparer<T>.Create((x, y) => comp.Invoke(x, y) * -1); | ||
_comparison = (x, y) => comp(x, y) * -1; | ||
} | ||
|
||
public int Count() | ||
|
@@ -55,7 +55,7 @@ private void Balance() | |
{ | ||
if (_dirty) | ||
{ | ||
_items.Sort(_comparer); // reverse | ||
_items.Sort(_comparison); // reverse | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. List.Sort(IComparer) allocates Comparison, so we pass delegate directly to avoid allocations. |
||
_dirty = false; | ||
} | ||
} | ||
|
@@ -88,7 +88,7 @@ public bool Remove(T item) | |
return false; | ||
|
||
//int idx = _items.BinarySearch(item, _comparer); // don't use this! Because reference types can be reused externally. | ||
int idx = _items.FindLastIndex(x => item.Equals(x)); | ||
int idx = _items.LastIndexOf(item); | ||
if (0 > idx) | ||
return false; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is tricky one. Since RcRentedArray now a value type, it will copy values. That means when we dispose it, all copies will remain reference to owner and array.
To fix that we introduce rent id. When rent array, get vacant id and generation. On dispose check the generation. If generation is different, that means this rent was already freed. Otherwise release array and increase the generation.