Skip to content

Fix parser fallback and improve wareki performance - #29

Merged
sugi merged 7 commits into
masterfrom
fix/unsupported-fallback-kansuji-cache
Jul 13, 2026
Merged

Fix parser fallback and improve wareki performance#29
sugi merged 7 commits into
masterfrom
fix/unsupported-fallback-kansuji-cache

Conversation

@sugi

@sugi sugi commented Jul 13, 2026

Copy link
Copy Markdown
Owner

Summary

  • preserve Wareki::UnsupportedDateRange for recognized but unsupported wareki dates instead of falling back to stdlib parsing
  • cache simple kansuji strings for values from 0 through 99 while preserving mutable return values
  • send ASCII-only Date.parse and Date._parse inputs directly to the saved stdlib parsers
  • deep-freeze calendar year definitions and update the calendar data generator to emit the same structure

Performance

  • %JF: about 30.8 µs to 2.1 µs per call
  • %JTF: about 32.1 µs to 2.5 µs per call
  • ASCII Date.parse: about 2.8 µs to 2.2 µs per call, approximately 0.01 µs above the saved stdlib parser in the independent review run

The kansuji cache adds about 0.95 ms and 9,400 transient allocations during initialization, so the formatting numbers describe steady-state performance.
Deep-freezing all 1,428 calendar year definitions adds about 0.07 ms during initialization.

Correctness and compatibility

  • Wareki.parse_to_date, Date.parse, Date._parse, and Time.parse now agree on unsupported wareki ranges
  • normal stdlib fallback remains covered, including the path after the wareki quick filter matches
  • cached formatting results remain mutable and cannot mutate the cache
  • YEAR_DEFS, every Year, and their month_starts and month_days arrays are frozen
  • generated calendar definitions retain the same deep-freeze invariant
  • Ruby 2.0-compatible syntax is preserved and the affected paths were checked on Ruby 2.0

Test plan

  • bundle exec rspec (92 examples, 0 failures)
  • bundle exec rubocop (17 files, no offenses)
  • Ruby syntax checks for all Ruby files
  • independent sub-agent review of parser boundaries, performance claims, cache compatibility, and calendar immutability

Implementation plans

  • docs/superpowers/plans/2026-07-14-fallback-and-kansuji-performance-fixes.md
  • docs/superpowers/plans/2026-07-14-parse-fast-path-and-calendar-freeze.md

sugi and others added 6 commits July 14, 2026 00:35
Co-authored-by: Codex <codex@openai.com>
Co-authored-by: Codex <codex@openai.com>
Co-authored-by: Codex <codex@openai.com>
Co-authored-by: Codex <codex@openai.com>
Co-authored-by: Codex <codex@openai.com>
Co-authored-by: Codex <codex@openai.com>
@coveralls

coveralls commented Jul 13, 2026

Copy link
Copy Markdown

Coverage Status

Coverage is 97.13%fix/unsupported-fallback-kansuji-cache into master. No base build found for master.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces performance optimizations and robustness improvements to the wareki library. It adds an early-return fast path for ASCII-only date strings, caches simple Kansuji conversions for numbers 0–99, deep-freezes calendar year definitions to prevent modification, and ensures that UnsupportedDateRange errors are preserved rather than falling back to standard parsing. The review feedback suggests adding an explicit type check in the new to_simple_kan helper to prevent runtime errors when non-integer values are passed.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread lib/wareki/utils.rb Outdated

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

This PR tightens the boundary between Wareki parsing and stdlib parsing (preserving Wareki::UnsupportedDateRange for recognized-but-unsupported inputs), while also reducing overhead on hot parsing/formatting paths via an ASCII fast-path and a small kansuji cache. It additionally makes the lunisolar calendar definition data immutable (deep-freeze) and updates the generator to keep that invariant.

Changes:

  • Preserve Wareki::UnsupportedDateRange by removing it from stdlib-fallback rescue paths in Wareki.parse_to_date and the Date monkey-patches.
  • Add an ASCII-only fast path for Date.parse / Date._parse to bypass Wareki normalization/filtering when not needed.
  • Cache 0..99 :simple kansuji strings (returning mutable copies) and deep-freeze YEAR_DEFS (and generator output) to prevent runtime mutation.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.

Show a summary per file
File Description
spec/wareki_spec.rb Updates expectations to require UnsupportedDateRange preservation for recognized Wareki inputs.
spec/utils_spec.rb Adds coverage for kansuji cache behavior/mutability and deep-freeze invariants on calendar definitions.
spec/std_ext_spec.rb Adds regression tests for unsupported-range errors, ASCII fast path, and non-ASCII Wareki path behavior.
lib/wareki/utils.rb Introduces SIMPLE_KANSUJI_CACHE and to_simple_kan, and switches time formatting to use the cache.
lib/wareki/std_ext.rb Adds ASCII-only direct delegation to original parsers; preserves unsupported-range errors by narrowing rescue.
lib/wareki/date.rb Switches date-format kansuji directives to use Utils.to_simple_kan.
lib/wareki/common.rb Preserves unsupported-range errors in parse_to_date by narrowing rescue to ArgumentError.
lib/wareki/calendar_def.rb Implements deep-freeze of year definitions and nested arrays, then freezes YEAR_DEFS.
build-util/gen-jp-cal-def.rb Updates generator to emit the same deep-freeze structure as calendar_def.rb.
docs/superpowers/plans/2026-07-14-parse-fast-path-and-calendar-freeze.md Documents the ASCII fast-path + deep-freeze work plan.
docs/superpowers/plans/2026-07-14-fallback-and-kansuji-performance-fixes.md Documents the fallback boundary + kansuji cache work plan.

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

Co-authored-by: Codex <codex@openai.com>
@sugi
sugi merged commit faa23c4 into master Jul 13, 2026
9 checks passed
sugi added a commit that referenced this pull request Jul 13, 2026
Move the era-definitions regeneration entry from the 2.0.0 section to
2.1.0; it was merged after the v2.0.0 tag, and the released 2.0.0 gem
still contains the old era boundaries. Also add the post-2.0.0 parser
and kansuji changes from PR #29.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0183s3PMdYWFbdUEkQQcYaUz
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