Skip to content

perf: stop allocating a warnings array for a clean compile - #6

Closed
msallin wants to merge 1 commit into
developfrom
perf/empty-warnings-no-alloc
Closed

perf: stop allocating a warnings array for a clean compile#6
msallin wants to merge 1 commit into
developfrom
perf/empty-warnings-no-alloc

Conversation

@msallin

@msallin msallin commented Sep 6, 2026

Copy link
Copy Markdown
Member

Every TypstDocument allocated new string[warningCount] and wrapped it with Array.AsReadOnly, including when there are no warnings at all, which is the common case.

Measured: the zero-length array is 24 bytes and the wrapper another 24. ReadOnlyCollection<string>.Empty allocates nothing. 48 bytes per document, small on its own, but free and on the path every compile takes including the zero-copy one.

Warnings stays IReadOnlyList<string> and stays immutable, which the existing WarningsFromADocumentCannotBeMutatedByCallers test pins. ReadOnlyCollection<T>.Empty is available from net8, so all three target frameworks are covered.

Tests

WarningsFromACleanCompileAreEmptyAndStillImmutable covers the branch that previously had none: empty, enumerable, not a raw string[], and still readable after the document is disposed.

57/57 tests pass. Builds clean on net8, net9 and net10 with zero warnings.

@msallin
msallin force-pushed the perf/empty-warnings-no-alloc branch from 332809d to 91b28e4 Compare September 6, 2026 19:09
Every document allocated a string array and wrapped it with Array.AsReadOnly,
including the common case of no warnings at all, where both are 24 bytes of
pure ceremony. The shared empty collection covers that case without allocating
and keeps Warnings immutable, which a caller-facing list has to stay.
@msallin
msallin force-pushed the perf/empty-warnings-no-alloc branch from 91b28e4 to e6e208e Compare September 6, 2026 19:16
@msallin

msallin commented Sep 6, 2026

Copy link
Copy Markdown
Member Author

Reopened upstream against the parent repository: evolvedlight#49

@msallin msallin closed this Sep 6, 2026
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.

1 participant