-
-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #957 from MattKotsenas/refactor/lookahead-allocs
Eliminate allocations from CharacterAnalyzer<StringLookAheadBuffer> +semver:feature
- Loading branch information
Showing
16 changed files
with
506 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// This file is part of YamlDotNet - A .NET library for YAML. | ||
// Copyright (c) Antoine Aubry and contributors | ||
// | ||
// 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. | ||
|
||
|
||
// Adapted from https://github.com/dotnet/aspnetcore/blob/2f1db20456007c9515068a35a65afdf99af70bc6/src/ObjectPool/src/DefaultObjectPool.cs which is | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Threading; | ||
|
||
namespace YamlDotNet.Core.ObjectPool | ||
{ | ||
/// <summary> | ||
/// Default implementation of <see cref="ObjectPool{T}"/>. | ||
/// </summary> | ||
/// <typeparam name="T">The type to pool objects for.</typeparam> | ||
/// <remarks>This implementation keeps a cache of retained objects. This means that if objects are returned when the pool has already reached "maximumRetained" objects they will be available to be Garbage Collected.</remarks> | ||
internal class DefaultObjectPool<T> : ObjectPool<T> where T : class | ||
{ | ||
private readonly Func<T> createFunc; | ||
private readonly Func<T, bool> returnFunc; | ||
private readonly int maxCapacity; | ||
private int numItems; | ||
|
||
private protected readonly ConcurrentQueue<T> items = new ConcurrentQueue<T>(); | ||
private protected T? fastItem; | ||
|
||
/// <summary> | ||
/// Creates an instance of <see cref="DefaultObjectPool{T}"/>. | ||
/// </summary> | ||
/// <param name="policy">The pooling policy to use.</param> | ||
public DefaultObjectPool(IPooledObjectPolicy<T> policy) | ||
: this(policy, Environment.ProcessorCount * 2) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates an instance of <see cref="DefaultObjectPool{T}"/>. | ||
/// </summary> | ||
/// <param name="policy">The pooling policy to use.</param> | ||
/// <param name="maximumRetained">The maximum number of objects to retain in the pool.</param> | ||
public DefaultObjectPool(IPooledObjectPolicy<T> policy, int maximumRetained) | ||
{ | ||
// cache the target interface methods, to avoid interface lookup overhead | ||
createFunc = policy.Create; | ||
returnFunc = policy.Return; | ||
maxCapacity = maximumRetained - 1; // -1 to account for fastItem | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override T Get() | ||
{ | ||
var item = fastItem; | ||
if (item == null || Interlocked.CompareExchange(ref fastItem, null, item) != item) | ||
{ | ||
if (items.TryDequeue(out item)) | ||
{ | ||
Interlocked.Decrement(ref numItems); | ||
return item; | ||
} | ||
|
||
// no object available, so go get a brand new one | ||
return createFunc(); | ||
} | ||
|
||
return item; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void Return(T obj) | ||
{ | ||
ReturnCore(obj); | ||
} | ||
|
||
/// <summary> | ||
/// Returns an object to the pool. | ||
/// </summary> | ||
/// <returns>true if the object was returned to the pool</returns> | ||
private protected bool ReturnCore(T obj) | ||
{ | ||
if (!returnFunc(obj)) | ||
{ | ||
// policy says to drop this object | ||
return false; | ||
} | ||
|
||
if (fastItem != null || Interlocked.CompareExchange(ref fastItem, obj, null) != null) | ||
{ | ||
if (Interlocked.Increment(ref numItems) <= maxCapacity) | ||
{ | ||
items.Enqueue(obj); | ||
return true; | ||
} | ||
|
||
// no room, clean up the count and drop the object on the floor | ||
Interlocked.Decrement(ref numItems); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// This file is part of YamlDotNet - A .NET library for YAML. | ||
// Copyright (c) Antoine Aubry and contributors | ||
// | ||
// 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. | ||
|
||
|
||
// Adapted from https://github.com/dotnet/aspnetcore/blob/2f1db20456007c9515068a35a65afdf99af70bc6/src/ObjectPool/src/DefaultObjectPool.cs which is | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace YamlDotNet.Core.ObjectPool | ||
{ | ||
/// <summary> | ||
/// Default implementation for <see cref="PooledObjectPolicy{T}"/>. | ||
/// </summary> | ||
/// <typeparam name="T">The type of object which is being pooled.</typeparam> | ||
internal class DefaultPooledObjectPolicy<T> : IPooledObjectPolicy<T> where T : class, new() | ||
{ | ||
/// <inheritdoc /> | ||
public T Create() | ||
{ | ||
return new T(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool Return(T obj) | ||
{ | ||
if (obj is IResettable resettable) | ||
{ | ||
return resettable.TryReset(); | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// This file is part of YamlDotNet - A .NET library for YAML. | ||
// Copyright (c) Antoine Aubry and contributors | ||
// | ||
// 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. | ||
|
||
|
||
// Adapted from https://github.com/dotnet/aspnetcore/blob/2f1db20456007c9515068a35a65afdf99af70bc6/src/ObjectPool/src/DefaultObjectPool.cs which is | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace YamlDotNet.Core.ObjectPool | ||
{ | ||
/// <summary> | ||
/// Represents a policy for managing pooled objects. | ||
/// </summary> | ||
/// <typeparam name="T">The type of object which is being pooled.</typeparam> | ||
internal interface IPooledObjectPolicy<T> where T : notnull | ||
{ | ||
/// <summary> | ||
/// Create a <typeparamref name="T"/>. | ||
/// </summary> | ||
/// <returns>The <typeparamref name="T"/> which was created.</returns> | ||
T Create(); | ||
|
||
/// <summary> | ||
/// Runs some processing when an object was returned to the pool. Can be used to reset the state of an object and indicate if the object should be returned to the pool. | ||
/// </summary> | ||
/// <param name="obj">The object to return to the pool.</param> | ||
/// <returns><see langword="true" /> if the object should be returned to the pool. <see langword="false" /> if it's not possible/desirable for the pool to keep the object.</returns> | ||
bool Return(T obj); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// This file is part of YamlDotNet - A .NET library for YAML. | ||
// Copyright (c) Antoine Aubry and contributors | ||
// | ||
// 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. | ||
|
||
|
||
// Adapted from https://github.com/dotnet/aspnetcore/blob/2f1db20456007c9515068a35a65afdf99af70bc6/src/ObjectPool/src/DefaultObjectPool.cs which is | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace YamlDotNet.Core.ObjectPool | ||
{ | ||
/// <summary> | ||
/// Defines a method to reset an object to its initial state. | ||
/// </summary> | ||
internal interface IResettable | ||
{ | ||
/// <summary> | ||
/// Reset the object to a neutral state, semantically similar to when the object was first constructed. | ||
/// </summary> | ||
/// <returns><see langword="true" /> if the object was able to reset itself, otherwise <see langword="false" />.</returns> | ||
/// <remarks> | ||
/// In general, this method is not expected to be thread-safe. | ||
/// </remarks> | ||
bool TryReset(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// This file is part of YamlDotNet - A .NET library for YAML. | ||
// Copyright (c) Antoine Aubry and contributors | ||
// | ||
// 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. | ||
|
||
|
||
// Adapted from https://github.com/dotnet/aspnetcore/blob/2f1db20456007c9515068a35a65afdf99af70bc6/src/ObjectPool/src/DefaultObjectPool.cs which is | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace YamlDotNet.Core.ObjectPool | ||
{ | ||
/// <summary> | ||
/// A pool of objects. | ||
/// </summary> | ||
/// <typeparam name="T">The type of objects to pool.</typeparam> | ||
internal abstract class ObjectPool<T> where T : class | ||
{ | ||
/// <summary> | ||
/// Gets an object from the pool if one is available, otherwise creates one. | ||
/// </summary> | ||
/// <returns>A <typeparamref name="T"/>.</returns> | ||
public abstract T Get(); | ||
|
||
/// <summary> | ||
/// Return an object to the pool. | ||
/// </summary> | ||
/// <param name="obj">The object to add to the pool.</param> | ||
public abstract void Return(T obj); | ||
} | ||
|
||
/// <summary> | ||
/// Methods for creating <see cref="ObjectPool{T}"/> instances. | ||
/// </summary> | ||
internal static class ObjectPool | ||
{ | ||
/// <summary> | ||
/// Create a new <see cref="ObjectPool{T}"/> instance using the default implementation. | ||
/// </summary> | ||
/// <typeparam name="T">The type of object to be pooled</typeparam> | ||
/// <param name="policy">The <see cref="IPooledObjectPolicy{T}"/> to use (or <see cref="DefaultPooledObjectPolicy{T}"/> if <see langword="null"/>).</param> | ||
/// <returns>An instance of <see cref="DefaultObjectPool{T}"/> with the specified policy.</returns> | ||
public static ObjectPool<T> Create<T>(IPooledObjectPolicy<T>? policy = null) where T : class, new() | ||
{ | ||
return new DefaultObjectPool<T>(policy ?? new DefaultPooledObjectPolicy<T>()); | ||
} | ||
|
||
/// <inheritdoc cref="Create{T}(IPooledObjectPolicy{T}?)" /> | ||
/// <param name="maximumRetained">The maximum number of objects to retain in the pool.</param> | ||
/// <param name="policy">The <see cref="IPooledObjectPolicy{T}"/> to use (or <see cref="DefaultPooledObjectPolicy{T}"/> if <see langword="null"/>).</param> | ||
public static ObjectPool<T> Create<T>(int maximumRetained, IPooledObjectPolicy<T>? policy = null) where T : class, new() | ||
{ | ||
return new DefaultObjectPool<T>(policy ?? new DefaultPooledObjectPolicy<T>(), maximumRetained); | ||
} | ||
} | ||
} |
Oops, something went wrong.