Skip to content

fix(criterion-compat): fix URI formatting when criterion_group!/criterion_main! are bypassed#169

Open
not-matthias wants to merge 2 commits into
mainfrom
cod-2324-investigate-issues-with-uri-formatting-in-codspeed-criterion
Open

fix(criterion-compat): fix URI formatting when criterion_group!/criterion_main! are bypassed#169
not-matthias wants to merge 2 commits into
mainfrom
cod-2324-investigate-issues-with-uri-formatting-in-codspeed-criterion

Conversation

@not-matthias

Copy link
Copy Markdown
Member

No description provided.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes CodSpeed Criterion-compat benchmark URI construction when users bypass criterion_group! / criterion_main! (e.g., custom main), ensuring URIs don’t include empty segments and that current_file is derived from the callsite when missing.

Changes:

  • Build benchmark URIs from non-empty components (file / macro group / group name / function) to avoid malformed :: sequences.
  • Derive current_file from #[track_caller] callsite when criterion_group! didn’t set it.
  • Add a new bench reproducer for the “custom main” scenario and register it in Cargo.toml.

Reviewed changes

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

File Description
crates/criterion_compat/src/compat/group.rs Makes URI formatting robust to missing file and/or macro group components.
crates/criterion_compat/src/compat/criterion.rs Adds callsite-based fallback to populate current_file when macros aren’t used.
crates/criterion_compat/benches/repro_custom_main.rs Adds a reproducer bench for bypassing criterion_group!/criterion_main!.
crates/criterion_compat/Cargo.toml Registers the new repro_custom_main bench target.

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

You can also share your feedback on Copilot code review. Take the survey.

Comment thread crates/criterion_compat/src/compat/group.rs Outdated
Comment thread crates/criterion_compat/src/compat/criterion.rs
Comment thread crates/criterion_compat/benches/custom_main.rs
@codspeed-hq

codspeed-hq Bot commented Mar 13, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 18 improved benchmarks
❌ 16 regressed benchmarks
✅ 549 untouched benchmarks
🆕 8 new benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
WallTime find_highest_set_bit[255] 5 ns 47 ns -89.36%
WallTime bench_array2[1] 1 ns 2 ns -50%
Simulation fibo_10 90 ns 119.2 ns -24.48%
Simulation add 91.4 ns 120.6 ns -24.19%
Simulation mul 91.4 ns 120.6 ns -24.19%
Simulation find_highest_set_bit[42] 122.2 ns 151.4 ns -19.27%
Simulation find_highest_set_bit[1024] 122.2 ns 151.4 ns -19.27%
Simulation find_highest_set_bit[255] 122.2 ns 151.4 ns -19.27%
Simulation find_highest_set_bit[65535] 122.2 ns 151.4 ns -19.27%
WallTime iter_batched_per_iteration 46 ns 56 ns -17.86%
WallTime iter_batched_ref_large_input 5 ns 6 ns -16.67%
WallTime iter_with_setup 45 ns 54 ns -16.67%
WallTime iter_batched_large_input 7 ns 8 ns -12.5%
Simulation iter_manual_simple 536.9 ns 566.1 ns -5.15%
Simulation uppercase_chars_with_counter 2.1 µs 2.2 µs -4.03%
Simulation process_items_with_counter 835.8 ns 865 ns -3.37%
WallTime hamiltonian_cycle[5] 1,439 ns 902 ns +59.53%
WallTime vec_copy_with_bytes_counter 57 ns 36 ns +58.33%
WallTime count_set_bits[0] 11 ns 7 ns +57.14%
WallTime string_processing_multi_counter 574 ns 370 ns +55.14%
... ... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing cod-2324-investigate-issues-with-uri-formatting-in-codspeed-criterion (ec46807) with main (99c7b5a)

Open in CodSpeed

@not-matthias not-matthias force-pushed the cod-2324-investigate-issues-with-uri-formatting-in-codspeed-criterion branch from 74da3b2 to 3fddbb2 Compare March 16, 2026 15:33

@GuillaumeLagrange GuillaumeLagrange 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.

olgtm

Comment thread crates/criterion_compat/benches/repro_custom_main.rs Outdated
@not-matthias

Copy link
Copy Markdown
Member Author

@not-matthias not-matthias force-pushed the cod-2324-investigate-issues-with-uri-formatting-in-codspeed-criterion branch from 3fddbb2 to be00bcf Compare June 5, 2026 14:13
@greptile-apps

greptile-apps Bot commented Jun 5, 2026

Copy link
Copy Markdown

Greptile Summary

