Skip to content

Instantly share code, notes, and snippets.

@mkrueger
Created June 24, 2019 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkrueger/4abeee034a64cbb366f829e33de2c731 to your computer and use it in GitHub Desktop.
Save mkrueger/4abeee034a64cbb366f829e33de2c731 to your computer and use it in GitHub Desktop.
//
// CollectionCache.cs
//
// Author:
// Mike Krüger <mikkrg@microsoft.com>
//
// Copyright (c) 2019 Microsoft Corporation. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using Microsoft.Extensions.ObjectPool;
namespace MonoDevelop.Core
{
public static class CollectionCache
{
public static List<T> AllocateList<T> () => Pool<T, List<T>>.Allocate ();
public static void Free<T> (List<T> obj) => Pool<T, List<T>>.Deallocate (obj);
public static HashSet<T> AllocateHashSet<T> () => Pool<T, HashSet<T>>.Allocate ();
public static void Free<T> (HashSet<T> obj) => Pool<T, HashSet<T>>.Deallocate (obj);
public static Dictionary<T, U> AllocateDictionary<T, U> () => Pool<KeyValuePair<T, U>, Dictionary<T, U>>.Allocate ();
public static void Free<T, U> (Dictionary<T, U> obj) => Pool<KeyValuePair<T, U>, Dictionary<T, U>>.Deallocate (obj);
static class Pool<T, U> where U : class, ICollection<T>, new()
{
static readonly ObjectPool<U> pool = new DefaultObjectPoolProvider ().Create (new PooledObjectPolicy ());
internal static U Allocate () => pool.Get ();
internal static void Deallocate (U collection)
{
if (collection == null)
throw new ArgumentNullException (nameof (collection));
pool.Return (collection);
}
class PooledObjectPolicy : PooledObjectPolicy<U>
{
public override U Create () => new U ();
public override bool Return (U obj)
{
obj.Clear ();
return true;
}
}
}
public static Stack<T> AllocateStack<T> () => StackPool<T>.Allocate ();
public static void Free<T> (Stack<T> obj) => StackPool<T>.Deallocate (obj);
static class StackPool<T>
{
static readonly ObjectPool<Stack<T>> pool = new DefaultObjectPoolProvider ().Create (new PooledObjectPolicy ());
internal static Stack<T> Allocate () => pool.Get ();
internal static void Deallocate (Stack<T> stack)
{
if (stack == null)
throw new ArgumentNullException (nameof (stack));
pool.Return (stack);
}
class PooledObjectPolicy : PooledObjectPolicy<Stack<T>>
{
public override Stack<T> Create () => new Stack<T> ();
public override bool Return (Stack<T> obj)
{
obj.Clear ();
return true;
}
}
}
public static Queue<T> AllocateQueue<T> () => QueuePool<T>.Allocate ();
public static void Free<T> (Queue<T> obj) => QueuePool<T>.Deallocate (obj);
static class QueuePool<T>
{
static readonly ObjectPool<Queue<T>> pool = new DefaultObjectPoolProvider ().Create (new PooledObjectPolicy ());
internal static Queue<T> Allocate () => pool.Get ();
internal static void Deallocate (Queue<T> queue)
{
if (queue == null)
throw new ArgumentNullException (nameof (queue));
pool.Return (queue);
}
class PooledObjectPolicy : PooledObjectPolicy<Queue<T>>
{
public override Queue<T> Create () => new Queue<T> ();
public override bool Return (Queue<T> obj)
{
obj.Clear ();
return true;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment