Skip to content

perf(types): format dates, times and decimals without fmt - #33345

Merged
fengttt merged 2 commits into
matrixorigin:mainfrom
fengttt:perf-temporal-decimal-format
Nov 27, 2026
Merged

fengttt merged 2 commits into
matrixorigin:mainfrom
fengttt:perf-temporal-decimal-format

Conversation

@fengttt

@fengttt fengttt commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • Improvement

Which issue(s) this PR fixes:

issue #10181 (found while profiling step 6b; not part of the type-dispatch stack)

What this PR does / why we need it:

This PR is independent: it's based on main, not on the #10181 stack.

A CPU profile of MySQL result-row encoding (from #33344) showed fmt.Sprintf inside Datetime.String2 and Decimal64.Format taking about 15% of the time for mixed columns. This PR removes fmt from the date, time and decimal formatters.

  • Decimals. Decimal64.Format and Decimal128.Format built their string one digit at a time with fmt.Sprintf("%c") and prepended it, allocating per digit. They now write digits into a stack buffer from the right, with the same control flow, as Decimal256.Format already did. Decimal128 takes its digits by exact 128-bit division by 10 (math/bits.Div64) instead of a rounding Div128 plus a correction.
  • Dates and times. Datetime.String2, Date.String, Time.String2 and Timestamp.String2 write their digits directly. The fmt code stays as the fallback for values the fast path doesn't cover:
    • years outside 0..9999;
    • negative datetimes;
    • TIME hours of three or more digits;
    • a Timestamp scale above 6, which still panics as before.

Speed

BenchmarkFormat, median of 5:

Formatter before after allocs
Decimal64.Format, scale 0 766 ns 26 ns (−97%) 12 → 1
Decimal64.Format, scale 2 406 ns 19 ns (−95%) 7 → 1
Decimal128.Format, scale 4 839 ns 42 ns (−95%) 15 → 1
Datetime.String2, scale 6 329 ns 29 ns (−91%) 5 → 1
Timestamp.String2, scale 6 338 ns 32 ns (−90%) 5 → 1
Time.String2, scale 3 205 ns 20 ns (−90%) 3 → 1
Date.String 123 ns 16 ns (−87%) 2 → 1

End to end, BenchmarkColumnSlicesRows encodes MySQL result rows of 1024 × 8 mixed columns (ints, decimal, varchar, date, datetime):

  • text: 105 → 47 ns/cell (−55%);
  • binary: 121 → 60 ns/cell (−50%);
  • about 4,650 fewer allocations per batch;
  • an all-int64 batch is unchanged.

The benchmark file is identical to the one in #33344, so the two PRs don't conflict.

Correctness

  • The fmt-based implementations are kept in format_legacy_test.go.
  • TestFormatMatchesLegacy requires identical output, including panics. It covers edge values and 20,000 random values per type, scales −3..40 and four time zones: negative and out-of-range datetimes, MinInt64, three-digit TIME hours, the decimal sign bit, and full 128-bit decimals.
  • I checked the test fails when the fraction writer is broken.
  • ./pkg/container/types/, ./pkg/container/vector/, all of ./pkg/frontend/... and all of ./pkg/sql/plan/function/... pass.
  • Lint reports 0 issues.

🤖 Generated with Claude Code

Decimal64.Format and Decimal128.Format built their string one digit at a
time with fmt.Sprintf("%c") and prepended it, allocating per digit. They
now write the digits into a stack buffer from the right, with the same
control flow, as Decimal256.Format already did. Decimal128 takes its
digits by exact 128-bit division by 10 (math/bits.Div64) instead of a
rounding Div128 followed by a correction.

Datetime.String2, Date.String, Time.String2 and Timestamp.String2 write
their digits directly. The fmt code stays as the fallback for values the
fast path does not cover: years outside 0..9999, negative datetimes,
TIME hours of three or more digits, and a Timestamp scale above 6 (which
still panics as before).

BenchmarkFormat (median of 5):
  Decimal64.Format scale 0      766 -> 26 ns  -97%  12 -> 1 allocs
  Decimal64.Format scale 2      406 -> 19 ns  -95%   7 -> 1
  Decimal128.Format scale 4     839 -> 42 ns  -95%  15 -> 1
  Datetime.String2 scale 6      329 -> 29 ns  -91%   5 -> 1
  Timestamp.String2 scale 6     338 -> 32 ns  -90%   5 -> 1
  Time.String2 scale 3          205 -> 20 ns  -90%   3 -> 1
  Date.String                   123 -> 16 ns  -87%   2 -> 1

End to end, BenchmarkColumnSlicesRows (MySQL result rows, 1024 x 8 mixed
columns): text 105 -> 47 ns/cell (-55%), binary 121 -> 60 ns/cell (-50%),
~4,650 fewer allocations per batch; an all-int64 batch is unchanged.

The fmt-based implementations are kept in format_legacy_test.go;
TestFormatMatchesLegacy requires identical output, including panics, for
edge values and 20,000 random values per type, scales -3..40 and four
time zones.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fengttt
fengttt requested a review from XuPeng-SH as a code owner November 26, 2026 22:32
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@mergify

mergify Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

This pull request is merged.

@XuPeng-SH

XuPeng-SH commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: e2f9653f7f1b164d27551606cf5a7ac7f33122d7

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH 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.

mo-bot review · mo-bot-local · review

Commit: e2f9653f7f1b164d27551606cf5a7ac7f33122d7

Assessment: request_changes

The direct digit writers reuse the existing lookup table and substantially improve the measured in-range formatter costs. The date formatter needs an additional boundary guard to preserve behavior for raw dates that reach existing Parquet conversion paths.

Model: gpt-6.1-terra / max

Session: 01a219dc-9732-7b72-bb9a-a6303daa1919 · slot 0 · generation 19

  • P2 pkg/container/types/date.go:443: A Parquet DATE value of -719164 days since the Unix epoch is converted to a VARCHAR or JSON value.
    Impact: The conversion now panics with an index-out-of-range error instead of returning the string produced by the previous formatter, interrupting the affected query or load.
    Evidence: The value maps to Date(-2), whose Calendar(true) result is (1, 2, 224). The fast path checks only the year before using day 224 to index the 100-entry digit table. The Parquet conversion at pkg/sql/colexec/external/parquet.go:4615 passes raw dates to String(). A focused conversion test passed on the merge base and failed on the PR head, while an ordinary-date control passed on both. Route out-of-range month or day components through the existing slow formatter, and cover this boundary.

Validation:

  • Private /tmp and loopback preflight passed; receipt checks/1795737165177704805-5.json.

  • The full pkg/container/types test suite passed, including TestFormatMatchesLegacy; receipt checks/1795737361387199642-5.json and types-tests.log.

  • The six legacy formatter test bodies match the frozen merge-base implementations under AST formatting; receipt checks/1795737490367591318-6.json.

  • Three bounded BenchmarkFormat repetitions showed substantial in-range component improvements; receipt checks/1795737460549016591-5.json and format-benchmarks.log.

  • The focused Parquet conversion probe failed on the candidate and passed on the merge base; receipts checks/1795737654219096885-7.json and checks/1795737733455858830-5.json.

Limitations:

  • The Parquet probe exercised the conversion helper used by the mapper, rather than a running SQL service.

  • The frontend end-to-end benchmark reported in the PR was not independently rerun.

@fengttt
fengttt merged commit be6eb76 into matrixorigin:main Nov 27, 2026
5 of 7 checks passed

This branch was successfully deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants