diff options
author | therzok <marius.ungureanu@xamarin.com> | 2019-06-20 16:36:30 +0300 |
---|---|---|
committer | therzok <marius.ungureanu@xamarin.com> | 2019-06-20 16:43:16 +0300 |
commit | 682372f496c10759409f7e51fe4256c47bd666d5 (patch) | |
tree | 89a4b25ff8db5d66ca58cba777fab4079581c6b5 /main/src/core/MonoDevelop.Core | |
parent | 0ce71ae568b2782bdb18e990145b40e98cbdf975 (diff) |
[Core] Refactor a bit of list pooling to optimize for common case
Diffstat (limited to 'main/src/core/MonoDevelop.Core')
3 files changed, 56 insertions, 3 deletions
diff --git a/main/src/core/MonoDevelop.Core/MonoDevelop.Core.csproj b/main/src/core/MonoDevelop.Core/MonoDevelop.Core.csproj index 49fdc36721..e9ffce8362 100644 --- a/main/src/core/MonoDevelop.Core/MonoDevelop.Core.csproj +++ b/main/src/core/MonoDevelop.Core/MonoDevelop.Core.csproj @@ -733,6 +733,7 @@ <Compile Include="MonoDevelop.Projects.MSBuild\MSBuildSdkProject.cs" /> <Compile Include="MonoDevelop.Projects\SdkProjectShortTargetFramework.cs" /> <Compile Include="MonoDevelop.Utilities\SampleProfiler.cs" /> + <Compile Include="MonoDevelop.Core\PooledListPolicy.cs" /> </ItemGroup> <ItemGroup> <None Include="BuildVariables.cs.in" /> diff --git a/main/src/core/MonoDevelop.Core/MonoDevelop.Core/PooledListPolicy.cs b/main/src/core/MonoDevelop.Core/MonoDevelop.Core/PooledListPolicy.cs new file mode 100644 index 0000000000..a6d88f3df6 --- /dev/null +++ b/main/src/core/MonoDevelop.Core/MonoDevelop.Core/PooledListPolicy.cs @@ -0,0 +1,48 @@ +// +// PooledListPolicy.cs +// +// Author: +// Marius Ungureanu <maungu@microsoft.com> +// +// Copyright (c) 2019 Microsoft Inc. +// +// 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 +{ + sealed class PooledListPolicy<T> : PooledObjectPolicy<List<T>> + { + public int InitialCapacity { get; set; } = 8; + public int MaximumRetainedCapacity { get; set; } = 32; + + public override List<T> Create () => new List<T> (InitialCapacity); + + public override bool Return (List<T> obj) + { + if (obj.Capacity > MaximumRetainedCapacity) + obj.Capacity = MaximumRetainedCapacity; + + obj.Clear (); + return true; + } + } +} diff --git a/main/src/core/MonoDevelop.Core/MonoDevelop.Projects/Project.cs b/main/src/core/MonoDevelop.Core/MonoDevelop.Projects/Project.cs index 1054338175..45139f8c57 100644 --- a/main/src/core/MonoDevelop.Core/MonoDevelop.Projects/Project.cs +++ b/main/src/core/MonoDevelop.Core/MonoDevelop.Projects/Project.cs @@ -4614,7 +4614,8 @@ namespace MonoDevelop.Projects }); } - static readonly ObjectPool<List<(FilePath, ProjectItem)>> projectItemListPool = ObjectPool.Create<List<(FilePath, ProjectItem)>> (); + static readonly ObjectPool<List<(FilePath, ProjectItem)>> projectItemListPool + = ObjectPool.Create (new PooledListPolicy<(FilePath, ProjectItem)> { MaximumRetainedCapacity = 8, InitialCapacity = 4 }); void OnFileCreatedExternally (FilePath fileName) { @@ -4649,15 +4650,19 @@ namespace MonoDevelop.Projects if (!UseAdvancedGlobSupport) globItems = globItems.Where (it => !it.Metadata.GetProperties ().Any ()); - var list = projectItemListPool.Get (); + List<(FilePath, ProjectItem)> list = null; foreach (var it in globItems) { var eit = CreateFakeEvaluatedItem (sourceProject, it, include, null); var pi = CreateProjectItem (eit); pi.Read (this, eit); + list ??= projectItemListPool.Get (); list.Add ((fileName, pi)); } + if (list == null) + return; + Runtime.RunInMainThread (() => { // Double check the file has not been added on the UI thread by the IDE. try { @@ -4665,7 +4670,6 @@ namespace MonoDevelop.Projects if (list.Count > 0) Items.AddRange (list.Select (item => item.Item2)); } finally { - list.Clear (); projectItemListPool.Return (list); } }).Ignore (); |