Skip to content

Commit

Permalink
Use caches
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfsck committed Oct 22, 2017
1 parent b7c6807 commit 591a4bb
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,21 @@ public static DmdTypeName Create(DmdType type) {
if ((object)declType.DeclaringType == null)
return new DmdTypeName(declType.MetadataNamespace, declType.MetadataName, type.Name);

var list = new List<DmdType>();
var list = ListCache<DmdType>.AllocList();
for (;;) {
if ((object)type.DeclaringType == null)
break;
list.Add(type);
type = type.DeclaringType;
}
var sb = new StringBuilder();
var sb = ObjectCache.AllocStringBuilder();
for (int i = list.Count - 1; i >= 0; i--) {
if (i != list.Count - 1)
sb.Append('+');
sb.Append(list[i].MetadataName);
}
return new DmdTypeName(type.MetadataNamespace, type.MetadataName, sb.ToString());
ListCache<DmdType>.Free(ref list);
return new DmdTypeName(type.MetadataNamespace, type.MetadataName, ObjectCache.FreeAndToString(ref sb));
}

return new DmdTypeName(null, string.Empty);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright (C) 2014-2017 de4dot@gmail.com
This file is part of dnSpy
dnSpy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
dnSpy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with dnSpy. If not, see <http://www.gnu.org/licenses/>.
*/

using System.Collections.Generic;
using System.Threading;

namespace dnSpy.Debugger.DotNet.Metadata {
static class ListCache<T> {
static volatile List<T> cachedList;
public static List<T> AllocList() => Interlocked.Exchange(ref cachedList, null) ?? new List<T>();
public static void Free(ref List<T> list) {
list.Clear();
cachedList = list;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright (C) 2014-2017 de4dot@gmail.com
This file is part of dnSpy
dnSpy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
dnSpy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with dnSpy. If not, see <http://www.gnu.org/licenses/>.
*/

using System.Text;
using System.Threading;

namespace dnSpy.Debugger.DotNet.Metadata {
static class ObjectCache {
const int MAX_STRINGBUILDER_CAPACITY = 1024;
static volatile StringBuilder stringBuilder;
public static StringBuilder AllocStringBuilder() => Interlocked.Exchange(ref stringBuilder, null) ?? new StringBuilder();
public static void Free(ref StringBuilder sb) {
if (sb.Capacity <= MAX_STRINGBUILDER_CAPACITY) {
sb.Clear();
stringBuilder = sb;
}
sb = null;
}
public static string FreeAndToString(ref StringBuilder sb) {
var res = sb.ToString();
Free(ref sb);
return res;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@
<Compile Include="Impl\MD\MetadataConstantUtilities.cs" />
<None Include="Impl\ReflectionTests.cs" />
<Compile Include="Impl\WellKnownMemberResolver.cs" />
<Compile Include="ListCache.cs" />
<Compile Include="MemberNotFoundException.cs" />
<Compile Include="ObjectCache.cs" />
<Compile Include="ObjectPools.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReadOnlyCollectionHelpers.cs" />
Expand Down

0 comments on commit 591a4bb

Please sign in to comment.