Skip to content

fix: Reduce allocations for classic histogram buckets - #2081

Merged
zeitlinger merged 9 commits into
mainfrom
reduce-allocations
May 6, 2026
Merged

fix: Reduce allocations for classic histogram buckets #2081
zeitlinger merged 9 commits into
mainfrom
reduce-allocations

Conversation

@jaydeluca

@jaydelucajaydeluca commented Apr 30, 2026

Copy link
Copy Markdown
Member

Related to #2075

This reduces allocation and GC pressure in histogram text formatting by eliminating unnecessary intermediate allocations.

@jaydelucajaydeluca changed the title Reduce allocations for classic histogram buckets feat: Reduce allocations for classic histogram buckets Apr 30, 2026
@jaydelucajaydeluca changed the title feat: Reduce allocations for classic histogram buckets fix: Reduce allocations for classic histogram buckets Apr 30, 2026
Signed-off-by: Jay DeLuca <jaydeluca4@gmail.com>
Signed-off-by: Jay DeLuca <jaydeluca4@gmail.com>
Signed-off-by: Jay DeLuca <jaydeluca4@gmail.com>
Signed-off-by: Jay DeLuca <jaydeluca4@gmail.com>
@jaydeluca
jaydelucaforce-pushed the reduce-allocations branch from 494d653 to 5fe88b1CompareMay 1, 2026 20:37
Signed-off-by: Ubuntu <jaydeluca4@gmail.com>
Signed-off-by: Jay DeLuca <jaydeluca4@gmail.com>
Signed-off-by: Jay DeLuca <jaydeluca4@gmail.com>

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR targets allocation reduction in the text exposition layer by removing repeated string building in hot formatting paths and replacing Long.toString(...) calls with direct writer-based long formatting. It also adds benchmark coverage around histogram text rendering so the impact can be measured in the benchmarking module.

Changes:

  • Added a manual writeLong(...) helper in TextFormatUtil and reused it for counts and timestamps.
  • Cached base/suffixed metric names in the Prometheus, OpenMetrics, and OpenMetrics 2 text writers to avoid repeated concatenation in loops.
  • Added a histogram text-format JMH benchmark and updated benchmark tasks to run with the GC profiler.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.

Show a summary per file
FileDescription
prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/TextFormatUtil.javaReplaces Long.toString(...) with manual long/timestamp writing helpers.
prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/PrometheusTextFormatWriter.javaCaches metric names and suffixed variants to reduce per-datapoint allocations in Prometheus text output.
prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/OpenMetricsTextFormatWriter.javaApplies the same cached-name optimization to OpenMetrics 1 text output.
prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/OpenMetrics2TextFormatWriter.javaReduces classic histogram bucket-name concatenation in OpenMetrics 2 text output.
mise.tomlUpdates benchmark tasks to pass JMH GC-profiler arguments.
benchmarks/src/main/java/io/prometheus/metrics/benchmarks/HistogramTextFormatBenchmark.javaAdds a dedicated JMH benchmark for classic histogram text rendering.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment threadmise.toml
Signed-off-by: Jay DeLuca <jaydeluca4@gmail.com>
Signed-off-by: Jay DeLuca <jaydeluca4@gmail.com>
@jaydeluca
jaydeluca marked this pull request as ready for review May 5, 2026 14:31
@zeitlinger
zeitlinger merged commit edd160a into mainMay 6, 2026
14 checks passed
@zeitlinger
zeitlinger deleted the reduce-allocations branch May 6, 2026 09:31
@github-actionsgithub-actionsBot mentioned this pull request May 6, 2026
zeitlinger added a commit that referenced this pull request Jun 3, 2026
🤖 I have created a release *beep* *boop*
---
##
[1.7.0](v1.6.1...v1.7.0)
(2026-06-03)
### Features
* Add StableApi marker and API diff check
([#2168](#2168))
([768fd3a](768fd3a))
* add typed metric family descriptors
([#2114](#2114))
([9c3b097](9c3b097))
* track api-diff baseline via Renovate and store diffs in docs/apidiffs
([#2174](#2174))
([3adb890](3adb890))
### Bug Fixes
* **deps:** update dependency com.github.ben-manes.caffeine:caffeine to
v3.2.4 ([#2088](#2088))
([144eb61](144eb61))
* **deps:** update dependency io.dropwizard.metrics:metrics-core to
v4.2.39 ([#2139](#2139))
([5817d13](5817d13))
* **deps:** update dependency io.dropwizard.metrics5:metrics-core to
v5.0.7 ([#2140](#2140))
([261c451](261c451))
* **deps:** update dependency
io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom-alpha
to v2.28.0-alpha
([#2126](#2126))
([b62b5d0](b62b5d0))
* **deps:** update dependency
io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom-alpha
to v2.28.0-alpha
([#2127](#2127))
([e11ce3d](e11ce3d))
* **deps:** update dependency
io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom-alpha
to v2.28.1-alpha
([#2132](#2132))
([b09be38](b09be38))
* **deps:** update dependency
io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom-alpha
to v2.28.1-alpha
([#2133](#2133))
([a241c16](a241c16))
* **deps:** update dependency org.apache.tomcat.embed:tomcat-embed-core
to v11.0.22
([#2099](#2099))
([22125c5](22125c5))
* **deps:** update jetty monorepo to v12.1.10
([#2169](#2169))
([ddd3991](ddd3991))
* **deps:** update jetty monorepo to v12.1.9
([#2102](#2102))
([04bee70](04bee70))
* **deps:** update protobuf
([#2129](#2129))
([320538a](320538a))
* Reduce allocations for classic histogram buckets
([#2081](#2081))
([edd160a](edd160a))
* restore legacy suffix compatibility
([#2100](#2100))
([b2ae70f](b2ae70f))
* restore reserved suffix stripping in
`PrometheusNaming.sanitizeMetricName()`
([#2124](#2124))
([2d0f508](2d0f508))
### Performance Improvements
* Refactored sorting to use optimized sort algorithms
([#2161](#2161))
([25b94fc](25b94fc))
### Documentation
* clarify downstream adapter validation requirements
([#2101](#2101))
([ef8c75c](ef8c75c))
* Document OM2
([#2059](#2059))
([45d753c](45d753c))
* document PushGateway shading workaround
([#2106](#2106))
([8ca0eb8](8ca0eb8))
---
> [!IMPORTANT]
> Close and reopen this PR to trigger CI checks.
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
jaydeluca pushed a commit that referenced this pull request Jun 4, 2026
🤖 I have created a release *beep* *boop*
---
##
[1.7.0](v1.6.1...v1.7.0)
(2026-06-03)
### Features
* Add StableApi marker and API diff check
([#2168](#2168))
([768fd3a](768fd3a))
* add typed metric family descriptors
([#2114](#2114))
([9c3b097](9c3b097))
* track api-diff baseline via Renovate and store diffs in docs/apidiffs
([#2174](#2174))
([3adb890](3adb890))
### Bug Fixes
* **deps:** update dependency com.github.ben-manes.caffeine:caffeine to
v3.2.4 ([#2088](#2088))
([144eb61](144eb61))
* **deps:** update dependency io.dropwizard.metrics:metrics-core to
v4.2.39 ([#2139](#2139))
([5817d13](5817d13))
* **deps:** update dependency io.dropwizard.metrics5:metrics-core to
v5.0.7 ([#2140](#2140))
([261c451](261c451))
* **deps:** update dependency
io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom-alpha
to v2.28.0-alpha
([#2126](#2126))
([b62b5d0](b62b5d0))
* **deps:** update dependency
io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom-alpha
to v2.28.0-alpha
([#2127](#2127))
([e11ce3d](e11ce3d))
* **deps:** update dependency
io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom-alpha
to v2.28.1-alpha
([#2132](#2132))
([b09be38](b09be38))
* **deps:** update dependency
io.opentelemetry.instrumentation:opentelemetry-instrumentation-bom-alpha
to v2.28.1-alpha
([#2133](#2133))
([a241c16](a241c16))
* **deps:** update dependency org.apache.tomcat.embed:tomcat-embed-core
to v11.0.22
([#2099](#2099))
([22125c5](22125c5))
* **deps:** update jetty monorepo to v12.1.10
([#2169](#2169))
([ddd3991](ddd3991))
* **deps:** update jetty monorepo to v12.1.9
([#2102](#2102))
([04bee70](04bee70))
* **deps:** update protobuf
([#2129](#2129))
([320538a](320538a))
* Reduce allocations for classic histogram buckets
([#2081](#2081))
([edd160a](edd160a))
* restore legacy suffix compatibility
([#2100](#2100))
([b2ae70f](b2ae70f))
* restore reserved suffix stripping in
`PrometheusNaming.sanitizeMetricName()`
([#2124](#2124))
([2d0f508](2d0f508))
### Performance Improvements
* Refactored sorting to use optimized sort algorithms
([#2161](#2161))
([25b94fc](25b94fc))
### Documentation
* clarify downstream adapter validation requirements
([#2101](#2101))
([ef8c75c](ef8c75c))
* Document OM2
([#2059](#2059))
([45d753c](45d753c))
* document PushGateway shading workaround
([#2106](#2106))
([8ca0eb8](8ca0eb8))
---
> [!IMPORTANT]
> Close and reopen this PR to trigger CI checks.
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Gregor Zeitlinger <gregor.zeitlinger@grafana.com>
Signed-off-by: Jay DeLuca <jaydeluca4@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@jaydeluca@zeitlinger