2020using System ;
2121using System . Diagnostics ;
2222using System . Globalization ;
23- using System . Security ;
2423using System . Text ;
2524using System . Reflection ;
26- using System . Collections ;
2725using System . Windows ;
2826
2927using Microsoft . Win32 ;
3028using MS . Win32 ;
3129using MS . Internal . WindowsBase ;
32- using System . Collections . Generic ;
3330
3431namespace MS . Internal
3532{
@@ -263,18 +260,22 @@ public string Trace(TraceEventType type, int eventId, string message, string[] l
263260 {
264261 // Don't bother building the string if this trace is going to be ignored.
265262
266- if ( _traceSource == null || ! _traceSource . Switch . ShouldTrace ( type ) )
263+ if ( _traceSource == null || ! _traceSource . Switch . ShouldTrace ( type ) )
267264 return null ;
268265
269266 // Compose the trace string.
270267
271268 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.
274270 int formatIndex = 0 ;
275271
276272 if ( ! parameters . IsEmpty && labels ? . Length > 0 )
277273 {
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+
278279 int i = 1 , j = 0 ;
279280 for ( ; i < labels . Length && j < parameters . Length ; i ++ , j ++ )
280281 {
@@ -285,14 +286,14 @@ public string Trace(TraceEventType type, int eventId, string message, string[] l
285286
286287 // Add the label to the combined list.
287288
288- combinedList . Add ( labels [ i ] ) ;
289+ combinedArgs [ j * 2 ] = labels [ i ] ;
289290
290291 // If the parameter is null, convert to "<null>"; otherwise,
291292 // when a string.format is ultimately called it produces bad results.
292293
293294 if ( parameters [ j ] == null )
294295 {
295- combinedList . Add ( "<null>" ) ;
296+ combinedArgs [ j * 2 + 1 ] = "<null>" ;
296297 }
297298
298299 // 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
309310
310311 // Add the parameter to the combined list.
311312
312- combinedList . Add ( parameters [ j ] ) ;
313+ combinedArgs [ j * 2 + 1 ] = parameters [ j ] ;
313314 }
314315 else // Add the parameter to the combined list.
315316 {
316- combinedList . Add ( parameters [ j ] ) ;
317+ combinedArgs [ j * 2 + 1 ] = parameters [ j ] ;
317318 }
318319 }
319320
@@ -329,12 +330,7 @@ public string Trace(TraceEventType type, int eventId, string message, string[] l
329330 // Send the trace
330331
331332 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 ) ;
338334
339335 // When in the debugger, always flush the output, to guarantee that the
340336 // traces and other info (e.g. exceptions) get interleaved correctly.
0 commit comments