Extensions.cs 6.1 KB
Newer Older
1
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the Apache License, Version 2.0.  See License.txt in the project root for license information.
P
Pilchie 已提交
2 3

using System.Collections.Generic;
4
using System.Diagnostics;
P
Pilchie 已提交
5
using System.Text;
T
Tomas Matousek 已提交
6
using Microsoft.CodeAnalysis.PooledObjects;
P
Pilchie 已提交
7

T
Tomas Matousek 已提交
8
namespace Microsoft.CodeAnalysis
P
Pilchie 已提交
9 10 11 12 13
{
    internal static class SharedPoolExtensions
    {
        private const int Threshold = 512;

14 15 16 17 18
        public static PooledObject<StringBuilder> GetPooledObject(this ObjectPool<StringBuilder> pool)
        {
            return PooledObject<StringBuilder>.Create(pool);
        }

19 20 21 22 23
        public static PooledObject<Stopwatch> GetPooledObject(this ObjectPool<Stopwatch> pool)
        {
            return PooledObject<Stopwatch>.Create(pool);
        }

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
        public static PooledObject<Stack<TItem>> GetPooledObject<TItem>(this ObjectPool<Stack<TItem>> pool)
        {
            return PooledObject<Stack<TItem>>.Create(pool);
        }

        public static PooledObject<Queue<TItem>> GetPooledObject<TItem>(this ObjectPool<Queue<TItem>> pool)
        {
            return PooledObject<Queue<TItem>>.Create(pool);
        }

        public static PooledObject<HashSet<TItem>> GetPooledObject<TItem>(this ObjectPool<HashSet<TItem>> pool)
        {
            return PooledObject<HashSet<TItem>>.Create(pool);
        }

        public static PooledObject<Dictionary<TKey, TValue>> GetPooledObject<TKey, TValue>(this ObjectPool<Dictionary<TKey, TValue>> pool)
        {
            return PooledObject<Dictionary<TKey, TValue>>.Create(pool);
        }

        public static PooledObject<List<TItem>> GetPooledObject<TItem>(this ObjectPool<List<TItem>> pool)
        {
            return PooledObject<List<TItem>>.Create(pool);
        }

C
Cyrus Najmabadi 已提交
49 50 51 52 53 54 55
        public static PooledObject<List<TItem>> GetPooledObject<TItem>(this ObjectPool<List<TItem>> pool, out List<TItem> list)
        {
            var pooledObject = PooledObject<List<TItem>>.Create(pool);
            list = pooledObject.Object;
            return pooledObject;
        }

56 57 58 59 60
        public static PooledObject<T> GetPooledObject<T>(this ObjectPool<T> pool) where T : class
        {
            return new PooledObject<T>(pool, p => p.Allocate(), (p, o) => p.Free(o));
        }

P
Pilchie 已提交
61 62 63 64 65 66 67 68
        public static StringBuilder AllocateAndClear(this ObjectPool<StringBuilder> pool)
        {
            var sb = pool.Allocate();
            sb.Clear();

            return sb;
        }

69 70 71 72 73 74 75 76
        public static Stopwatch AllocateAndClear(this ObjectPool<Stopwatch> pool)
        {
            var watch = pool.Allocate();
            watch.Reset();

            return watch;
        }

P
Pilchie 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
        public static Stack<T> AllocateAndClear<T>(this ObjectPool<Stack<T>> pool)
        {
            var set = pool.Allocate();
            set.Clear();

            return set;
        }

        public static Queue<T> AllocateAndClear<T>(this ObjectPool<Queue<T>> pool)
        {
            var set = pool.Allocate();
            set.Clear();

            return set;
        }

        public static HashSet<T> AllocateAndClear<T>(this ObjectPool<HashSet<T>> pool)
        {
            var set = pool.Allocate();
            set.Clear();

            return set;
        }

        public static Dictionary<TKey, TValue> AllocateAndClear<TKey, TValue>(this ObjectPool<Dictionary<TKey, TValue>> pool)
        {
            var map = pool.Allocate();
            map.Clear();

            return map;
        }

        public static List<T> AllocateAndClear<T>(this ObjectPool<List<T>> pool)
        {
            var list = pool.Allocate();
            list.Clear();

            return list;
        }

        public static void ClearAndFree(this ObjectPool<StringBuilder> pool, StringBuilder sb)
        {
            if (sb == null)
            {
                return;
            }

            sb.Clear();

            if (sb.Capacity > Threshold)
            {
                sb.Capacity = Threshold;
            }

            pool.Free(sb);
        }

134 135 136 137 138 139 140 141 142 143 144
        public static void ClearAndFree(this ObjectPool<Stopwatch> pool, Stopwatch watch)
        {
            if (watch == null)
            {
                return;
            }

            watch.Reset();
            pool.Free(watch);
        }

P
Pilchie 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
        public static void ClearAndFree<T>(this ObjectPool<HashSet<T>> pool, HashSet<T> set)
        {
            if (set == null)
            {
                return;
            }

            var count = set.Count;
            set.Clear();

            if (count > Threshold)
            {
                set.TrimExcess();
            }

            pool.Free(set);
        }

        public static void ClearAndFree<T>(this ObjectPool<Stack<T>> pool, Stack<T> set)
        {
            if (set == null)
            {
                return;
            }

            var count = set.Count;
            set.Clear();

            if (count > Threshold)
            {
                set.TrimExcess();
            }

            pool.Free(set);
        }

        public static void ClearAndFree<T>(this ObjectPool<Queue<T>> pool, Queue<T> set)
        {
            if (set == null)
            {
                return;
            }

            var count = set.Count;
            set.Clear();

            if (count > Threshold)
            {
                set.TrimExcess();
            }

            pool.Free(set);
        }

        public static void ClearAndFree<TKey, TValue>(this ObjectPool<Dictionary<TKey, TValue>> pool, Dictionary<TKey, TValue> map)
        {
            if (map == null)
            {
                return;
            }

206 207 208 209 210 211 212
            // if map grew too big, don't put it back to pool
            if (map.Count > Threshold)
            {
                pool.ForgetTrackedObject(map);
                return;
            }

P
Pilchie 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
            map.Clear();
            pool.Free(map);
        }

        public static void ClearAndFree<T>(this ObjectPool<List<T>> pool, List<T> list)
        {
            if (list == null)
            {
                return;
            }

            list.Clear();

            if (list.Capacity > Threshold)
            {
                list.Capacity = Threshold;
            }

            pool.Free(list);
        }
    }
}