mirror of
https://github.com/ppy/osu.git
synced 2025-03-05 13:13:22 +08:00
Fix blocking overhead when calling WriteAsync
This commit is contained in:
parent
7d988da2c7
commit
f74b4ac277
@ -385,11 +385,22 @@ namespace osu.Game.Database
|
|||||||
/// Write changes to realm asynchronously, guaranteeing order of execution.
|
/// Write changes to realm asynchronously, guaranteeing order of execution.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="action">The work to run.</param>
|
/// <param name="action">The work to run.</param>
|
||||||
public async Task WriteAsync(Action<Realm> action)
|
public Task WriteAsync(Action<Realm> action)
|
||||||
{
|
{
|
||||||
total_writes_async.Value++;
|
// Regardless of calling Realm.GetInstance of Realm.GetInstanceAsync, there is a blocking overhead on retrieval.
|
||||||
using (var realm = getRealmInstance())
|
// Adding a forced Task.Run resolves this.
|
||||||
await realm.WriteAsync(() => action(realm));
|
|
||||||
|
return Task.Run(async () =>
|
||||||
|
{
|
||||||
|
total_writes_async.Value++;
|
||||||
|
|
||||||
|
// Not attempting to use Realm.GetInstanceAsync as there's seemingly no benefit to us (for now) and it adds complexity due to locking
|
||||||
|
// concerns in getRealmInstance(). On a quick check, it looks to be more suited to cases where realm is connecting to an online sync
|
||||||
|
// server, which we don't use. May want to report upstream or revisit in the future.
|
||||||
|
using (var realm = getRealmInstance())
|
||||||
|
// ReSharper disable once AccessToDisposedClosure (WriteAsync should be marked as [InstantHandle]).
|
||||||
|
await realm.WriteAsync(() => action(realm));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
Loading…
Reference in New Issue
Block a user