Skip to content

Generate generic math and modern interface members, and keep nullability out of paths - #102

Merged
Mpdreamz merged 1 commit into
mainfrom
modern-interfaces
Nov 14, 2026
Merged

Mpdreamz merged 1 commit into
mainfrom
modern-interfaces

Conversation

@Mpdreamz

Copy link
Copy Markdown
Contributor

Static abstract interface members and default interface bodies were rare in the generated libraries: static abstract members turned up in 7 of 1,000 and default bodies in 11. They're what modern .NET is built on, and generic math above all, so the generator now writes them on purpose.

Generator

  • A generic-math module in about half the namespaces:
    • an interface over itself (where TSelf : IMath<TSelf>, sometimes also IEquatable<TSelf>) with static abstract operators (+, *, <, >) and properties (Zero), static abstract methods, static virtual and static members, and default instance members;
    • a readonly struct that implements it, sometimes through explicit implementation;
    • a static method constrained to the interface.
  • Plain interfaces gain static abstract and default-bodied properties, and static virtual and sealed members.
  • Fix: an interface that extends one with static abstract members is now kept out of type arguments too (CS8920).

Per 1,000 libraries: static abstract members appear in 405, static virtual in 191, generic-math operators in 377, and default bodies in 51 methods and 22 properties.

What the mirror property found

A member's path took its nullability from whichever version was newer. So comparing in one direction gave Extensions › X6(T0?), and in the other X6(T0). Nullability isn't part of a member's identity, so a path now leaves reference-type annotations out (int? is a different type and stays).

Checks: one soak run with 2,000 libraries per property and 800 pairs passed. The seed that found the path bug fails again with the old path. mise run ci passes: 81 library tests and 630 tool tests.

🤖 Generated with Claude Code

…ity out of paths

The generator now writes the shape generic math is built on: an interface over itself with static abstract operators
and properties, static virtual and default members, a struct that implements it, explicitly or not, and a method
constrained to it. Plain interfaces gain static abstract and default-bodied properties, and static virtual and sealed
members. Static abstract members now appear in about 400 of 1,000 generated libraries, where they were in 7.

Comparing two libraries both ways then showed that a member's path took nullability from whichever version was new,
as X6(T0?) one way and X6(T0) the other. Nullability is not part of a member's identity, so a path now leaves it out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Mpdreamz Mpdreamz added the bug A defect fixed (patch) label Nov 14, 2026
@Mpdreamz
Mpdreamz merged commit 3d66937 into main Nov 14, 2026
6 of 7 checks passed
@Mpdreamz
Mpdreamz deleted the modern-interfaces branch November 14, 2026 20:57
Mpdreamz added a commit that referenced this pull request Nov 14, 2026
…103)

Benchmarks of reading, writing, parsing and comparing, with allocations,
on `System.Collections.Immutable` and CoreLib (35,751 `.api` lines),
using BenchmarkDotNet's `MemoryDiagnoser`. The profiles come from
`dotnet-trace`.

| CoreLib | Before | After |
|---|---|---|
| Read | 407 ms, 832 MB | 108 ms, 109 MB |
| Write | 19 ms, 25 MB | 19 ms, 25 MB |
| Parse | 160 ms, 120 MB | 122 ms, 72 MB |
| Compare with its own `.api` file | **39 s, 4.4 GB** | 20 ms, 23 MB |

On System.Collections.Immutable everything takes 2 ms or less: read 1.8
ms, write 0.45 ms, parse 1.2 ms, compare 0.36 ms.

## What was slow

- **Compare:**
- For every member and parameter it wrote both types out as C# to see
whether they differed.
- It built each member's report path from rendered parameter types,
changed or not.
- Writing a type looked at every member of the type it was in, so the
cost grew with the square of the member count: `AdvSimd`, with 1,772
members, took 5.3 s alone.

Now it compares the models structurally (`Same`), and writes C# and
builds paths only for a change. It pairs members by a structural
identity instead of a string key, checks attributes and constraints on
the models first, and caches one `ApiDeclarationWriter` per type.
- **Read:**
- Decoding an attribute's enum argument, such as
`[EditorBrowsable(EditorBrowsableState.Never)]`, scanned every type in
the assembly.
  - Every attribute check built the attribute type's name again.

Both are looked up once now. Nullable attributes are read from their
blob instead of decoded the general way, primitive types and empty lists
are shared, and names are compared in place.
- **Parse:**
  - Each line was checked for a type declaration twice.
  - Every word was allocated twice, once to peek and once to take.
  - Each member got a new scope.

## What's left

GC pauses are 45–60% of the remaining time. Reading or parsing builds a
model that survives, and background GC handles that poorly. With
`DOTNET_gcConcurrent=0`, CoreLib parses in 61 ms and reads in 105 ms.
The tool now sets
`<ConcurrentGarbageCollection>false</ConcurrentGarbageCollection>`. A
library can't choose its host's GC mode, so library callers keep the
numbers in the table.

## Correctness

- The CsCheck properties from #101 and #102 pass on this branch,
including one soak run with 2,000 libraries per property and 800 pairs,
so the faster paths give the same answers.
- The Roslyn oracle, the round-trip and comparison snapshots, and the
630 tool tests pass.
- `api/` gains the new public `ApiDeclarationWriter`, which `verifyapi`
caught and the regenerated files record.

Run the benchmarks with `dotnet run -c Release --project
benchmarks/Nullean.PublicApi.Benchmarks -- --filter '*' --inProcess`.
They run in-process because BenchmarkDotNet 0.15.8 doesn't recognise
net11.0 when it launches a separate process.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug A defect fixed (patch)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant