perf(regexp): hoist constant MustCompile calls on hot paths to package level #57299
AI review by Codex (OpenAI) - workflow run
patch is correct. The regexp patterns and compilation modes are preserved, and the shared compiled expressions are used safely. No actionable regressions found. Validation was limited to static review; tests were not run.
Benchmarks
Benchmark execution time: 2026-10-02 08:44:46
Comparing candidate commit 67872e8c727de1475760d32f8e6b7bae642ead19 in PR branch louis-cqrl/regexp-hoist-prod with baseline commit 5421b65ebfa64e03993af8ded783766380e36e6e in branch main.
Found 0 performance improvements and 0 performance regressions! Performance is the same for 3 metrics, 0 unstable metrics.
<details>
<summary><h3>Explanation</h3></summary>
This is an A/B test comparing a candidate commit's performance against that of a baseline commit. Performance changes are noted in the tables below as:
🟩 = significantly better candidate vs. baseline
🟥 = significantly worse candidate vs. baseline
We compute a confidence interval (CI) over the relative difference of means between metrics from the candidate and baseline commits, considering the baseline as the reference.
If the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD), the change is considered significant.
Feel free to reach out to #apm-benchmarking-platform on Slack if you have any questions.
<details>
<summary><h4>More details about the CI and significant changes</h4></summary>
You can imagine this CI as a range of values that is likely to contain the true difference of means between the candidate and baseline commits.
CIs of the difference of means are often centered around 0%, because often changes are not that big:
---------------------------------(------|---^--------)-------------------------------->
-0.6% 0% 0.3% +1.2%
| | |
lower bound of the CI --' | |
sample mean (center of the CI) -------------' |
upper bound of the CI ----------------------'
As described above, a change is considered significant if the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD).
For instance, for an execution time metric, this confidence interval indicates a significantly worse performance:
----------------------------------------|---------|---(---------^---------)---------->
0% 1% 1.3% 2.2% 3.1%
| | | |
significant impact threshold --------------' | | |
lower bound of CI --------------' | |
sample mean (center of the CI) --------------------------' |
upper bound of CI ----------------------------------'
</details>
</details>
🎯 Code Coverage (<a target='_blank' href='https://app.datadoghq.com/ci/code-coverage/github.com%2Fdatadog%2Fdatadog-agent/pull-requests/57299'>details</a>)<br>• <strong><span title="Percentage of modified executable lines covered by tests in this PR">Patch Coverage</span></strong>: 62.50%<br>• <strong><span title="Percentage of all executable lines covered by tests in the entire codebase">Overall Coverage</span></strong>: 56.99% <span title="Change in overall coverage compared to the base branch">(-1.54%)</span><br>
<sub>
This comment will be updated automatically if new data arrives. <br />
🔗 Commit SHA: 420c8d7 | <a href="https://docs.datadoghq.com/integrations/guide/source-code-integration/#pr-comments">Docs</a> | Give us <a href="https://dd.enterprise.slack.com/archives/C07E3U2QRMM">feedback</a>!
</sub>
Files inventory check summary
File checks results against ancestor 3061e3e9:
Results for datadog-agent_7.85.0~devel.git.715.420c8d7.pipeline.142657881-1_amd64.deb:
No change detected
Results for datadog-iot-agent_7.85.0~devel.git.715.420c8d7.pipeline.142657881-1_amd64.deb:
No change detected
Static quality checks
✅ Please find below the results from static quality gates
Comparison made with ancestor 3061e3e9ed1bfeb4df9819ca317d793178aae42f
📊 Static Quality Gates Dashboard
🔗 SQG Job
SOME SIZE DELTAS ARE N/A (ANCESTOR METRICS NOT YET AVAILABLE). RETRY JOB
<details open>
<summary>Successful checks</summary>
Info
||Quality gate|Change|Size (prev → curr → max)|
|--|--|--|--|
|✅|agent_deb_amd64|N/A|N/A → 782.913 → 785.980|
|✅|agent_deb_amd64_fips|N/A|N/A → 717.283 → 719.210|
|✅|agent_heroku_amd64|N/A|N/A → 317.613 → 323.180|
|✅|agent_msi|N/A|N/A → 652.540 → 668.830|
|✅|agent_rpm_amd64|N/A|N/A → 782.896 → 785.950|
|✅|agent_rpm_amd64_fips|N/A|N/A → 717.267 → 719.210|
|✅|agent_rpm_arm64|N/A|N/A → 753.791 → 757.920|
|✅|agent_rpm_arm64_fips|N/A|N/A → 693.562 → 696.980|
|✅|agent_suse_amd64|N/A|N/A → 782.896 → 785.950|
|✅|agent_suse_amd64_fips|N/A|N/A → 717.267 → 719.210|
|✅|agent_suse_arm64|N/A|N/A → 753.791 → 757.920|
|✅|agent_suse_arm64_fips|N/A|N/A → 693.562 → 696.980|
|✅|docker_agent_amd64|N/A|N/A → 839.198 → 843.560|
|✅|docker_agent_arm64|N/A|N/A → 834.723 → 841.880|
|✅|docker_agent_jmx_amd64|N/A|N/A → 1030.068 → 1034.470|
|✅|docker_agent_jmx_arm64|N/A|N/A → 1014.373 → 1021.570|
|✅|docker_cws_instrumentation_amd64|N/A|N/A → 7.439 → 8.400|
|✅|docker_cws_instrumentation_arm64|N/A|N/A → 6.877 → 7.110|
|✅|docker_dogstatsd_amd64|N/A|N/A → 39.414 → 40.440|
|✅|docker_dogstatsd_arm64|N/A|N/A → 37.503 → 38.580|
|✅|docker_host_profiler_amd64|N/A|N/A → 308.055 → 319.490|
|✅|docker_host_profiler_arm64|N/A|N/A → 319.275 → 330.690|
|✅|dogstatsd_deb_amd64|N/A|N/A → 30.157 → 31.220|
|✅|dogstatsd_deb_arm64|N/A|N/A → 28.165 → 29.590|
|✅|dogstatsd_rpm_amd64|N/A|N/A → 30.157 → 31.220|
|✅|dogstatsd_suse_amd64|N/A|N/A → 30.157 → 31.220|
|✅|iot_agent_deb_amd64|N/A|N/A → 46.599 → 47.550|
|✅|iot_agent_deb_arm64|N/A|N/A → 43.220 → 44.220|
|✅|iot_agent_deb_armhf|N/A|N/A → 44.045 → 45.020|
|✅|iot_agent_rpm_amd64|N/A|N/A → 46.600 → 47.550|
|✅|iot_agent_suse_amd64|N/A|N/A → 46.599 → 47.550|
|✅|docker_cluster_agent_amd64|N/A|N/A → 213.655 → 213.700|
|✅|docker_cluster_agent_arm64|N/A|N/A → 226.600 → 226.660|
</details>
Regression Detector
Regression Detector Results
Metrics dashboard
Target profiles
Job ID: 66cfd8cd-a786-40b7-a7b4-5ea2e8c8b26f
Baseline: 3061e3e9ed1bfeb4df9819ca317d793178aae42f
Comparison: 67872e8c727de1475760d32f8e6b7bae642ead19
[Diff](3061e3e9ed1bfeb4df9819ca317d793178aae42f...67872e8c727de1475760d32f8e6b7bae642ead19)
Optimization Goals: ✅ No significant changes detected
<details>
<summary><h2>
Fine details of change detection per experiment
</h2></summary>
| perf | experiment | goal | Δ mean % | Δ mean % CI | trials | links |
|------|---------------------------------------------|--------------------|----------|----------------|--------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ➖ | quality_gate_metrics_logs | memory utilization | +1.02 | [+0.79, +1.25] | 1 | Logs bounds checks dashboard |
| ➖ | dsd_uds_client_drop_detector_cpu | % cpu utilization | +0.24 | [-0.28, +0.76] | 1 | Logs |
| ➖ | quality_gate_security_mean_fs_load | memory utilization | +0.10 | [+0.06, +0.14] | 1 | Logs bounds checks dashboard |
| ➖ | quality_gate_security_no_fs_load | memory utilization | +0.06 | [-0.02, +0.13] | 1 | Logs bounds checks dashboard |
| ➖ | quality_gate_idle_all_features | memory utilization | +0.04 | [-0.02, +0.09] | 1 | Logs bounds checks dashboard |
| ➖ | quality_gate_security_idle | memory utilization | -0.05 | [-0.09, -0.01] | 1 | Logs bounds checks dashboard |
| ➖ | quality_gate_idle | memory utilization | -0.08 | [-0.12, -0.04] | 1 | Logs bounds checks dashboard |
| ➖ | dsd_uds_10mb_3k_timestamped_contexts_cpu | % cpu utilization | -0.15 | [-0.40, +0.09] | 1 | Logs |
| ➖ | python_openmetrics | % cpu utilization | -0.22 | [-0.89, +0.46] | 1 | Logs bounds checks dashboard |
| ➖ | quality_gate_private_action_runner | memory utilization | -0.32 | [-0.45, -0.19] | 1 | Logs bounds checks dashboard |
| ➖ | dsd_uds_10mb_3k_timestamped_contexts_memory | memory utilization | -0.46 | [-0.67, -0.24] | 1 | Logs |
| ➖ | quality_gate_logs | % cpu utilization | -1.85 | [-2.72, -0.98] | 1 | Logs bounds checks dashboard |
</details>
<details>
<summary><h2>
Bounds Checks: ✅ Passed
</h2></summary>
| perf | experiment | bounds_check_name | replicates_passed | observed_value | links |
|------|------------------------------------|-----------------------|-------------------|-----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ✅ | python_openmetrics | checks_execution_time | 10/10 | 78.15 ≤ 100 | bounds checks dashboard |
| ✅ | python_openmetrics | cpu_usage | 10/10 | 1341.84 ≤ 1500 | bounds checks dashboard |
| ✅ | python_openmetrics | memory_usage | 10/10 | 4.35GiB ≤ 4.75GiB | bounds checks dashboard |
| ✅ | quality_gate_idle | intake_connections | 10/10 | 4 ≤ 5 | bounds checks dashboard |
| ✅ | quality_gate_idle | memory_usage | 10/10 | 176.06MiB ≤ 181MiB | bounds checks dashboard |
| ✅ | quality_gate_idle | total_bytes_received | 10/10 | 753.36KiB ≤ 819.20KiB | bounds checks dashboard |
| ✅ | quality_gate_idle_all_features | intake_connections | 10/10 | 2 ≤ 5 | bounds checks dashboard |
| ✅ | quality_gate_idle_all_features | memory_usage | 10/10 | 468.33MiB ≤ 542MiB | bounds checks dashboard |
| ✅ | quality_gate_idle_all_features | total_bytes_received | 10/10 | 1.12MiB ≤ 1.25MiB | bounds checks dashboard |
| ✅ | quality_gate_logs | intake_connections | 10/10 | 19 ≤ 40 | bounds checks dashboard |
| ✅ | quality_gate_logs | memory_usage | 10/10 | 213.14MiB ≤ 228MiB | bounds checks dashboard |
| ✅ | quality_gate_logs | missed_bytes | 10/10 | 0B = 0B | bounds checks dashboard |
| ✅ | quality_gate_logs | total_bytes_received | 10/10 | 263.48MiB ≤ 292MiB | bounds checks dashboard |
| ✅ | quality_gate_metrics_logs | cpu_usage | 10/10 | 352.75 ≤ 2000 | bounds checks dashboard |
| ✅ | quality_gate_metrics_logs | intake_connections | 10/10 | 19 ≤ 40 | bounds checks dashboard |
| ✅ | quality_gate_metrics_logs | memory_usage | 10/10 | 413.55MiB ≤ 455MiB | bounds checks dashboard |
| ✅ | quality_gate_metrics_logs | missed_bytes | 10/10 | 0B = 0B | bounds checks dashboard |
| ✅ | quality_gate_metrics_logs | total_bytes_received | 10/10 | 0.94GiB ≤ 1.04GiB | bounds checks dashboard |
| ✅ | quality_gate_private_action_runner | memory_usage | 10/10 | 72.59MiB ≤ 75MiB | bounds checks dashboard |
| ✅ | quality_gate_security_idle | cpu_usage | 10/10 | 28.06 ≤ 100 | bounds checks dashboard |
| ✅ | quality_gate_security_idle | memory_usage | 10/10 | 327.70MiB ≤ 357MiB | bounds checks dashboard |
| ✅ | quality_gate_security_mean_fs_load | cpu_usage | 10/10 | 61.35 ≤ 200 | bounds checks dashboard |
| ✅ | quality_gate_security_mean_fs_load | memory_usage | 10/10 | 303.23MiB ≤ 337MiB | bounds checks dashboard |
| ✅ | quality_gate_security_no_fs_load | cpu_usage | 10/10 | 22.52 ≤ 100 | bounds checks dashboard |
| ✅ | quality_gate_security_no_fs_load | memory_usage | 10/10 | 313.00MiB ≤ 348MiB | bounds checks dashboard |
</details>
<details>
<summary><h2>
Explanation
</h2></summary>
Confidence level: 90.00%
Effect size tolerance: |Δ mean %| ≥ 5.00%
Performance changes are noted in the perf column of each table:
✅ = significantly better comparison variant performance
❌ = significantly worse comparison variant performance
➖ = no significant change in performance
A regression test is an A/B test of target performance in a repeatable rig, where "performance" is measured as "comparison variant minus baseline variant" for an optimization goal (e.g., ingress throughput). Due to intrinsic variability in measuring that goal, we can only estimate its mean value for each experiment; we report uncertainty in that value as a 90.00% confidence interval denoted "Δ mean % CI".
For each experiment, we decide whether a change in performance is a "regression" -- a change worth investigating further -- if all of the following criteria are true:
1. Its estimated |Δ mean %| ≥ 5.00%, indicating the change is big enough to merit a closer look.
2. Its 90.00% confidence interval "Δ mean % CI" does not contain zero, indicating that if our statistical model is accurate*, there is at least a 90.00% chance there is a difference in performance between baseline and comparison variants.
3. Its configuration does not mark it "erratic".
</details>
CI Pass/Fail Decision
✅ Passed. All Quality Gates passed.
AI review by Codex (OpenAI) - workflow run
patch is correct. No actionable regressions found. Regex patterns and compilation modes are preserved, and the shared instances introduce no identified mutation or initialization hazards. Tests were not run in the read-only environment.
Approved these changes.
I'm a little worried that this moves the cost (cpu and memory usage) to anything which imports the package even without using it, but the scrubber should always be initialized anyway so should be fine
Approved these changes.
I am thinking a bit more about this and I am not sure if moving all entries to the package level is a good idea. For example, in this case, this function gets called only once, and only when the GPU monitoring module is initialized. This PR would force the regex to always get compiled even if it's not used.
Approved these changes.
Approved these changes.
Approved these changes.
Agree with the change overall and I think the comments from Pierre & Guillermo make sense, the move can be suboptimal sometimes, you might want to consider it.
Approved these changes.
Approved these changes.
Approved these changes.
Note: this can be a memory de-optimization in cases where a codepath is either not used or isn't inside any kind of a loop. Now any code which imports the package will pay the cpu/memory cost of regex compilation at init time.
Approved these changes.
Approved these changes.
Approved these changes.
Approved these changes.
This pull request has been automatically marked as stale because it has not had activity in the past 15 days.
It will be closed in 30 days if no further activity occurs. If this pull request is still relevant, adding a comment or pushing new commits will keep it open. Also, you can always reopen the pull request if you missed the window.
Thank you for your contributions!
ichinaski
louis-cqrl
What does this PR do?
Hoists constant-pattern
regexp.MustCompile/regexp.MustCompilePOSIXcalls to package-level vars — but only on hot paths: functions that run many times or compile inside loops. 13 hoists across 9 files.Motivation
Compiling a constant regexp on every call wastes CPU on hot paths. An earlier revision of this PR hoisted all ~111 such sites repo-wide, but reviewers noted that hoisting in once-run functions forces unconditional package-init compilation even when the feature is disabled, for no runtime benefit. This revision keeps only the genuinely hot-path hoists and reverts the once-run ones.
Describe how you validated your changes
dda inv test --targets=./pkg/config/utils/— 88 tests passgofmtclean on all touched filesAdditional Notes
Companion linter PR #57310 adds a loop-scoped
regexphoistrule to guard against future in-loop constant compiles.