microsoft / microsoft/FASTER

Suggestion: C#: "skip locals init" and "readonly"

Open
#904 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
6.6k
Forks
595
PR merge metrics
No merged PRs in 30d

Description

I wanted to discuss these before jumping in;

  1. skip locals init
  2. readonly

skip locals init

[module:SkipLocalsInit]`

This changes .locals init to .locals at the IL level;
This can make a measurable and worthwhile difference for some scenarios, especially where stackalloc etc is used; for "normal" code, "definite assignment" rules means this is safe; however, there are a few unsafe and stackalloc scenarios where you need to be careful - for example, your fresh stackalloc data will not be zero'd - needs a code review in particular around any stackalloc or "fixed buffer" scenarios.

This is on-by-default in most framework code, and is well understood. Example here


readonly

Marking non-mutating methods on struct types as readonly (when the struct is not itself readonly) has significant advantages for code that uses in or ref readonly semantics (which might be inside your library, or in consumer code) - avoiding a defensive copy of the struct. This should be fully automated via IDE0251 - you should be safe to blindly accept IDE0251 project-wide; for example, for SpanByte it should be fine for Pointer, ToPointer(), Serialized, MetadataSize, Length get, TotalSize, LengthWithoutMetadata, Invalid get, ToPointerWithMetadata, ExtraMetadata get, AsSpan(), AsReadOnlySpan(), AsSpanWithMetadata(), AsReadOnlySpanWithMetadata(), Deserialize(), ToMemoryOwner(), ToByteArray(), ToString(), CopyTo(), TryCopyTo() - and possiibly a few others!

in some cases you might need to cheat, for example changing Unsafe.AsPointer(ref payload) to Unsafe.AsPointer(ref Unsafe.AsRef(in payload)) (which is JITted away to nothing, but makes the compiler happy to readonly)

Happy to contribute some time towards this if they're desirable

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the two proposals in the issue and locating the affected C# projects, structs, and any existing compiler or performance guidance. The issue names no files or tests, so first establish the scope and validation approach with maintainers. Done means the agreed changes are applied safely, including review of unsafe and stackalloc cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
performance
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.