@@ -301,6 +301,8 @@ func (c *Collector) collectWorkerRaw() {
301301 )
302302
303303 buf := make ([]byte , 1 )
304+ // Reuse string cache across collection cycles to reduce allocations
305+ stringCache := make (map [* uint16 ]string , 64 )
304306
305307 for data := range c .collectCh {
306308 err = (func () error {
@@ -334,7 +336,10 @@ func (c *Collector) collectWorkerRaw() {
334336 elemValue := reflect .ValueOf (reflect .New (elemType ).Interface ()).Elem ()
335337
336338 indexMap := map [string ]int {}
337- stringMap := map [* uint16 ]string {}
339+ // Clear and reuse string cache instead of creating new one each time
340+ for k := range stringCache {
341+ delete (stringCache , k )
342+ }
338343
339344 for _ , counter := range c .counters {
340345 for _ , instance := range counter .Instances {
@@ -372,11 +377,6 @@ func (c *Collector) collectWorkerRaw() {
372377
373378 items = unsafe .Slice ((* RawCounterItem )(unsafe .Pointer (& buf [0 ])), itemCount )
374379
375- var (
376- instanceName string
377- ok bool
378- )
379-
380380 for _ , item := range items {
381381 if item .RawValue .CStatus != CstatusValidData && item .RawValue .CStatus != CstatusNewData {
382382 c .logger .Debug ("skipping counter item with invalid data status" ,
@@ -388,9 +388,14 @@ func (c *Collector) collectWorkerRaw() {
388388 continue
389389 }
390390
391- if instanceName , ok = stringMap [item .SzName ]; ! ok {
391+ var (
392+ instanceName string
393+ ok bool
394+ )
395+
396+ if instanceName , ok = stringCache [item .SzName ]; ! ok {
392397 instanceName = windows .UTF16PtrToString (item .SzName )
393- stringMap [item .SzName ] = instanceName
398+ stringCache [item .SzName ] = instanceName
394399 }
395400
396401 if strings .HasSuffix (instanceName , InstanceTotal ) && ! c .totalCounterRequested {
@@ -401,12 +406,8 @@ func (c *Collector) collectWorkerRaw() {
401406 instanceName = InstanceEmpty
402407 }
403408
404- var (
405- index int
406- ok bool
407- )
408-
409- if index , ok = indexMap [instanceName ]; ! ok {
409+ index , indexExists := indexMap [instanceName ]
410+ if ! indexExists {
410411 index = dv .Len ()
411412 indexMap [instanceName ] = index
412413
@@ -475,6 +476,8 @@ func (c *Collector) collectWorkerFormatted() {
475476 )
476477
477478 buf := make ([]byte , 1 )
479+ // Reuse string cache across collection cycles to reduce allocations
480+ stringCache := make (map [* uint16 ]string , 64 )
478481
479482 for data := range c .collectCh {
480483 err = (func () error {
@@ -508,7 +511,10 @@ func (c *Collector) collectWorkerFormatted() {
508511 elemValue := reflect .ValueOf (reflect .New (elemType ).Interface ()).Elem ()
509512
510513 indexMap := map [string ]int {}
511- stringMap := map [* uint16 ]string {}
514+ // Clear and reuse string cache instead of creating new one each time
515+ for k := range stringCache {
516+ delete (stringCache , k )
517+ }
512518
513519 for _ , counter := range c .counters {
514520 for _ , instance := range counter .Instances {
@@ -556,9 +562,9 @@ func (c *Collector) collectWorkerFormatted() {
556562 continue
557563 }
558564
559- if instanceName , ok = stringMap [item .SzName ]; ! ok {
565+ if instanceName , ok = stringCache [item .SzName ]; ! ok {
560566 instanceName = windows .UTF16PtrToString (item .SzName )
561- stringMap [item .SzName ] = instanceName
567+ stringCache [item .SzName ] = instanceName
562568 }
563569
564570 if strings .HasSuffix (instanceName , InstanceTotal ) && ! c .totalCounterRequested {
@@ -569,12 +575,8 @@ func (c *Collector) collectWorkerFormatted() {
569575 instanceName = InstanceEmpty
570576 }
571577
572- var (
573- index int
574- ok bool
575- )
576-
577- if index , ok = indexMap [instanceName ]; ! ok {
578+ index , indexExists := indexMap [instanceName ]
579+ if ! indexExists {
578580 index = dv .Len ()
579581 indexMap [instanceName ] = index
580582
0 commit comments