Skip to content

Add scoped everywhere #252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2025
Merged

Conversation

Joy-less
Copy link
Contributor

@Joy-less Joy-less commented May 3, 2025

Adds the scoped keyword everywhere to ensure that stackalloc is always able to be used. This is especially relevant fo ValueStringBuilder(ReadOnlySpan<char> initialText).

Copy link

sonarqubecloud bot commented May 3, 2025

@linkdotnet
Copy link
Owner

Adds the scoped keyword everywhere to ensure that stackalloc is always able to be used. This is especially relevant fo ValueStringBuilder(ReadOnlySpan<char> initialText).

Wouldn't stackalloc be more prone to Span<char>? Where this isn't needed.

Therefore, which scenarios would we enable with that change?

@Joy-less
Copy link
Contributor Author

Joy-less commented May 3, 2025

Adds the scoped keyword everywhere to ensure that stackalloc is always able to be used. This is especially relevant fo ValueStringBuilder(ReadOnlySpan<char> initialText).

Wouldn't stackalloc be more prone to Span<char>? Where this isn't needed.

Therefore, which scenarios would we enable with that change?

I've played around and I'm not really sure what the purpose of the scoped keyword is. It seems like in many cases the compiler just allows stackalloc anyway. It's not really clear, but it seems like it's necessary in some situations.

Nevertheless, I noticed that scoped was used intermittently in the code base so I decided to use it absolutely everywhere where it's possible. I thought if we are using [MethodImpl.AggressiveInlining] everywhere we might as well use scoped everywhere as well. It doesn't hurt and it's a free optimisation wherever it is needed.

It's honestly up to you if you accept this.

@linkdotnet
Copy link
Owner

linkdotnet commented May 3, 2025

Imagine a function like:

public void Append(Span<char> str)

In theory we could do something inside like

public void Append(Span<char> str)
{
    this.buffer = str;

So now we have a function that calls that:

public void Append(Rune value)
{
    Span<char> valueChars = stackalloc char[2];
    var valueCharsWritten = value.EncodeToUtf16(valueChars);
    ReadOnlySpan<char> valueCharsSlice = valueChars[..valueCharsWritten];

    Append(valueCharsSlice);
}

Now the valueCharsSlice could potentially outlive the function (as we assign it to buffer), which will result in undefined/unsafe behavior. Basically, scoped will guarantee that we don't assign/outlive the scope.

So we could add this everywhere, as it doesn't "destroy" anything. Have to think about that.

Copy link
Owner

@linkdotnet linkdotnet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do thunk it is absolutely fine to "decorate" the methods, that don't capture the context

@linkdotnet linkdotnet merged commit 6491ea4 into linkdotnet:main May 6, 2025
2 of 3 checks passed
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.

2 participants