Skip to content

Commit 0e223e4

Browse files
authored
Improvements in minimize stage when cleaning the string table (#52)
1 parent 5aa5b62 commit 0e223e4

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

source/MetadataProcessor.Core/Tables/nanoStringTable.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public IEnumerable<string> Sort(
3535
/// <summary>
3636
/// Maps for each unique string and related identifier (offset in strings table).
3737
/// </summary>
38-
private readonly Dictionary<string, ushort> _idsByStrings =
38+
private Dictionary<string, ushort> _idsByStrings =
3939
new Dictionary<string, ushort>(StringComparer.Ordinal);
4040

4141
/// <summary>
@@ -138,6 +138,7 @@ internal void MergeValues(
138138
public void RemoveUnusedItems(HashSet<MetadataToken> items)
139139
{
140140
var setAll = new HashSet<MetadataToken>();
141+
List<string> usedStrings = new List<string>();
141142

142143
// build a collection of the existing tokens
143144
foreach (var a in _idsByStrings)
@@ -148,14 +149,25 @@ public void RemoveUnusedItems(HashSet<MetadataToken> items)
148149
// remove the ones that are used
149150
foreach (var t in items)
150151
{
151-
setAll.Remove(t);
152+
if(setAll.Remove(t))
153+
{
154+
usedStrings.Add(TryGetString((ushort)t.ToUInt32()));
155+
}
152156
}
153157

154-
// the ones left are the ones not used, OK to remove them
155-
foreach (var t in setAll)
158+
// reset dictionary
159+
_idsByStrings = new Dictionary<string, ushort>(StringComparer.Ordinal);
160+
161+
// and last ID too
162+
_lastAvailableId = 0;
163+
164+
// First item in string table always empty string
165+
GetOrCreateStringId(string.Empty);
166+
167+
// fill in the dictionary with the used strings
168+
foreach (var s in usedStrings)
156169
{
157-
var itemToRemove = TryGetString((ushort)t.ToUInt32());
158-
_idsByStrings.Remove(itemToRemove);
170+
GetOrCreateStringId(s);
159171
}
160172
}
161173

source/MetadataProcessor.Core/Tables/nanoTablesContext.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,10 +420,5 @@ internal void ResetSignaturesTable()
420420
{
421421
SignaturesTable = new nanoSignaturesTable(this);
422422
}
423-
424-
internal void ResetStringsTable()
425-
{
426-
StringTable = new nanoStringTable();
427-
}
428423
}
429424
}

source/MetadataProcessor.Core/nanoAssemblyBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void Write(
9292
/// Minimizes the assembly, removing unwanted and unused elements.
9393
/// </summary>
9494
/// <remarks>
95-
/// In order to minize an assembly it has to have been previously compiled.
95+
/// In order to minimize an assembly it has to have been previously compiled.
9696
/// </remarks>
9797
public void Minimize()
9898
{
@@ -156,7 +156,6 @@ public void Minimize()
156156
}
157157

158158
// need to reset several tables so they are recreated only with the used items
159-
_tablesContext.ResetStringsTable();
160159
_tablesContext.AssemblyReferenceTable.RemoveUnusedItems(set);
161160
_tablesContext.TypeReferencesTable.RemoveUnusedItems(set);
162161
_tablesContext.FieldsTable.RemoveUnusedItems(set);
@@ -168,7 +167,8 @@ public void Minimize()
168167
_tablesContext.AttributesTable.RemoveUnusedItems(set);
169168
_tablesContext.ResetByteCodeTable();
170169
_tablesContext.ResetSignaturesTable();
171-
170+
_tablesContext.StringTable.RemoveUnusedItems(set);
171+
172172
// renormalise type definitions look-up tables
173173
// by removing items that are not used
174174

0 commit comments

Comments
 (0)