Skip to content

Check the reader, file and comparison with CsCheck, and fix what it found - #101

Merged
Mpdreamz merged 1 commit into
mainfrom
cscheck-properties
Nov 14, 2026
Merged

Mpdreamz merged 1 commit into
mainfrom
cscheck-properties

Conversation

@Mpdreamz

Copy link
Copy Markdown
Contributor

Until now the round trip was checked against six framework assemblies rather than generated APIs, as #95 said. This adds CsCheck properties over generated libraries, and they found seven bugs.

The generator

GeneratedLibrary writes a C# library from a sequence of numbers, one decision per number, the way Hypothesis builds values from a choice sequence. CsCheck generates the sequences and shrinks a failing one: fewer and smaller numbers mean fewer types, members and features, so a failure is reported as a small library.

  • Compiles: every generated library compiles; a failure would show its source.
  • Varied: over 1,000 samples, libraries run from 8 to 94 lines with a median of 38. Every feature in the list below shows up, the rarest (static abstract interface members) in 44 of them.
  • Covers: kinds and modifiers, generics with constraints and variance, base classes and interfaces, nullability, tuples, dynamic, nint, ref kinds, params, defaults, constants, required and init, protected setters, events, operators, conversions, indexers, extension methods, records, nested and protected types, and default and static abstract interface members.

The properties

Property Samples per run
the reader agrees with Roslyn on every visible signature, modifier, constraint and base 150
writing and parsing gives the same text and, field by field, the same model 150
split by namespace or top-level namespace, the files parse back to the same model 150
an API compared with itself, or with its own .api file, has no changes 150
comparing two libraries one way mirrors comparing them the other way 60 pairs

One soak run with 2,000 libraries per property and 800 pairs passed. Each regular run takes about 3 seconds.

What it found, fixed here

Reader

  • notnull was missed when a type constraint sat next to it, as in where T : notnull, IComparable<T>.
  • A property's type took its nullability from the getter's context instead of the type's. That showed up on indexers.
  • dynamic was lost in an in parameter or a ref readonly return that carries a modreq, because the modifier takes a flag of its own.

Parser

  • A delegate whose return type is a tuple lost its name.

Comparison (found by the mirror property)

  • A class that became a struct also reported its constructors changing, and only in one direction.
  • A class that became an enum compared its base types in one direction and its underlying type in the other.
  • Virtual and abstract changes were named differently depending on the direction.

Mutation check: putting the delegate-name bug back fails 3 properties, and putting the dynamic offset bug back fails 1.

api/ is unchanged by the reader fixes; neither library hits those cases. All 630 tool tests pass.

🤖 Generated with Claude Code

…ound

GeneratedLibrary writes a C# library from a sequence of choices, so CsCheck can generate libraries and shrink a
failing one: kinds and modifiers, generics with constraints and variance, base classes and interfaces, nullability,
tuples, dynamic, ref kinds, params, defaults, constants, required and init, events, operators, conversions, indexers,
extension methods, records, nested and protected types, default and static abstract interface members. Every one
compiles. The properties: the reader agrees with Roslyn; writing and parsing gives the same text and model, split
or not; an API compared with itself or its own file has no changes; and comparing two libraries one way mirrors
comparing them the other way.

What they found, fixed:
- notnull was missed next to a type constraint, as in where T : notnull, IComparable<T>
- a property's type took its nullability from the getter's context instead of the type's
- dynamic was lost in an in parameter or ref readonly return that carries a modreq, which takes a flag of its own
- a delegate returning a tuple lost its name in the parser
- a class becoming a struct also reported its constructors, and only one way round
- a class becoming an enum compared its base types one way and its underlying type the other
- virtual and abstract changes were named differently depending on the direction

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 4321719 into main Nov 14, 2026
6 of 7 checks passed
@Mpdreamz
Mpdreamz deleted the cscheck-properties branch November 14, 2026 20:35
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