Skip to content
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

Stop consuming result of GetPinnedReference() #1280

Merged
merged 2 commits into from
Apr 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions src/benchmarks/micro/libraries/System.Memory/ReadOnlySpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,33 @@ namespace System.Memory
public class ReadOnlySpan
{
private readonly string _sampeString = "this is a very nice sample string";
private readonly Consumer _consumer = new Consumer();

[Benchmark]
public ReadOnlySpan<char> StringAsSpan() => _sampeString.AsSpan();

[Benchmark(OperationsPerInvoke = 16)]
public void GetPinnableReference()
[Benchmark(OperationsPerInvoke = 16 * 2)]
public char GetPinnableReference()
{
ReadOnlySpan<char> span = _sampeString.AsSpan();
var consumer = _consumer;

consumer.Consume(span.GetPinnableReference()); consumer.Consume(span.GetPinnableReference());
consumer.Consume(span.GetPinnableReference()); consumer.Consume(span.GetPinnableReference());
consumer.Consume(span.GetPinnableReference()); consumer.Consume(span.GetPinnableReference());
consumer.Consume(span.GetPinnableReference()); consumer.Consume(span.GetPinnableReference());
consumer.Consume(span.GetPinnableReference()); consumer.Consume(span.GetPinnableReference());
consumer.Consume(span.GetPinnableReference()); consumer.Consume(span.GetPinnableReference());
consumer.Consume(span.GetPinnableReference()); consumer.Consume(span.GetPinnableReference());
consumer.Consume(span.GetPinnableReference()); consumer.Consume(span.GetPinnableReference());
char c;

c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
Comment on lines +26 to +41
Copy link
Member

Choose a reason for hiding this comment

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

is there a possibility that in the future JIT is going to replace all 32 calls with a single one?

@EgorBo or perhaps it can happen as of today with Mono using LLVM?

If so, we should most probably xor the results

Suggested change
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c = span.GetPinnableReference();
c = span.GetPinnableReference(); c ^= span.GetPinnableReference();
c ^= span.GetPinnableReference(); c ^= span.GetPinnableReference();
c ^= span.GetPinnableReference(); c ^= span.GetPinnableReference();
c ^= span.GetPinnableReference(); c ^= span.GetPinnableReference();
c ^= span.GetPinnableReference(); c ^= span.GetPinnableReference();
c ^= span.GetPinnableReference(); c ^= span.GetPinnableReference();
c ^= span.GetPinnableReference(); c ^= span.GetPinnableReference();
c ^= span.GetPinnableReference(); c ^= span.GetPinnableReference();
c ^= span.GetPinnableReference(); c ^= span.GetPinnableReference();
c ^= span.GetPinnableReference(); c ^= span.GetPinnableReference();
c ^= span.GetPinnableReference(); c ^= span.GetPinnableReference();
c ^= span.GetPinnableReference(); c ^= span.GetPinnableReference();
c ^= span.GetPinnableReference(); c ^= span.GetPinnableReference();
c ^= span.GetPinnableReference(); c ^= span.GetPinnableReference();
c ^= span.GetPinnableReference(); c ^= span.GetPinnableReference();
c ^= span.GetPinnableReference(); c ^= span.GetPinnableReference();

Copy link
Member

Choose a reason for hiding this comment

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

@adamsitnik I've just checked - Mono-LLVM optimizes out all the dead stores (basicaly, does span.GetPinnableReference() only once). The "^=" trick doesn't help 🙂 (with the proposed change it optimized the whole function to return 0

Copy link
Member

Choose a reason for hiding this comment

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

@EgorBo thank you! Do you know how can we trick the Mono-LLVM ? Mix + with ^ ?

Copy link
Member

Choose a reason for hiding this comment

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

No idea, GetPinnableReference is just too simple and returns a pointer. I guess only if you hide span to a non-inlineable method e.g. GetSpan().GetPinnableReference() ^ .. but I guess the overhead of that method will be bigger than the actual GetPinnableReference

Copy link
Member Author

Choose a reason for hiding this comment

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

Weird. Surprisingly RYUJit doesn't eliminate dead stores (at least for this example). The code that I pasted above is precisely that gets generated today. I will investigate and possibly open an issue to track this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated existing issue with my comments. dotnet/runtime#13727 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

Perhaps we can do something like this?

private static void Consume(in System.Span<T> _) { }

But yeah, that would have method call overhead too.

Copy link
Member

Choose a reason for hiding this comment

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

In such case, I think that I am going to merge it as it is.

If it becomes a problem in the future, we might consider removing this benchmark because it looks like it might be impossible to properly benchmark this method (it's too simple).

/cc @billwert @DrewScoggins

return c;
}

[Benchmark(OperationsPerInvoke = 16)]
Expand Down Expand Up @@ -97,4 +105,4 @@ private static string GenerateInputString(char source, int count, char replaceCh
return new string(str);
}
}
}
}