Skip to content

Move memory-mapped-text storage system to use the new pattern introduced for memory-mapped-stream storage.#73218

Merged
CyrusNajmabadi merged 12 commits intodotnet:mainfrom
CyrusNajmabadi:textStorage2
Apr 25, 2024
Merged

Move memory-mapped-text storage system to use the new pattern introduced for memory-mapped-stream storage.#73218
CyrusNajmabadi merged 12 commits intodotnet:mainfrom
CyrusNajmabadi:textStorage2

Conversation

@CyrusNajmabadi
Copy link
Contributor

Updates to follow the patterns here: #73115

Followup to: #73216

@ghost ghost added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Apr 24, 2024

internal sealed partial class TemporaryStorageService
{
private unsafe class DirectMemoryAccessStreamReader : TextReaderWithLength
Copy link
Contributor Author

@CyrusNajmabadi CyrusNajmabadi Apr 24, 2024

Choose a reason for hiding this comment

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

Just a move into its own file. this didn't change at all (besides becoming nested and private).

/// <summary>
/// TemporaryStorage can be used to read and write text to a temporary storage location.
/// </summary>
internal interface ITemporaryTextStorageInternal
Copy link
Contributor Author

Choose a reason for hiding this comment

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

begone!

/// <summary>
/// TemporaryStorage can be used to read and write text to a temporary storage location.
/// </summary>
internal interface ITemporaryStorageWithName
Copy link
Contributor Author

Choose a reason for hiding this comment

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

begone!

/// The <see cref="SourceText"/> in the current process.
/// </summary>
/// <remarks>
/// <inheritdoc cref="Storage"/>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was referencing the namespace... whoopsie :)

var identifier = new TemporaryStorageIdentifier(memoryMappedInfo.Name, memoryMappedInfo.Offset, memoryMappedInfo.Size);
return new(this, memoryMappedInfo.MemoryMappedFile, identifier, text.ChecksumAlgorithm, text.Encoding, text.GetContentHash());

MemoryMappedInfo WriteToMemoryMappedFile()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this code was inlined from the old TemporaryTextStorage type (which was deleted).

var identifier = new TemporaryStorageIdentifier(memoryMappedInfo.Name, memoryMappedInfo.Offset, memoryMappedInfo.Size);
return new(memoryMappedInfo.MemoryMappedFile, identifier);

MemoryMappedInfo WriteToMemoryMappedFile()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this code was inlined from TemporaryStreamStorage (which was itself deleted).

}
}

internal unsafe class DirectMemoryAccessStreamReader : TextReaderWithLength
Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved to new file.

}
}

internal sealed class TemporaryStreamStorage
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this abstraction just wasnt' necessary. i inlined it all into the one caller.

@CyrusNajmabadi CyrusNajmabadi marked this pull request as ready for review April 24, 2024 20:04
@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner April 24, 2024 20:04

var info = new MemoryMappedInfo(memoryMappedFile, Identifier.Name, Identifier.Offset, Identifier.Size);
return info.CreateReadableStream();
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is just the old storage.ReadStream method inlined (so we could delete TemporaryStreamStorage).

{
await Task.Yield().ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

existing logic. just inlined.

@CyrusNajmabadi CyrusNajmabadi requested a review from ToddGrun April 24, 2024 20:32
Task WriteStreamAsync(Stream stream, CancellationToken cancellationToken = default);
}

/// <summary>
Copy link
Contributor

Choose a reason for hiding this comment

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

Have the interfaces and their implementations above been obsoleted long enough for removal?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have to make then Obsolete(error: true) first. i can do that in a followup.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

opened: #73226

@CyrusNajmabadi CyrusNajmabadi requested a review from ToddGrun April 25, 2024 00:05

namespace Microsoft.CodeAnalysis.Host;

internal interface ITemporaryStorageTextHandle
Copy link
Contributor

Choose a reason for hiding this comment

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

ITemporaryStorageTextHandle

nit: would it be useful for ITemporaryStorageTextHandle and ITemporaryStorageStreamHandle to derive from a common interface?

Copy link
Contributor

Choose a reason for hiding this comment

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

It almost seems like there could just be a single

ITemporaryStorageHandle interface that returned TStorage from the Read* methods

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe? I just don't see it buying much . This isn't a system we've needed to generalize. It handles byte streams and encoded strings effectively. I'm fine keeping it specific to those two needs.

{
// Return a read-only view of the underlying buffer to prevent users from overwriting or directly
// disposing the backing storage.
return new MemoryStream(streamCopy.GetBuffer(), 0, (int)streamCopy.Length, writable: false);
Copy link
Contributor

Choose a reason for hiding this comment

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

new MemoryStream

Does this method not get called on many of the TrivialStorageStreamHandle instance created? If it's called on most/all of them, it seems like this could just be stored in the field.

If the earlier comment about combining the interfaces into ITemporaryStorageHandle were used, and if the caller would just pass in an appopriate read only stream, then the stream/text versions of these classes could be combined too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm preserving the behavior from before. I'm genuinely not sure if we have a single scenario exercising this code in the real world, so I'd prefer to leave the semantics as is for safety.

=> "Roslyn Temp Storage " + size.ToString() + " " + Guid.NewGuid().ToString("N");

public sealed class TemporaryTextStorage : ITemporaryTextStorageInternal, ITemporaryStorageWithName
public sealed class TemporaryStorageTextHandle(
Copy link
Contributor

Choose a reason for hiding this comment

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

TemporaryStorageTextHandle

nit: consider moving to it's own file

public override int Peek()
{
if (_position >= _end)
{
Copy link
Contributor

Choose a reason for hiding this comment

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

{

nit: might as well format the document as part of the file creation

@CyrusNajmabadi CyrusNajmabadi requested a review from ToddGrun April 25, 2024 15:17
Copy link
Contributor

@ToddGrun ToddGrun left a comment

Choose a reason for hiding this comment

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

:shipit:

@CyrusNajmabadi CyrusNajmabadi merged commit f60d444 into dotnet:main Apr 25, 2024
@CyrusNajmabadi CyrusNajmabadi deleted the textStorage2 branch April 25, 2024 16:37
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Apr 25, 2024
@CyrusNajmabadi
Copy link
Contributor Author

@jasonmalinowski for review when you get back.

@dibarbet dibarbet modified the milestones: Next, 17.11 P1 Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants