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

Free resources in a try/finally #72961

Merged
merged 1 commit into from
Apr 10, 2024
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
83 changes: 44 additions & 39 deletions src/Workspaces/Remote/ServiceHub/Host/AssetProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,57 +205,62 @@ public async ValueTask SynchronizeAssetsAsync<T, TArg>(
var usePool = missingChecksumsCount <= PooledChecksumArraySize;
var missingChecksums = usePool ? s_checksumPool.Allocate() : new Checksum[missingChecksumsCount];

missingChecksumsCount = 0;
foreach (var checksum in checksums)
try
Copy link
Member Author

Choose a reason for hiding this comment

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

view with whitespace off.

Copy link
Member

Choose a reason for hiding this comment

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

💭 Seems like the pool could just be removed in favor of stackalloc for the initial array.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, nevermind due to callbacks

Copy link
Member Author

Choose a reason for hiding this comment

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

and async/await :(

{
if (_assetCache.TryGetAsset<T>(checksum, out var existing))
{
callback?.Invoke(checksum, existing, arg!);
}
else
missingChecksumsCount = 0;
foreach (var checksum in checksums)
{
if (missingChecksumsCount == missingChecksums.Length)
if (_assetCache.TryGetAsset<T>(checksum, out var existing))
{
// This can happen if the asset cache has been modified by another thread during this method's execution.
var newMissingChecksums = new Checksum[missingChecksumsCount * 2];
Array.Copy(missingChecksums, newMissingChecksums, missingChecksumsCount);

if (usePool)
callback?.Invoke(checksum, existing, arg!);
}
else
{
if (missingChecksumsCount == missingChecksums.Length)
{
s_checksumPool.Free(missingChecksums);
usePool = false;
// This can happen if the asset cache has been modified by another thread during this method's execution.
var newMissingChecksums = new Checksum[missingChecksumsCount * 2];
Array.Copy(missingChecksums, newMissingChecksums, missingChecksumsCount);

if (usePool)
{
s_checksumPool.Free(missingChecksums);
usePool = false;
}

missingChecksums = newMissingChecksums;
}

missingChecksums = newMissingChecksums;
missingChecksums[missingChecksumsCount] = checksum;
missingChecksumsCount++;
}
}

missingChecksums[missingChecksumsCount] = checksum;
missingChecksumsCount++;
if (missingChecksumsCount > 0)
{
var missingChecksumsMemory = new ReadOnlyMemory<Checksum>(missingChecksums, 0, missingChecksumsCount);

await RequestAssetsAsync(
assetPath, missingChecksumsMemory,
static (
int index,
T missingAsset,
(AssetProvider assetProvider, Checksum[] missingChecksums, Action<Checksum, T, TArg>? callback, TArg? arg) tuple) =>
{
var missingChecksum = tuple.missingChecksums[index];

tuple.callback?.Invoke(missingChecksum, missingAsset, tuple.arg!);
tuple.assetProvider._assetCache.GetOrAdd(missingChecksum, missingAsset!);
},
(this, missingChecksums, callback, arg),
cancellationToken).ConfigureAwait(false);
}
}

if (missingChecksumsCount > 0)
finally
{
var missingChecksumsMemory = new ReadOnlyMemory<Checksum>(missingChecksums, 0, missingChecksumsCount);

await RequestAssetsAsync(
assetPath, missingChecksumsMemory,
static (
int index,
T missingAsset,
(AssetProvider assetProvider, Checksum[] missingChecksums, Action<Checksum, T, TArg>? callback, TArg? arg) tuple) =>
{
var missingChecksum = tuple.missingChecksums[index];

tuple.callback?.Invoke(missingChecksum, missingAsset, tuple.arg!);
tuple.assetProvider._assetCache.GetOrAdd(missingChecksum, missingAsset!);
},
(this, missingChecksums, callback, arg),
cancellationToken).ConfigureAwait(false);
if (usePool)
s_checksumPool.Free(missingChecksums);
}

if (usePool)
s_checksumPool.Free(missingChecksums);
}

return;
Expand Down
Loading