-
-
Notifications
You must be signed in to change notification settings - Fork 55
Большие оптимизации #866
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
Merged
Merged
Большие оптимизации #866
Changes from 4 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1a8bf1a
refactor: optimize event sending, add auto-scaling SeeRange
ThereDrD0 0d993b9
refactor: huge eye watching optimizations
ThereDrD0 6ecfbbc
refactor: optimize eye watching and related systems by list/hashset p…
ThereDrD0 f92bab7
refactor: split eye watching API into 3 parts, optimize API usings, o…
ThereDrD0 619a79a
refactor: micro optimize proximity
ThereDrD0 7e9042c
refactor: little more optimizations for blinking system
ThereDrD0 2161286
refactor: remove duplicate proximity checks from EyeWatchingSystem Up…
ThereDrD0 c95fec5
add: docs and little performance improvement
ThereDrD0 f847485
refactor: big fear refactor and optimizations
ThereDrD0 b566e01
add: dirty fields for fear component
ThereDrD0 2f3c320
add: remove constant MoveEvent events
ThereDrD0 0233252
refactor: move blinking alert logic to client
ThereDrD0 1630db3
refactor: optimize scp939 code
ThereDrD0 2f066f9
refactor: a bit more optimized 939
ThereDrD0 3753814
fix: ai review
ThereDrD0 ff35ec1
refactor: partialize scp939 hud class + replace PlayerAttached/Detach…
ThereDrD0 d964120
fix: better implementation
ThereDrD0 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,193 @@ | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| namespace Content.Shared._Scp.Helpers; | ||
|
|
||
| /// <summary> | ||
| /// Provides a static object pool for collections to minimize garbage collection allocations. | ||
| /// </summary> | ||
| /// <typeparam name="TCollection">The type of the collection being pooled. Must implement <see cref="ICollection{T}"/>.</typeparam> | ||
| /// <typeparam name="T">The type of the elements contained in the collection.</typeparam> | ||
| public static class CollectionPool<TCollection, T> | ||
| where TCollection : class, ICollection<T> | ||
| { | ||
| private static readonly Stack<TCollection> Pool = new(); | ||
| private static Func<TCollection>? _factory; | ||
|
|
||
| /// <summary> | ||
| /// Configures the factory function used to instantiate new collections when the pool is empty. | ||
| /// </summary> | ||
| /// <param name="factory">The delegate used to create new instances of <typeparamref name="TCollection"/>.</param> | ||
| /// <exception cref="InvalidOperationException">Thrown when the provided <paramref name="factory"/> is null.</exception> | ||
| public static void Configure(Func<TCollection> factory) | ||
| { | ||
| _factory = factory ?? throw new InvalidOperationException("Factory cannot be null"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a new collection using the configured factory. | ||
| /// </summary> | ||
| /// <returns>A new instance of <typeparamref name="TCollection"/>.</returns> | ||
| /// <exception cref="InvalidOperationException">Thrown if the pool has not been configured via <see cref="Configure"/>.</exception> | ||
| private static TCollection Create() | ||
| { | ||
| if (_factory is null) | ||
| { | ||
| throw new InvalidOperationException( | ||
| $"CollectionPool<{typeof(TCollection).Name}, {typeof(T).Name}> " + | ||
| $"is not configured. Call Configure(factory) before use."); | ||
| } | ||
|
|
||
| return _factory(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Rents a collection from the pool. If the pool is empty, a new collection is created. | ||
| /// </summary> | ||
| /// <returns>A disposable <see cref="PooledCollection"/> wrapper. Use within a <see langword="using"/> statement to automatically return the collection to the pool.</returns> | ||
| public static PooledCollection Rent() | ||
| { | ||
| return Pool.TryPop(out var collection) | ||
| ? new PooledCollection(collection) | ||
| : new PooledCollection(Create()); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns a collection to the pool. Clears the collection before storing it. | ||
| /// Collections will be dropped and garbage collected if the pool is full (>= 512 items) or if the collection capacity exceeds 1024. | ||
| /// </summary> | ||
| /// <param name="collection">The collection to return to the pool.</param> | ||
| internal static void Return(TCollection collection) | ||
| { | ||
| if (Pool.Count >= 512) | ||
| { | ||
| Logger.Warning("Pool bloated, new collections will not be stored"); | ||
| return; | ||
| } | ||
|
|
||
| if (collection is List<T> list && list.Capacity > 2048) | ||
| return; | ||
|
|
||
| if (collection is HashSet<T> hashSet && hashSet.Capacity > 2048) | ||
| return; | ||
|
|
||
| collection.Clear(); | ||
| Pool.Push(collection); | ||
ThereDrD0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// An allocation-free disposable wrapper around a rented collection. | ||
| /// </summary> | ||
| public struct PooledCollection : IDisposable | ||
| { | ||
| private TCollection? _value; | ||
| private bool _disposed; | ||
|
|
||
| /// <summary> | ||
| /// Gets the underlying rented collection. | ||
| /// </summary> | ||
| /// <exception cref="InvalidOperationException">Thrown if accessed after the collection has been returned to the pool.</exception> | ||
| public TCollection Value | ||
| { | ||
| get | ||
| { | ||
| if (_disposed) | ||
| throw new InvalidOperationException("The collection has already been returned to the pool."); | ||
|
|
||
| return _value!; | ||
| } | ||
| } | ||
|
|
||
| internal PooledCollection(TCollection value) | ||
| { | ||
| _value = value; | ||
| _disposed = false; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Returns the underlying collection to the pool and marks this wrapper as disposed. | ||
| /// </summary> | ||
| public void Dispose() | ||
| { | ||
| if (_disposed) | ||
| return; | ||
|
|
||
| _disposed = true; | ||
|
|
||
| if (_value is not null) | ||
| { | ||
| Return(_value); | ||
| _value = default; | ||
| } | ||
| } | ||
ThereDrD0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A pre-configured pool specifically for <see cref="List{T}"/> instances. | ||
| /// </summary> | ||
| /// <typeparam name="T">The type of elements in the list.</typeparam> | ||
| public static class ListPool<T> | ||
| { | ||
| static ListPool() | ||
| { | ||
| CollectionPool<List<T>, T>.Configure(() => new List<T>()); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Rents a list from the pool. | ||
| /// </summary> | ||
| /// <returns>A disposable wrapper containing the rented list.</returns> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static CollectionPool<List<T>, T>.PooledCollection Rent() | ||
| => CollectionPool<List<T>, T>.Rent(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A helper pool for renting lists of entities with specific components. | ||
| /// </summary> | ||
| /// <typeparam name="T">The component type associated with the entity.</typeparam> | ||
| public static class ListPoolEntity<T> where T : IComponent | ||
| { | ||
| /// <summary> | ||
| /// Rents a list of <see cref="Entity{T}"/> from the pool. | ||
| /// </summary> | ||
| /// <returns>A disposable wrapper containing the rented entity list.</returns> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static CollectionPool<List<Entity<T>>, Entity<T>>.PooledCollection Rent() | ||
| => ListPool<Entity<T>>.Rent(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A pre-configured pool specifically for <see cref="HashSet{T}"/> instances. | ||
| /// </summary> | ||
| /// <typeparam name="T">The type of elements in the hash set.</typeparam> | ||
| public static class HashSetPool<T> | ||
| { | ||
| static HashSetPool() | ||
| { | ||
| CollectionPool<HashSet<T>, T>.Configure(() => new HashSet<T>()); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Rents a hash set from the pool. | ||
| /// </summary> | ||
| /// <returns>A disposable wrapper containing the rented hash set.</returns> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static CollectionPool<HashSet<T>, T>.PooledCollection Rent() | ||
| => CollectionPool<HashSet<T>, T>.Rent(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A helper pool for renting hash sets of entities with specific components. | ||
| /// </summary> | ||
| /// <typeparam name="T">The component type associated with the entity.</typeparam> | ||
| public static class HashSetPoolEntity<T> where T : IComponent | ||
| { | ||
| /// <summary> | ||
| /// Rents a hash set of <see cref="Entity{T}"/> from the pool. | ||
| /// </summary> | ||
| /// <returns>A disposable wrapper containing the rented entity hash set.</returns> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public static CollectionPool<HashSet<Entity<T>>, Entity<T>>.PooledCollection Rent() | ||
| => HashSetPool<Entity<T>>.Rent(); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.