|
| 1 | +// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +namespace StyleCop.Analyzers.Helpers.ObjectPools |
| 5 | +{ |
| 6 | + // This code was copied from the Roslyn code base (and slightly modified) |
| 7 | + using System; |
| 8 | + using System.Diagnostics; |
| 9 | + using System.Threading; |
| 10 | + |
| 11 | + /// <summary> |
| 12 | + /// Generic implementation of object pooling pattern with predefined pool size limit. The main |
| 13 | + /// purpose is that limited number of frequently used objects can be kept in the pool for |
| 14 | + /// further recycling. |
| 15 | + /// |
| 16 | + /// Notes: |
| 17 | + /// 1) it is not the goal to keep all returned objects. Pool is not meant for storage. If there |
| 18 | + /// is no space in the pool, extra returned objects will be dropped. |
| 19 | + /// |
| 20 | + /// 2) it is implied that if object was obtained from a pool, the caller will return it back in |
| 21 | + /// a relatively short time. Keeping checked out objects for long durations is ok, but |
| 22 | + /// reduces usefulness of pooling. Just new up your own. |
| 23 | + /// |
| 24 | + /// Not returning objects to the pool in not detrimental to the pool's work, but is a bad practice. |
| 25 | + /// Rationale: |
| 26 | + /// If there is no intent for reusing the object, do not use pool - just use "new". |
| 27 | + /// </summary> |
| 28 | + /// <typeparam name="T">The type of the objects in this cache.</typeparam> |
| 29 | + internal class ObjectPool<T> |
| 30 | + where T : class |
| 31 | + { |
| 32 | + private readonly Element[] items; |
| 33 | + |
| 34 | + // factory is stored for the lifetime of the pool. We will call this only when pool needs to |
| 35 | + // expand. compared to "new T()", Func gives more flexibility to implementers and faster |
| 36 | + // than "new T()". |
| 37 | + private readonly Func<T> factory; |
| 38 | + |
| 39 | + // Storage for the pool objects. The first item is stored in a dedicated field because we |
| 40 | + // expect to be able to satisfy most requests from it. |
| 41 | + private T firstItem; |
| 42 | + |
| 43 | + internal ObjectPool(Func<T> factory) |
| 44 | + : this(factory, Environment.ProcessorCount * 2) |
| 45 | + { |
| 46 | + } |
| 47 | + |
| 48 | + internal ObjectPool(Func<T> factory, int size) |
| 49 | + { |
| 50 | + Debug.Assert(size >= 1, "The object pool can't be empty"); |
| 51 | + this.factory = factory; |
| 52 | + this.items = new Element[size - 1]; |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Produces an instance. |
| 57 | + /// </summary> |
| 58 | + /// <remarks> |
| 59 | + /// Search strategy is a simple linear probing which is chosen for it cache-friendliness. |
| 60 | + /// Note that Free will try to store recycled objects close to the start thus statistically |
| 61 | + /// reducing how far we will typically search. |
| 62 | + /// </remarks> |
| 63 | + /// <returns>A (poosibly) cached instance of type <typeparamref name="T"/>.</returns> |
| 64 | + internal T Allocate() |
| 65 | + { |
| 66 | + // PERF: Examine the first element. If that fails, AllocateSlow will look at the remaining elements. |
| 67 | + // Note that the initial read is optimistically not synchronized. That is intentional. |
| 68 | + // We will interlock only when we have a candidate. in a worst case we may miss some |
| 69 | + // recently returned objects. Not a big deal. |
| 70 | + T inst = this.firstItem; |
| 71 | + if (inst == null || inst != Interlocked.CompareExchange(ref this.firstItem, null, inst)) |
| 72 | + { |
| 73 | + inst = this.AllocateSlow(); |
| 74 | + } |
| 75 | + |
| 76 | + return inst; |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Returns objects to the pool. |
| 81 | + /// </summary> |
| 82 | + /// <remarks> |
| 83 | + /// Search strategy is a simple linear probing which is chosen for it cache-friendliness. |
| 84 | + /// Note that Free will try to store recycled objects close to the start thus statistically |
| 85 | + /// reducing how far we will typically search in Allocate. |
| 86 | + /// </remarks> |
| 87 | + /// <param name="obj">The object to free.</param> |
| 88 | + internal void Free(T obj) |
| 89 | + { |
| 90 | + if (this.firstItem == null) |
| 91 | + { |
| 92 | + // Intentionally not using interlocked here. |
| 93 | + // In a worst case scenario two objects may be stored into same slot. |
| 94 | + // It is very unlikely to happen and will only mean that one of the objects will get collected. |
| 95 | + this.firstItem = obj; |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + this.FreeSlow(obj); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private T CreateInstance() |
| 104 | + { |
| 105 | + var inst = this.factory(); |
| 106 | + return inst; |
| 107 | + } |
| 108 | + |
| 109 | + private T AllocateSlow() |
| 110 | + { |
| 111 | + var items = this.items; |
| 112 | + |
| 113 | + for (int i = 0; i < items.Length; i++) |
| 114 | + { |
| 115 | + // Note that the initial read is optimistically not synchronized. That is intentional. |
| 116 | + // We will interlock only when we have a candidate. in a worst case we may miss some |
| 117 | + // recently returned objects. Not a big deal. |
| 118 | + T inst = items[i].Value; |
| 119 | + if (inst != null) |
| 120 | + { |
| 121 | + if (inst == Interlocked.CompareExchange(ref items[i].Value, null, inst)) |
| 122 | + { |
| 123 | + return inst; |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return this.CreateInstance(); |
| 129 | + } |
| 130 | + |
| 131 | + private void FreeSlow(T obj) |
| 132 | + { |
| 133 | + var items = this.items; |
| 134 | + for (int i = 0; i < items.Length; i++) |
| 135 | + { |
| 136 | + if (items[i].Value == null) |
| 137 | + { |
| 138 | + // Intentionally not using interlocked here. |
| 139 | + // In a worst case scenario two objects may be stored into same slot. |
| 140 | + // It is very unlikely to happen and will only mean that one of the objects will get collected. |
| 141 | + items[i].Value = obj; |
| 142 | + break; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + [DebuggerDisplay("{Value,nq}")] |
| 148 | + private struct Element |
| 149 | + { |
| 150 | + internal T Value; |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments