Skip to content

perf(duration): faster and stricter ParseDuration.#246

Merged
fredbi merged 1 commit intogo-openapi:masterfrom
fredbi:perf/duration-regexp
Apr 29, 2026
Merged

perf(duration): faster and stricter ParseDuration.#246
fredbi merged 1 commit intogo-openapi:masterfrom
fredbi:perf/duration-regexp

Conversation

@fredbi
Copy link
Copy Markdown
Member

@fredbi fredbi commented Apr 29, 2026

This PR replaces the previous combo using time.ParseDuration from the standard library and supplementing it with an extra regexp-based parsing.

Performances

Parsing is now single-pass, without regexp: the parser is a modified copy of the one proposed by the standard library, with our additional units and aliases (e.g. "week", "day").

Behavior

The pre-existing tolerance for extra-blank space has been maintained.

Standard library support for "dot-only" decimal part is now generalized (e.g. .25 weeks works).

Lazy prefix-only match has been suppressed, and unit check is stricter with explicit plural forms: to "week" or "weeks" work, but "weekz" no longer matches.

Duration with composite units work with all aliases and blank space tolerance (e.g. "2 minutes 45 seconds" works).

Tests

Edge cases that used to be tested by the standard library only are now tested directly, since time.ParseDuration is no longer called.

Change type

Please select: 🆕 New feature or enhancement|🔧 Bug fix'|📃 Documentation update

Short description

Fixes

Full description

Checklist

  • I have signed all my commits with my name and email (see DCO. This does not require a PGP-signed commit
  • I have rebased and squashed my work, so only one commit remains
  • I have added tests to cover my changes.
  • I have properly enriched go doc comments in code.
  • I have properly documented any breaking change.

This PR replaces the previous combo using time.ParseDuration from the standard library
and supplementing it with an extra regexp-based parsing.

Performances
=============

Parsing is now single-pass, without regexp: the parser is a modified
copy of the one proposed by the standard library, with our additional
units and aliases (e.g. "week", "day").

Behavior
=============

The pre-existing tolerance for extra-blank space has been maintained.

Standard library support for "dot-only" decimal part is now generalized
(e.g. .25 weeks works).

Lazy prefix-only match has been suppressed, and unit check is stricter
with explicit plural forms: to "week" or "weeks" work, but "weekz" no longer matches.

Duration with composite units work with all aliases and blank space
tolerance (e.g. "2 minutes 45 seconds" works).

Tests
=============

Edge cases that used to be tested by the standard library only are now
tested directly, since time.ParseDuration is no longer called.

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 29, 2026

Codecov Report

❌ Patch coverage is 98.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.50%. Comparing base (c09c1cd) to head (bcbde68).
⚠️ Report is 1 commits behind head on master.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
duration.go 98.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #246      +/-   ##
==========================================
+ Coverage   86.13%   86.50%   +0.37%     
==========================================
  Files          18       18              
  Lines        2401     2467      +66     
==========================================
+ Hits         2068     2134      +66     
  Misses        230      230              
  Partials      103      103              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@fredbi fredbi merged commit ebb2f2f into go-openapi:master Apr 29, 2026
22 of 23 checks passed
@fredbi fredbi deleted the perf/duration-regexp branch April 29, 2026 07:46
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.

1 participant