Skip to content

feat(client): improve perfomance of generating path for client - #2046

Merged
mromaszewicz merged 7 commits into
oapi-codegen:mainfrom
sterligov:feat-gen-client-path-string
Aug 1, 2026
Merged

mromaszewicz merged 7 commits into
oapi-codegen:mainfrom
sterligov:feat-gen-client-path-string

Conversation

@sterligov

Copy link
Copy Markdown
Contributor

In this pull request, I made a small performance improvement to the client by using string concatenation instead of fmt.Sprintf. Below, I’ll include the benchmark results.

goos: darwin
goarch: arm64
pkg: github.com/oapi-codegen/oapi-codegen/v2/pkg/codegen
cpu: Apple M1 Pro
BenchmarkFmtPath1param
BenchmarkFmtPath1param-10       	21433833	        49.99 ns/op	      16 B/op	       1 allocs/op
BenchmarkConcatPath1param
BenchmarkConcatPath1param-10    	73644105	        16.27 ns/op	       0 B/op	       0 allocs/op
BenchmarkFmtPath2param
BenchmarkFmtPath2param-10       	18985250	        62.78 ns/op	      16 B/op	       1 allocs/op
BenchmarkConcatPath2param
BenchmarkConcatPath2param-10    	55796110	        21.45 ns/op	       0 B/op	       0 allocs/op
BenchmarkFmtPath3param
BenchmarkFmtPath3param-10       	15205623	        79.05 ns/op	      24 B/op	       1 allocs/op
BenchmarkConcatPath3param
BenchmarkConcatPath3param-10    	41759646	        28.44 ns/op	       0 B/op	       0 allocs/op
PASS

@sterligov
sterligov requested a review from a team as a code owner August 2, 2025 05:06
@kusari-inspector

kusari-inspector Bot commented Aug 2, 2025 •

Copy link
Copy Markdown

Kusari Analysis Results

Analysis for commit: b690045, performed at: 2025-08-02T11:55:19Z

• @kusari-inspector rerun - Trigger a re-analysis of this PR

• @kusari-inspector feedback [your message] - Send feedback to our AI and team


Recommendation

✅ PROCEED with this Pull Request

Summary

✅ No Flagged Issues Detected

All values appear to be within acceptable risk parameters.

No pinned version dependency changes, code issues or exposed secrets detected!

Found this helpful? Give it a 👍 or 👎 reaction!

@sterligov
sterligov force-pushed the feat-gen-client-path-string branch from 501b237 to ea62ed3 Compare August 2, 2025 05:09
@kusari-inspector

Copy link
Copy Markdown

Kusari PR Analysis rerun based on - ea62ed3 performed at: 2025-08-02T05:09:15Z - link to updated analysis

@sterligov
sterligov force-pushed the feat-gen-client-path-string branch from ea62ed3 to fab1963 Compare August 2, 2025 05:11
@kusari-inspector

Copy link
Copy Markdown

Kusari PR Analysis rerun based on - fab1963 performed at: 2025-08-02T05:11:59Z - link to updated analysis

@sterligov
sterligov force-pushed the feat-gen-client-path-string branch from fab1963 to a3cd398 Compare August 2, 2025 06:16
@kusari-inspector

Copy link
Copy Markdown

Kusari PR Analysis rerun based on - a3cd398 performed at: 2025-08-02T06:16:38Z - link to updated analysis

@sterligov
sterligov force-pushed the feat-gen-client-path-string branch from a3cd398 to 1511442 Compare August 2, 2025 07:22
@kusari-inspector

Copy link
Copy Markdown

Kusari PR Analysis rerun based on - 1511442 performed at: 2025-08-02T07:22:05Z - link to updated analysis

@jamietanna

Copy link
Copy Markdown
Member

Thanks, will try and review at some point. Is it worth committing the benchmark code for future testing?

@kusari-inspector

Copy link
Copy Markdown

Kusari PR Analysis rerun based on - 3acf76d performed at: 2025-08-02T11:53:02Z - link to updated analysis

@sterligov

Copy link
Copy Markdown
Contributor Author

Thanks, will try and review at some point. Is it worth committing the benchmark code for future testing?

I added a commit with a benchmark test

@sterligov
sterligov force-pushed the feat-gen-client-path-string branch from 3acf76d to b690045 Compare August 2, 2025 11:55
@kusari-inspector

Copy link
Copy Markdown

Kusari PR Analysis rerun based on - b690045 performed at: 2025-08-02T11:55:19Z - link to updated analysis

@jamietanna

Copy link
Copy Markdown
Member

Thanks. From what I can see:

Before:

% cd ./pkg/codegen
% go test ./ -bench=.
goos: linux
goarch: amd64
pkg: github.com/oapi-codegen/oapi-codegen/v2/pkg/codegen
cpu: 12th Gen Intel(R) Core(TM) i7-12800H
Benchmark_concatPath1param-20           131724429               10.39 ns/op            0 B/op          0 allocs/op
Benchmark_concatPath2param-20           25299087                44.80 ns/op           48 B/op          1 allocs/op
PASS
ok      github.com/oapi-codegen/oapi-codegen/v2/pkg/codegen     3.672s
go test ./ -bench=.  5.07s user 0.64s system 141% cpu 4.032 total

After:

% cd ./pkg/codegen
% go test ./ -bench=.
goos: linux
goarch: amd64
pkg: github.com/oapi-codegen/oapi-codegen/v2/pkg/codegen
cpu: 12th Gen Intel(R) Core(TM) i7-12800H
Benchmark_concatPath1param-20           130095781                8.806 ns/op           0 B/op          0 allocs/op
Benchmark_concatPath2param-20           34524310                37.85 ns/op           48 B/op          1 allocs/op
PASS
ok      github.com/oapi-codegen/oapi-codegen/v2/pkg/codegen     3.674s
go test ./ -bench=.  4.89s user 0.53s system 135% cpu 3.994 total

Is that right? The performance improvement seems fairly low

# Conflicts:
#	examples/generate/serverurls/gen.go
#	internal/test/any_of/codegen/ref_schema/openapi.gen.go
#	internal/test/any_of/param/param.gen.go
#	internal/test/issues/issue-1180/issue.gen.go
#	internal/test/issues/issue-1298/issue1298.gen.go
#	internal/test/issues/issue-1397/issue1397.gen.go
#	internal/test/issues/issue-grab_import_names/issue.gen.go
#	internal/test/options/response_getters/enabled/response_getters.gen.go
#	internal/test/references/external/pathalias/client.gen.go
#	internal/test/references/external/response_refs/pkg2/pkg2.gen.go
#	internal/test/references/external/x_go_name_qualification/gen/spec_base/issue.gen.go
#	internal/test/schemas/schemas.gen.go
#	internal/test/strict-server/client/client.gen.go
Regenerates all generated code so the new string-concatenation path
builder in client.tmpl is applied to the test and example files that
moved during the "refactor all tests by category" reorganization.
@mromaszewicz mromaszewicz added bug Something isn't working enhancement New feature or request labels Aug 1, 2026
GenPathString built a format string and ran it through fmt.Sprintf at
generation time, so any percent sign in a spec path was interpreted as a
format verb. Percent-encoded paths such as /search/%20/{id} are legal
OpenAPI and produced corrupt output like

    "/search/%!/(string=  " + pathParam0 + ")%!s(MISSING)"

Deriving the parameter count from strings.Count(tmpl, "%s") compounded
this: a path containing a literal "%s" was counted as a parameter, so
/literal%s/{id} emitted a reference to an undeclared pathParam1.

Literal segments were also pasted into the generated source unquoted,
so a path containing a double quote or a backslash produced Go code
that did not compile or escaped incorrectly.

Build the concatenation from the regexp match offsets instead, quoting
each literal segment with strconv.Quote. Percent signs, quotes and
backslashes are then correct by construction, and the parameter count
comes from the number of matches rather than from counting needles in
an intermediate string.

Output is unchanged for every spec in the repository; `make generate`
produces no diff.
The surrounding template helpers are genParamArgs, genParamTypes and
genParamNames, and the Go function backing this one is GenPathString,
so getPathString reads as a typo. Rename it before it settles.

Generated output is unchanged.
@mromaszewicz

Copy link
Copy Markdown
Member

@greptileai review

@greptile-apps

greptile-apps Bot commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR replaces fmt.Sprintf-based client path generation with Go string literals and concatenation while preserving path-parameter ordering and reuse.

  • Adds GenPathString to generate escaped concatenation expressions.
  • Updates the client template and regenerated client fixtures.
  • Covers static, escaped, prefixed, and repeated path parameters with unit tests.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains; the previously reported repeated-parameter compilation issue is addressed by reusing the first variable index for duplicate placeholders, and the other prior compatibility concern was explicitly accepted as intentional.

Important Files Changed

Filename Overview
pkg/codegen/utils.go Adds path-expression generation whose placeholder normalization and deduplication align with the existing path-parameter ordering logic.
pkg/codegen/templates/client.tmpl Uses the new helper to emit string concatenation instead of runtime formatting for generated client paths.
pkg/codegen/template_helpers.go Registers the new path-generation helper; the removed legacy helper was already reviewed and explicitly accepted as an intentional compatibility change.
pkg/codegen/utils_test.go Adds focused coverage for literals, escaping, parameter ordering, repeated parameters, and supported placeholder forms.

Reviews (2): Last reviewed commit: "fix(codegen): reuse the same variable fo..." | Re-trigger Greptile

Comment thread pkg/codegen/utils.go Resolved
Comment thread pkg/codegen/template_helpers.go Resolved
A path parameter may appear more than once in one path, as in the
Keycloak-style /admin/realms/{realm}/clients/{client-uuid}/roles/{role-name}/composites/clients/{client-uuid}
that motivated oapi-codegen#2220. SortParamsByPath deduplicates such parameters and
the client template declares one variable per deduplicated parameter,
but GenPathString numbered variables by occurrence, so the second
{client-uuid} referred to a pathParam that was never declared and the
generated client failed to compile.

Key the variable index by parameter name so repeats resolve back to the
first occurrence, and treat the prefixed and exploded forms {.param} and
{;param*} as the same parameter, matching OrderedParamsFromUri.

This also corrects the output rather than only restoring compilation.
The previous fmt.Sprintf form emitted one %s per occurrence but only one
argument per unique parameter, so the generated code failed go vet and
put a literal %!s(MISSING) in the request path at runtime.
@mromaszewicz

Copy link
Copy Markdown
Member

I found a few real bugs (and then so did Greptile) when reviewing your PR. I've pushed fixes and I'll merge this, so you get PR credit. Thanks for doing this. It's always good to avoid unnecessary function calls, even if the gain is minimal when you look at the big picture, vs a synthetic benchmark.

@mromaszewicz
mromaszewicz merged commit b7f2e1d into oapi-codegen:main Aug 1, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants