20
20
using System ;
21
21
using System . Diagnostics ;
22
22
using System . Globalization ;
23
- using System . Security ;
24
23
using System . Text ;
25
24
using System . Reflection ;
26
- using System . Collections ;
27
25
using System . Windows ;
28
26
29
27
using Microsoft . Win32 ;
30
28
using MS . Win32 ;
31
29
using MS . Internal . WindowsBase ;
32
- using System . Collections . Generic ;
33
30
34
31
namespace MS . Internal
35
32
{
@@ -263,18 +260,22 @@ public string Trace(TraceEventType type, int eventId, string message, string[] l
263
260
{
264
261
// Don't bother building the string if this trace is going to be ignored.
265
262
266
- if ( _traceSource == null || ! _traceSource . Switch . ShouldTrace ( type ) )
263
+ if ( _traceSource == null || ! _traceSource . Switch . ShouldTrace ( type ) )
267
264
return null ;
268
265
269
266
// Compose the trace string.
270
267
271
268
AvTraceBuilder traceBuilder = new AvTraceBuilder ( AntiFormat ( message ) ) ; // Holds the format string
272
- List < object > combinedList = new ( parameters . Length * 2 ) ; // Holds the combined labels & parameters arrays.
273
-
269
+ object [ ] combinedArgs = null ; // Holds the combined labels & parameters arrays.
274
270
int formatIndex = 0 ;
275
271
276
272
if ( ! parameters . IsEmpty && labels ? . Length > 0 )
277
273
{
274
+ // Create array of pre-computed size
275
+ int combinedArgsLength = Math . Min ( labels . Length - 1 , parameters . Length ) * 2 ;
276
+ if ( combinedArgsLength > 0 )
277
+ combinedArgs = new object [ combinedArgsLength ] ;
278
+
278
279
int i = 1 , j = 0 ;
279
280
for ( ; i < labels . Length && j < parameters . Length ; i ++ , j ++ )
280
281
{
@@ -285,14 +286,14 @@ public string Trace(TraceEventType type, int eventId, string message, string[] l
285
286
286
287
// Add the label to the combined list.
287
288
288
- combinedList . Add ( labels [ i ] ) ;
289
+ combinedArgs [ j * 2 ] = labels [ i ] ;
289
290
290
291
// If the parameter is null, convert to "<null>"; otherwise,
291
292
// when a string.format is ultimately called it produces bad results.
292
293
293
294
if ( parameters [ j ] == null )
294
295
{
295
- combinedList . Add ( "<null>" ) ;
296
+ combinedArgs [ j * 2 + 1 ] = "<null>" ;
296
297
}
297
298
298
299
// Otherwise, if this is an interesting object, add the hash code and type to
@@ -309,11 +310,11 @@ public string Trace(TraceEventType type, int eventId, string message, string[] l
309
310
310
311
// Add the parameter to the combined list.
311
312
312
- combinedList . Add ( parameters [ j ] ) ;
313
+ combinedArgs [ j * 2 + 1 ] = parameters [ j ] ;
313
314
}
314
315
else // Add the parameter to the combined list.
315
316
{
316
- combinedList . Add ( parameters [ j ] ) ;
317
+ combinedArgs [ j * 2 + 1 ] = parameters [ j ] ;
317
318
}
318
319
}
319
320
@@ -329,12 +330,7 @@ public string Trace(TraceEventType type, int eventId, string message, string[] l
329
330
// Send the trace
330
331
331
332
string traceMessage = traceBuilder . ToString ( ) ;
332
-
333
- _traceSource . TraceEvent (
334
- type ,
335
- eventId ,
336
- traceMessage ,
337
- combinedList . ToArray ( ) ) ; //Cannot avoid the alloc here, no ROS<object> overload
333
+ _traceSource . TraceEvent ( type , eventId , traceMessage , combinedArgs ) ;
338
334
339
335
// When in the debugger, always flush the output, to guarantee that the
340
336
// traces and other info (e.g. exceptions) get interleaved correctly.
0 commit comments