@@ -507,49 +507,51 @@ private static int WriteExemplar(byte[] buffer, int writePosition, in Exemplar e
507
507
508
508
private static int WriteHistogramBuckets ( byte [ ] buffer , int writePosition , HistogramBuckets buckets )
509
509
{
510
- // Collect all counts (assuming GetHistogramBuckets() returns in order)
511
- var bucketCounts = new List < ulong > ( ) ;
510
+ const int Fixed64Size = 8 ;
512
511
513
- foreach ( var x in buckets )
514
- {
515
- bucketCounts . Add ( ( ulong ) x . BucketCount ) ;
516
- }
512
+ int length = buckets . BucketCounts . Length ;
517
513
518
- // Write packed repeated fixed64 bucket_counts
519
- var fieldNumber = ProtobufOtlpMetricFieldNumberConstants . HistogramDataPoint_Bucket_Counts ;
520
- var tag2 = ( fieldNumber << 3 ) | 2 ; // wire type 2 for length-delimited
521
- writePosition = ProtobufSerializer . WriteVarInt32 ( buffer , writePosition , ( uint ) tag2 ) ;
514
+ writePosition = ProtobufSerializer . WriteTagAndLength (
515
+ buffer ,
516
+ writePosition ,
517
+ length * Fixed64Size ,
518
+ ProtobufOtlpMetricFieldNumberConstants . HistogramDataPoint_Bucket_Counts ,
519
+ ProtobufWireType . LEN ) ;
522
520
523
- int length = bucketCounts . Count * 8 ;
524
- writePosition = ProtobufSerializer . WriteVarInt32 ( buffer , writePosition , ( uint ) length ) ;
525
-
526
- foreach ( var bucketCount in bucketCounts )
521
+ for ( int i = 0 ; i < length ; i ++ )
527
522
{
528
- writePosition = ProtobufSerializer . WriteFixed64LittleEndianFormat ( buffer , writePosition , bucketCount ) ;
523
+ writePosition = ProtobufSerializer . WriteFixed64LittleEndianFormat (
524
+ buffer ,
525
+ writePosition ,
526
+ ( ulong ) buckets . BucketCounts [ i ] . SnapshotValue ) ;
529
527
}
530
528
531
- var explicitBounds = new List < double > ( ) ;
529
+ length = 0 ;
532
530
533
- foreach ( var x in buckets )
531
+ for ( int i = 0 ; i < buckets . ExplicitBounds ! . Length ; i ++ )
534
532
{
535
- if ( x . ExplicitBound != double . PositiveInfinity )
533
+ if ( buckets . ExplicitBounds [ i ] != double . PositiveInfinity )
536
534
{
537
- explicitBounds . Add ( x . ExplicitBound ) ;
535
+ length ++ ;
538
536
}
539
537
}
540
538
541
- if ( explicitBounds . Count > 0 )
539
+ if ( length > 0 )
542
540
{
543
- var boundsFieldNumber = ProtobufOtlpMetricFieldNumberConstants . HistogramDataPoint_Explicit_Bounds ;
544
- var boundsTag = ( boundsFieldNumber << 3 ) | 2 ; // wire type 2
545
- writePosition = ProtobufSerializer . WriteVarInt32 ( buffer , writePosition , ( uint ) boundsTag ) ;
546
-
547
- int boundsLength = explicitBounds . Count * 8 ;
548
- writePosition = ProtobufSerializer . WriteVarInt32 ( buffer , writePosition , ( uint ) boundsLength ) ;
549
-
550
- foreach ( var bound in explicitBounds )
541
+ writePosition = ProtobufSerializer . WriteTagAndLength (
542
+ buffer ,
543
+ writePosition ,
544
+ length * Fixed64Size ,
545
+ ProtobufOtlpMetricFieldNumberConstants . HistogramDataPoint_Explicit_Bounds ,
546
+ ProtobufWireType . LEN ) ;
547
+
548
+ for ( int i = 0 ; i < buckets . ExplicitBounds ! . Length ; i ++ )
551
549
{
552
- writePosition = ProtobufSerializer . WriteDouble ( buffer , writePosition , bound ) ;
550
+ var value = buckets . ExplicitBounds [ i ] ;
551
+ if ( value != double . PositiveInfinity )
552
+ {
553
+ writePosition = ProtobufSerializer . WriteDouble ( buffer , writePosition , value ) ;
554
+ }
553
555
}
554
556
}
555
557
0 commit comments