Skip to content

Commit d642556

Browse files
authored
Reduce allocations (#54)
- Updates DimensionLabels' Equatable and Hashable conformances to avoid allocating unnecessary strings. Updates DimensionHistogramLabels and DimensionSummaryLabels to use DimensionLabels internally and removes manually implemented protocol conformances.
1 parent 9d37dcc commit d642556

File tree

1 file changed

+23
-48
lines changed

1 file changed

+23
-48
lines changed

Sources/Prometheus/PrometheusMetrics.swift

Lines changed: 23 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,6 @@ private struct StringCodingKey: CodingKey {
323323
}
324324
}
325325

326-
327-
328326
/// Helper for dimensions
329327
public struct DimensionLabels: MetricLabels {
330328
let dimensions: [(String, String)]
@@ -339,21 +337,24 @@ public struct DimensionLabels: MetricLabels {
339337

340338
public func encode(to encoder: Encoder) throws {
341339
var container = encoder.container(keyedBy: StringCodingKey.self)
342-
try self.dimensions.forEach {
343-
try container.encode($0.1, forKey: .init($0.0))
340+
for (key, value) in self.dimensions {
341+
try container.encode(value, forKey: .init(key))
344342
}
345343
}
346344

347345
public func hash(into hasher: inout Hasher) {
348-
hasher.combine(dimensions.map { "\($0.0)-\($0.1)"})
349-
}
350-
351-
fileprivate var identifiers: String {
352-
return dimensions.map { $0.0 }.joined(separator: "-")
346+
for (key, value) in dimensions {
347+
hasher.combine(key)
348+
hasher.combine(value)
349+
}
353350
}
354-
351+
355352
public static func == (lhs: DimensionLabels, rhs: DimensionLabels) -> Bool {
356-
return lhs.dimensions.map { "\($0.0)-\($0.1)"} == rhs.dimensions.map { "\($0.0)-\($0.1)"}
353+
guard lhs.dimensions.count == rhs.dimensions.count else { return false }
354+
for index in 0..<lhs.dimensions.count {
355+
guard lhs.dimensions[index] == rhs.dimensions[index] else { return false }
356+
}
357+
return false
357358
}
358359
}
359360

@@ -364,79 +365,53 @@ public struct DimensionHistogramLabels: HistogramLabels {
364365
/// Bucket
365366
public var le: String
366367
/// Dimensions
367-
let dimensions: [(String, String)]
368+
let labels: DimensionLabels
368369

369370
/// Empty init
370371
public init() {
371372
self.le = ""
372-
self.dimensions = []
373+
self.labels = DimensionLabels()
373374
}
374375

375376
/// Init with dimensions
376377
public init(_ dimensions: [(String, String)]) {
377378
self.le = ""
378-
self.dimensions = dimensions
379+
self.labels = DimensionLabels(dimensions)
379380
}
380381

381382
public func encode(to encoder: Encoder) throws {
382383
var container = encoder.container(keyedBy: StringCodingKey.self)
383-
try self.dimensions.forEach {
384-
try container.encode($0.1, forKey: .init($0.0))
384+
for (key, value) in self.labels.dimensions {
385+
try container.encode(value, forKey: .init(key))
385386
}
386387
try container.encode(le, forKey: .init("le"))
387388
}
388-
389-
public func hash(into hasher: inout Hasher) {
390-
hasher.combine(dimensions.map { "\($0.0)-\($0.1)"})
391-
hasher.combine(le)
392-
}
393-
394-
fileprivate var identifiers: String {
395-
return dimensions.map { $0.0 }.joined(separator: "-")
396-
}
397-
398-
public static func == (lhs: DimensionHistogramLabels, rhs: DimensionHistogramLabels) -> Bool {
399-
return lhs.dimensions.map { "\($0.0)-\($0.1)"} == rhs.dimensions.map { "\($0.0)-\($0.1)"} && rhs.le == lhs.le
400-
}
401389
}
402390

403391
/// Helper for dimensions
404392
public struct DimensionSummaryLabels: SummaryLabels {
405393
/// Quantile
406394
public var quantile: String
407395
/// Dimensions
408-
let dimensions: [(String, String)]
409-
396+
let labels: DimensionLabels
397+
410398
/// Empty init
411399
public init() {
412400
self.quantile = ""
413-
self.dimensions = []
401+
self.labels = DimensionLabels()
414402
}
415403

416404
/// Init with dimensions
417405
public init(_ dimensions: [(String, String)]) {
418406
self.quantile = ""
419-
self.dimensions = dimensions
407+
self.labels = DimensionLabels(dimensions)
420408
}
421409

422410
public func encode(to encoder: Encoder) throws {
423411
var container = encoder.container(keyedBy: StringCodingKey.self)
424-
try self.dimensions.forEach {
425-
try container.encode($0.1, forKey: .init($0.0))
412+
for (key, value) in self.labels.dimensions {
413+
try container.encode(value, forKey: .init(key))
426414
}
427415
try container.encode(quantile, forKey: .init("quantile"))
428416
}
429-
430-
public func hash(into hasher: inout Hasher) {
431-
hasher.combine(dimensions.map { "\($0.0)-\($0.1)"})
432-
hasher.combine(quantile)
433-
}
434-
435-
fileprivate var identifiers: String {
436-
return dimensions.map { $0.0 }.joined(separator: "-")
437-
}
438-
439-
public static func == (lhs: DimensionSummaryLabels, rhs: DimensionSummaryLabels) -> Bool {
440-
return lhs.dimensions.map { "\($0.0)-\($0.1)"} == rhs.dimensions.map { "\($0.0)-\($0.1)"} && rhs.quantile == lhs.quantile
441-
}
442417
}

0 commit comments

Comments
 (0)