Fixes malformed URIs like ::::my_group::my_bench that occurred when users bypassed criterion_group!/criterion_main! macros, leaving current_file and macro_group empty. The fix has two parts: build_uri replaces bare format! URI construction by filtering out empty segments before joining with ::, and ensure_current_file lazily resolves the benchmark file path from the #[track_caller] call site when set_current_file was never called.

  • build_uri + caller_file_path are added to crates/codspeed/src/utils.rs as shared utilities, with unit tests covering all three segment-population scenarios (all empty, partial, full).
  • ensure_current_file is added to both the compat Criterion and the fork Criterion, and called unconditionally from benchmark_group; it is a no-op when set_current_file has already been invoked by the macro-generated code.
  • All public bench-entry-point methods (bench_function, bench_with_input, benchmark_group) in both code paths gain #[track_caller] so the implicit caller location propagates correctly through the chain to Location::caller().

Confidence Score: 5/5

Safe to merge — the change is a targeted bug fix with no breaking API surface and a clearly scoped impact limited to URI generation when the standard macros are bypassed.

Both code paths (compat layer and criterion fork) are updated consistently, the fix is guarded by an idempotent early-return in ensure_current_file, and the new utility functions are exercised by unit tests and an integration benchmark covering the exact bypass patterns described in the issue.

No files require special attention.

Important Files Changed

Filename Overview
crates/codspeed/src/utils.rs Adds build_uri (filters empty :: segments) and caller_file_path (resolves call site via #[track_caller]), with accompanying tests.
crates/criterion_compat/src/compat/criterion.rs Adds ensure_current_file helper and #[track_caller] on public bench methods; calls ensure_current_file from benchmark_group for lazy file-path resolution.
crates/criterion_compat/src/compat/group.rs Switches URI construction from bare format! to build_uri, eliminating empty :: segments when macro_group or file path is unset.
crates/criterion_compat/criterion_fork/src/lib.rs Adds ensure_current_file in the codspeed sub-module, #[track_caller] on bench entry points, and calls ensure_current_file from benchmark_group.
crates/criterion_compat/criterion_fork/src/analysis/mod.rs Replaces inline format! URI construction with build_uri, consistent with the compat layer's group.rs change.
crates/criterion_compat/benches/custom_main.rs New integration benchmark demonstrating both bypass patterns (direct call and criterion_group!-routed call) to validate correct URI generation.
crates/criterion_compat/Cargo.toml Registers the new custom_main bench target with harness = false.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["User benchmark file\n(calls bench_function / benchmark_group)"]

    subgraph PatternA ["Pattern A — bypass criterion_group!"]
        A -->|"#[track_caller] chain"| BG["benchmark_group()"]
        BG --> ECF["ensure_current_file()"]
        ECF --> CFP["caller_file_path()\nLocation::caller() → user file path"]
    end

    subgraph PatternB ["Pattern B — through criterion_group! macro"]
        A2["criterion_group!-generated fn"] -->|"set_current_file()\nset_macro_group()"| BG2["benchmark_group()"]
        BG2 --> ECF2["ensure_current_file()\n(no-op — already set)"]
    end

    CFP --> URI["build_uri(&[file, macro_group, group_name])\nfilters empty segments"]
    ECF2 --> URI

    URI --> OUT["crates/...bench.rs::group::bench\n(no :::::: artifacts)"]
Loading

Reviews (3): Last reviewed commit: "test(criterion_compat): add custom main ..." | Re-trigger Greptile

Comment thread crates/criterion_compat/src/compat/criterion.rs Outdated
@not-matthias not-matthias force-pushed the cod-2324-investigate-issues-with-uri-formatting-in-codspeed-criterion branch from be00bcf to 8c539a3 Compare June 5, 2026 14:52
@not-matthias

Copy link
Copy Markdown
Member Author

Works now!
image

@GuillaumeLagrange GuillaumeLagrange 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.

olgtm

Comment thread crates/criterion_compat/criterion_fork/src/lib.rs Outdated
Comment thread crates/criterion_compat/src/compat/criterion.rs Outdated
Comment thread crates/criterion_compat/src/compat/criterion.rs
…bypassed

When users define a custom main instead of criterion_group!/criterion_main!,
current_file and macro_group are never set, producing URIs with empty
segments like `::::my_group::my_bench`.

Derive current_file from the caller location (track_caller) when the macro
did not set it, keeping it inside Criterion, and join URI segments with a
shared build_uri helper that skips empty parts so the fork and compat
implementations stay in sync.
Covers calling bench functions directly with a custom main, both with and
without going through a criterion_group!-generated function.
@not-matthias not-matthias force-pushed the cod-2324-investigate-issues-with-uri-formatting-in-codspeed-criterion branch from 8c539a3 to ec46807 Compare June 10, 2026 10:39
Sign up for free to 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