feat: Adds tidy option for serialization. Codegens ballotbox. Fixes pooled timer leaking (#681)

* Fixes pooled timer leaking
* Fixes `[dumptimers` command so it outputs properly, adds spacing, and stacktraces
* Adds `[Tidy]` for serializing Lists. This will remove deleted entities during world save before serializing the list.
* Adds helpers for managing Lists/Sets/Dictionaries


### New API

```cs
// Creates the list if it is null, then adds
Utility.Add(ref list, value);
Utility.Add(ref set, value);
Utility.Add(ref dict, key, value);

// Nulls the variable if the count is zero
Utility.Remove(ref list, value);
Utility.Remove(ref set, value);
Utility.Remove(ref dict, key);

// Marks entity as dirty in addition to doing the action
entity.Add(list, value);
// Marks entity as dirty, and will create list if it doesn't exist
entity.Add(ref list, value);

// Marks entity as dirty in addition to doing the action
entity.Remove(list, value);
// Marks entity as dirty, and will null the list count is zero
entity.Remove(ref list, value);
```


### Updates to [dumptimers
<img width="825" alt="Screen Shot 2021-08-14 at 2 55 10 AM" src="https://user-images.githubusercontent.com/3953314/129442449-ccf7fe14-29d6-4f3f-9366-c8eb7b9828a7.png">
This commit is contained in:
Kamron Batman 2021-08-14 03:02:53 -07:00 • committed by GitHub
parent 84cbd52a2a
commit 360143478a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
53 changed files with 627 additions and 464 deletions

View file

@ -1,165 +0,0 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: RefPool.cs *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System;
using System.Collections.Generic;
namespace Server.Utilities
{
/// <summary> A resource reference object that can be disposed. </summary>
/// <remarks>
/// Disposing the reference is expected to return itself back into the
/// original pool that created it.
/// </remarks>
public interface IRef : IDisposable
{
}
/// <summary>
/// Base implementation of the <see cref="IRef" /> interface.
/// </summary>
/// <remarks>
/// New implementations of <see cref="IRef" /> should either derive from, or mirror
/// the functionality of this base implementation.
/// </remarks>
/// <typeparam name="TDerived"></typeparam>
public abstract class BaseRef<TDerived> : IRef where TDerived : IRef
{
private readonly RefPool<TDerived> m_Pool;
public BaseRef(RefPool<TDerived> pool) => m_Pool = pool;
public void Dispose()
{
OnDispose();
m_Pool.Return((TDerived)(object)this);
}
protected abstract void OnDispose();
}
/// <summary>
/// A resource reference pool that manages a collection of reusable resources.
/// </summary>
/// <typeparam name="TRef">The <see cref="IRef" /> resource type the pool will contain.</typeparam>
public class RefPool<TRef> where TRef : IRef
{
public delegate TRef Generator(RefPool<TRef> targetPool);
public const int DEFAULT_RESOURCE_RETENTION = 10;
private readonly Generator m_Generator;
private readonly Stack<TRef> m_Resources = new();
private int m_MaxRefrenceRetention;
/// <param name="generator">The generator function for creating new resources.</param>
/// <param name="preGenerateCount">
/// An amount of resources that should be pre-generated during initialization of the resource
/// pool.
/// </param>
public RefPool(Generator generator, int preGenerateCount = 0, int maxRefrenceRetention = DEFAULT_RESOURCE_RETENTION)
{
if (generator == null)
{
throw new ArgumentNullException(nameof(generator));
}
if (preGenerateCount > maxRefrenceRetention)
{
throw new IndexOutOfRangeException(
$"{nameof(preGenerateCount)} greater than {nameof(maxRefrenceRetention)}"
);
}
m_Generator = generator;
m_MaxRefrenceRetention = maxRefrenceRetention;
while (--preGenerateCount >= 0)
{
m_Resources.Push(generator(this));
}
}
/// <summary>
/// The maximum number of unused resources to hold in the pool.
/// </summary>
public int MaxRefrenceRetention
{
get => m_MaxRefrenceRetention;
set
{
m_MaxRefrenceRetention = value;
while (m_Resources.Count > value)
{
m_Resources.Pop();
}
}
}
/// <summary>
/// Retrieves a resource reference that is managed by this <see cref="RefPool{TRef}" />. If the pool is has unused
/// resources,
/// it will remove one from the pool and return it; otherwise, a new resource will be generated.
/// </summary>
/// <returns>Unused resource, or a new resource if no unused resources available.</returns>
public TRef Get() => m_Resources.TryPop(out var item) ? item : m_Generator(this);
/// <summary>
/// Returns a resource reference to the pool of unused resources.
/// </summary>
/// <param name="queueRef">Resource to be returned.</param>
public void Return(TRef queueRef)
{
if (m_Resources.Count < MaxRefrenceRetention)
{
m_Resources.Push(queueRef);
}
}
}
public class QueueRef<T> : Queue<T>, IRef
{
/// <summary>
/// Generator function for creating instances of the <see cref="QueueRef{T}" /> resource.
/// </summary>
public static RefPool<QueueRef<T>>.Generator Generate = targetPool => new QueueRef<T>(targetPool);
private readonly RefPool<QueueRef<T>> m_Pool;
private QueueRef(RefPool<QueueRef<T>> pool) => m_Pool = pool;
/// <summary>Clears the queue and returns this resource to its parent resource pool.</summary>
public void Dispose()
{
Clear();
m_Pool.Return(this);
}
}
public class StackRef<T> : Stack<T>, IRef
{
/// <summary>
/// Generator function for creating instances of the <see cref="StackRef{T}" /> resource.
/// </summary>
public static RefPool<StackRef<T>>.Generator Generate = targetPool => new StackRef<T>(targetPool);
private readonly RefPool<StackRef<T>> m_Pool;
private StackRef(RefPool<StackRef<T>> pool) => m_Pool = pool;
/// <summary>Clears the stack and returns this resource to its parent resource pool.</summary>
public void Dispose()
{
Clear();
m_Pool.Return(this);
}
}
}

View file

@ -1492,5 +1492,159 @@ namespace Server
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string GetTimeStamp() => Core.Now.ToString("yyyy-MM-dd-HH-mm-ss");
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Add<T>(ref List<T> list, T value)
{
list ??= new List<T>();
list.Add(value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Add<T>(ref HashSet<T> set, T value)
{
set ??= new HashSet<T>();
set.Add(value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Add<K, V>(ref Dictionary<K, V> dict, K key, V value)
{
dict ??= new Dictionary<K, V>();
dict.Add(key, value);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Remove<T>(ref List<T> list, T value)
{
if (list != null)
{
var removed = list.Remove(value);
if (list.Count == 0)
{
list = null;
}
return removed;
}
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Remove<T>(ref HashSet<T> set, T value)
{
if (set != null)
{
var removed = set.Remove(value);
if (set.Count == 0)
{
set = null;
}
return removed;
}
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Remove<K, V>(ref Dictionary<K, V> dict, K key)
{
if (dict != null)
{
var removed = dict.Remove(key);
if (dict.Count == 0)
{
dict = null;
}
return removed;
}
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Remove<K, V>(ref Dictionary<K, V> dict, K key, out V value)
{
if (dict != null)
{
var removed = dict.Remove(key, out value);
if (dict.Count == 0)
{
dict = null;
}
return removed;
}
value = default;
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Replace<T>(ref List<T> list, T oldValue, T newValue)
{
if (oldValue != null && newValue != null)
{
var index = list?.IndexOf(oldValue) ?? -1;
if (index >= 0)
{
list![index] = newValue;
}
else
{
Add(ref list, newValue);
}
}
else if (oldValue != null)
{
Remove(ref list, oldValue);
}
else if (newValue != null)
{
Add(ref list, newValue);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Replace<K, V>(ref Dictionary<K, V> dict, K key, V oldValue, V newValue)
{
if (newValue != null)
{
Add(ref dict, key, newValue);
}
else if (oldValue != null)
{
Remove(ref dict, key);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Clear<T>(ref List<T> list)
{
list.Clear();
list = null;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Clear<T>(ref HashSet<T> set)
{
set.Clear();
set = null;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Clear<K, V>(ref Dictionary<K, V> dict)
{
dict.Clear();
dict = null;
}
}
}