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

Add notes regarding using inline delegates with Scheduler.AddOnce #6023

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions osu.Framework.Tests/Threading/SchedulerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ public void TestAddOnceInlineDelegate()
{
classInvocations = 0;

// Note that while this works here (even with a capture), there's no guarantee that will always be the
// case. As such it's always best to use a local function or private method.
Comment on lines +432 to +433
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm... surprised that this works...

In this case the compiler has somehow decided that it can correctly reduce the inline delegate to a method group:

1696945472

For comparison, an actual method group:

1696945522

Wizardry I tell ya...

for (int i = 0; i < 10; i++)
invokeInlineDelegateAction();

Expand Down
2 changes: 1 addition & 1 deletion osu.Framework/Threading/Scheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public bool AddOnce<T>(Action<T> task, T data)
/// Adds a task which will only be run once per frame, no matter how many times it was scheduled in the previous frame.
/// </summary>
/// <remarks>The task will be run on the next <see cref="Update"/> independent of the current clock time.</remarks>
/// <param name="task">The work to be done.</param>
/// <param name="task">The work to be done. Avoid using inline delegates as they may not be cached, bypassing the once-per-frame guarantee.</param>
/// <returns>Whether this is the first queue attempt of this work.</returns>
public bool AddOnce(Action task)
{
Expand Down
Loading