@@ -48,17 +48,30 @@ public IEnumerable<string> Sort(
4848 /// </summary>
4949 private ushort _lastAvailableId ;
5050
51+ private ushort _lastPreAllocatedId ;
52+
53+ /// <summary>
54+ /// Assembly tables context - contains all tables used for building target assembly.
55+ /// </summary>
56+ private readonly nanoTablesContext _context ;
57+
58+ /// <summary>
59+ /// Last pre-allocated string identifier.
60+ /// </summary>
61+ public ushort LastPreAllocatedId { get => _lastPreAllocatedId ; }
62+
5163 /// <summary>
5264 /// Creates new instance of <see cref="nanoStringTable"/> object.
5365 /// </summary>
5466 public nanoStringTable (
67+ nanoTablesContext context ,
5568 ICustomStringSorter stringSorter = null )
5669 {
5770 GetOrCreateStringId ( string . Empty ) ; // First item in string table always empty string
5871 _stringSorter = stringSorter ?? new EmptyStringSorter ( ) ;
59- }
6072
61-
73+ _context = context ;
74+ }
6275
6376 /// <summary>
6477 /// Gets existing or creates new string reference identifier related to passed string value.
@@ -164,6 +177,46 @@ public void RemoveUnusedItems(HashSet<MetadataToken> items)
164177 // First item in string table always empty string
165178 GetOrCreateStringId ( string . Empty ) ;
166179
180+ // Pre-allocate strings from some tables
181+ _context . AssemblyReferenceTable . AllocateStrings ( ) ;
182+ _context . TypeReferencesTable . AllocateStrings ( ) ;
183+
184+
185+ var memberReferences = _context . AssemblyDefinition . MainModule . GetMemberReferences ( ) ;
186+ List < MemberReference > memberReferencesInUse = new List < MemberReference > ( ) ;
187+
188+ foreach ( var item in memberReferences )
189+ {
190+ var memberRef = _context . TypeReferencesTable . Items . FirstOrDefault ( m => m . FullName . Contains ( item . DeclaringType . FullName ) ) ;
191+ if ( memberRef != null )
192+ {
193+ memberReferencesInUse . Add ( item ) ;
194+ }
195+ }
196+
197+ foreach ( var item in memberReferencesInUse )
198+ {
199+ GetOrCreateStringId ( item . Name ) ;
200+ }
201+
202+ foreach ( var item in _context . TypeDefinitionTable . Items )
203+ {
204+ GetOrCreateStringId ( item . Namespace ) ;
205+ GetOrCreateStringId ( item . Name ) ;
206+
207+ foreach ( var f in item . Fields )
208+ {
209+ GetOrCreateStringId ( f . Name ) ;
210+ }
211+
212+ foreach ( var m in item . Methods )
213+ {
214+ GetOrCreateStringId ( m . Name ) ;
215+ }
216+ }
217+
218+ _lastPreAllocatedId = _idsByStrings . Last ( ) . Value ;
219+
167220 // fill in the dictionary with the used strings
168221 foreach ( var s in usedStrings )
169222 {
0 commit comments