Initial Commit
This commit is contained in:
parent
53eb92e9af
commit
270ab7d11f
15341 changed files with 700234 additions and 0 deletions
|
@ -0,0 +1,77 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Unity.VisualScripting
|
||||
{
|
||||
public static class ArrayPool<T>
|
||||
{
|
||||
private static readonly object @lock = new object();
|
||||
private static readonly Dictionary<int, Stack<T[]>> free = new Dictionary<int, Stack<T[]>>();
|
||||
private static readonly HashSet<T[]> busy = new HashSet<T[]>();
|
||||
|
||||
public static T[] New(int length)
|
||||
{
|
||||
lock (@lock)
|
||||
{
|
||||
if (!free.ContainsKey(length))
|
||||
{
|
||||
free.Add(length, new Stack<T[]>());
|
||||
}
|
||||
|
||||
if (free[length].Count == 0)
|
||||
{
|
||||
free[length].Push(new T[length]);
|
||||
}
|
||||
|
||||
var array = free[length].Pop();
|
||||
|
||||
busy.Add(array);
|
||||
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Free(T[] array)
|
||||
{
|
||||
lock (@lock)
|
||||
{
|
||||
if (!busy.Contains(array))
|
||||
{
|
||||
throw new ArgumentException("The array to free is not in use by the pool.", nameof(array));
|
||||
}
|
||||
|
||||
for (var i = 0; i < array.Length; i++)
|
||||
{
|
||||
array[i] = default(T);
|
||||
}
|
||||
|
||||
busy.Remove(array);
|
||||
|
||||
free[array.Length].Push(array);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class XArrayPool
|
||||
{
|
||||
public static T[] ToArrayPooled<T>(this IEnumerable<T> source)
|
||||
{
|
||||
var array = ArrayPool<T>.New(source.Count());
|
||||
|
||||
var i = 0;
|
||||
|
||||
foreach (var item in source)
|
||||
{
|
||||
array[i++] = item;
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
public static void Free<T>(this T[] array)
|
||||
{
|
||||
ArrayPool<T>.Free(array);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 5a1d9970af4874af8ba5dc44b00c2b76
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,55 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Unity.VisualScripting
|
||||
{
|
||||
public static class DictionaryPool<TKey, TValue>
|
||||
{
|
||||
private static readonly object @lock = new object();
|
||||
private static readonly Stack<Dictionary<TKey, TValue>> free = new Stack<Dictionary<TKey, TValue>>();
|
||||
private static readonly HashSet<Dictionary<TKey, TValue>> busy = new HashSet<Dictionary<TKey, TValue>>();
|
||||
|
||||
// Do not allow an IDictionary parameter here to avoid allocation on foreach
|
||||
public static Dictionary<TKey, TValue> New(Dictionary<TKey, TValue> source = null)
|
||||
{
|
||||
lock (@lock)
|
||||
{
|
||||
if (free.Count == 0)
|
||||
{
|
||||
free.Push(new Dictionary<TKey, TValue>());
|
||||
}
|
||||
|
||||
var dictionary = free.Pop();
|
||||
|
||||
busy.Add(dictionary);
|
||||
|
||||
if (source != null)
|
||||
{
|
||||
foreach (var kvp in source)
|
||||
{
|
||||
dictionary.Add(kvp.Key, kvp.Value);
|
||||
}
|
||||
}
|
||||
|
||||
return dictionary;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Free(Dictionary<TKey, TValue> dictionary)
|
||||
{
|
||||
lock (@lock)
|
||||
{
|
||||
if (!busy.Contains(dictionary))
|
||||
{
|
||||
throw new ArgumentException("The dictionary to free is not in use by the pool.", nameof(dictionary));
|
||||
}
|
||||
|
||||
dictionary.Clear();
|
||||
|
||||
busy.Remove(dictionary);
|
||||
|
||||
free.Push(dictionary);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b2c3a553ebf314f6d9f657be31dfffcb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,46 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Unity.VisualScripting
|
||||
{
|
||||
public static class GenericPool<T> where T : class, IPoolable
|
||||
{
|
||||
private static readonly object @lock = new object();
|
||||
private static readonly Stack<T> free = new Stack<T>();
|
||||
private static readonly HashSet<T> busy = new HashSet<T>(ReferenceEqualityComparer<T>.Instance);
|
||||
|
||||
public static T New(Func<T> constructor)
|
||||
{
|
||||
lock (@lock)
|
||||
{
|
||||
if (free.Count == 0)
|
||||
{
|
||||
free.Push(constructor());
|
||||
}
|
||||
|
||||
var item = free.Pop();
|
||||
|
||||
item.New();
|
||||
|
||||
busy.Add(item);
|
||||
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Free(T item)
|
||||
{
|
||||
lock (@lock)
|
||||
{
|
||||
if (!busy.Remove(item))
|
||||
{
|
||||
throw new ArgumentException("The item to free is not in use by the pool.", nameof(item));
|
||||
}
|
||||
|
||||
item.Free();
|
||||
|
||||
free.Push(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 56ea34a1497c84735913b875ba6c54cf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,64 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Unity.VisualScripting
|
||||
{
|
||||
public static class HashSetPool<T>
|
||||
{
|
||||
private static readonly object @lock = new object();
|
||||
private static readonly Stack<HashSet<T>> free = new Stack<HashSet<T>>();
|
||||
private static readonly HashSet<HashSet<T>> busy = new HashSet<HashSet<T>>();
|
||||
|
||||
public static HashSet<T> New()
|
||||
{
|
||||
lock (@lock)
|
||||
{
|
||||
if (free.Count == 0)
|
||||
{
|
||||
free.Push(new HashSet<T>());
|
||||
}
|
||||
|
||||
var hashSet = free.Pop();
|
||||
|
||||
busy.Add(hashSet);
|
||||
|
||||
return hashSet;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Free(HashSet<T> hashSet)
|
||||
{
|
||||
lock (@lock)
|
||||
{
|
||||
if (!busy.Remove(hashSet))
|
||||
{
|
||||
throw new ArgumentException("The hash set to free is not in use by the pool.", nameof(hashSet));
|
||||
}
|
||||
|
||||
hashSet.Clear();
|
||||
|
||||
free.Push(hashSet);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class XHashSetPool
|
||||
{
|
||||
public static HashSet<T> ToHashSetPooled<T>(this IEnumerable<T> source)
|
||||
{
|
||||
var hashSet = HashSetPool<T>.New();
|
||||
|
||||
foreach (var item in source)
|
||||
{
|
||||
hashSet.Add(item);
|
||||
}
|
||||
|
||||
return hashSet;
|
||||
}
|
||||
|
||||
public static void Free<T>(this HashSet<T> hashSet)
|
||||
{
|
||||
HashSetPool<T>.Free(hashSet);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4775483b5fa10443292f1bd03d6cdf3d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,8 @@
|
|||
namespace Unity.VisualScripting
|
||||
{
|
||||
public interface IPoolable
|
||||
{
|
||||
void New();
|
||||
void Free();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 44ee32ab645b1426bbe97848d0f17b67
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,66 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Unity.VisualScripting
|
||||
{
|
||||
public static class ListPool<T>
|
||||
{
|
||||
private static readonly object @lock = new object();
|
||||
private static readonly Stack<List<T>> free = new Stack<List<T>>();
|
||||
private static readonly HashSet<List<T>> busy = new HashSet<List<T>>();
|
||||
|
||||
public static List<T> New()
|
||||
{
|
||||
lock (@lock)
|
||||
{
|
||||
if (free.Count == 0)
|
||||
{
|
||||
free.Push(new List<T>());
|
||||
}
|
||||
|
||||
var array = free.Pop();
|
||||
|
||||
busy.Add(array);
|
||||
|
||||
return array;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Free(List<T> list)
|
||||
{
|
||||
lock (@lock)
|
||||
{
|
||||
if (!busy.Contains(list))
|
||||
{
|
||||
throw new ArgumentException("The list to free is not in use by the pool.", nameof(list));
|
||||
}
|
||||
|
||||
list.Clear();
|
||||
|
||||
busy.Remove(list);
|
||||
|
||||
free.Push(list);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class XListPool
|
||||
{
|
||||
public static List<T> ToListPooled<T>(this IEnumerable<T> source)
|
||||
{
|
||||
var list = ListPool<T>.New();
|
||||
|
||||
foreach (var item in source)
|
||||
{
|
||||
list.Add(item);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
public static void Free<T>(this List<T> list)
|
||||
{
|
||||
ListPool<T>.Free(list);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 647aea73c3dcd42329217119e305ea8a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
|
@ -0,0 +1,44 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Unity.VisualScripting
|
||||
{
|
||||
public static class ManualPool<T> where T : class
|
||||
{
|
||||
private static readonly object @lock = new object();
|
||||
private static readonly Stack<T> free = new Stack<T>();
|
||||
private static readonly HashSet<T> busy = new HashSet<T>();
|
||||
|
||||
public static T New(Func<T> constructor)
|
||||
{
|
||||
lock (@lock)
|
||||
{
|
||||
if (free.Count == 0)
|
||||
{
|
||||
free.Push(constructor());
|
||||
}
|
||||
|
||||
var item = free.Pop();
|
||||
|
||||
busy.Add(item);
|
||||
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Free(T item)
|
||||
{
|
||||
lock (@lock)
|
||||
{
|
||||
if (!busy.Contains(item))
|
||||
{
|
||||
throw new ArgumentException("The item to free is not in use by the pool.", nameof(item));
|
||||
}
|
||||
|
||||
busy.Remove(item);
|
||||
|
||||
free.Push(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 3285d7e90c0424169943385822822d8a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Add table
Add a link
Reference in a new issue