Skip to content

Merge byUTF inner overloads to reduce template instantiations - #11092

Closed
LightBender wants to merge 1 commit into
dlang:masterfrom
LightBender:byutf-templates
Closed

Merge byUTF inner overloads to reduce template instantiations#11092
LightBender wants to merge 1 commit into
dlang:masterfrom
LightBender:byutf-templates

Conversation

@LightBender

Copy link
Copy Markdown
Contributor

Rationale

Merge the two inner byUTF overloads into one function so autodecodable strings no longer re-enter byUTF after byCodeUnit. Nested Result types now wrap typeof(units) instead of the original range type.

The public eponymous byUTF!(C, flag) API is unchanged, including byChar / byWchar / byDchar, two-stage byUTF!C!R instantiation, qualifier aliasing, and auto ref / by-value R r. Behavior is unchanged; autodecodable inputs cost one fewer inner byUTF instantiation.

Pre-review checklist

  • I have performed a self-review of my code.
  • If my PR fixes a bug or introduces a new feature, I have added thorough tests.
  • If my changes are non-trivial and do not concern a reported issue, I have added a changelog entry.

Existing std.utf unittests cover this internal refactor; no new tests or changelog entry.

LLM/AI disclosure

This PR was written entirely by an LLM/AI.

@schveiguy

schveiguy commented Sep 4, 2026

Copy link
Copy Markdown
Member

This adds more generated code. Instead of wrapping the same implementation with 2 entry points, it reimplements the same function with a small change at the front.

It also does not reduce the number of templates used.

@LightBender

Copy link
Copy Markdown
Contributor Author

Method

Compared parent 982f6034a (two inner overloads) vs a536e384c (merged inner byUTF) with DMD 2.113.0:

  • -c -vtemplates=list-instances
  • same driver: autodecodable strings, byCodeUnit ranges, only ranges, bidirectional/retro, both replacement flags

Caveat: -version=MeasureStrings/Ranges/Mixed only gated main(), so all three compiles were the same mixed program. That does not change the old-vs-new comparison.

Generated code (the bloat metric)

Artifact Old New Delta
Object size 535,310 B 575,246 B +39,936 B (~7.5%)
dumpbin /SYMBOLS lines matching byUTF 931 1,081 +150
dumpbin /SYMBOLS lines matching Result 1,107 1,266 +159

Object size is the better bloat signal. The slightly smaller vtemplate log (327 KB → 321 KB) is a red herring: fewer tiny trampoline listings, more generated methods.

Template instantiations

Outer byUTF(C, Flag) is unchanged: 43 (5 distinct) on both sides.

Inner byUTF(R) per outer C grouping:

Old New
Full implementation 9 distinct (utf.d:4470) 11 distinct of the full body (utf.d:4464)
Autodecodable trampoline 4 distinct (utf.d:4464) gone

Old full-impl instances were mostly ByCodeUnitImpl, dstring, dchar[], OnlyResult!(...). Autodecodable string / wstring / char[] / wchar[] only hit the tiny trampoline, which then forwarded to byUTF(r.byCodeUnit()).

New instances include those same autodecodable types as full-function instantiations: string, wstring, dstring, char[], wchar[], dchar[], ByCodeUnitImpl, OnlyResult!(...).

Driver consume(R):

  • old: 44 (29 distinct)
  • new: 44 (33 distinct)

Same call sites, more distinct return types, so more generated foreach / range methods.

Why this happens

The old trampoline was a one-liner. Merging it into the large function means every autodecodable R now instantiates the decode/encode Result machinery instead of sharing the ByCodeUnitImpl instantiation.

That matches the review: wrapping two entry points around one implementation is cheaper than reimplementing the large function with a small static if at the front.

Goal check

Goal Result
Fewer inner overload declarations Yes (2 → 1)
Fewer template instantiations of the large body No (9 → 11 distinct per C)
Less method bloat / generated code No (~40 KB larger objects, more symbols)

We did not achieve the goal that matters for Phobos.

@LightBender LightBender closed this Sep 4, 2026
@0xEAB 0xEAB added the Review:AI Generated Code that is generated by an LLM AI — or suspected to be. label Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review:AI Generated Code that is generated by an LLM AI — or suspected to be. Review:Refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants