Skip to content

Remove call to SetLength when copying ReadOnlyMemory<byte> to a MemoryMapped file - #11796

Merged
TIHan merged 1 commit into
dotnet:mainfrom
TIHan:remove-set-length
Jul 8, 2021
Merged

Remove call to SetLength when copying ReadOnlyMemory<byte> to a MemoryMapped file#11796
TIHan merged 1 commit into
dotnet:mainfrom
TIHan:remove-set-length

Conversation

@TIHan

@TIHanTIHan commented Jul 7, 2021

Copy link
Copy Markdown
Contributor

@vzarytovskii correctly pointed out that we can't set the length of a memory mapped file. It's already fixed length so it should be safe to not do it.

stream.SetLength(stream.Length + length)
let span = Span<byte>(stream.PositionPointer |> NativePtr.toVoidPtr, int length)
bytes.Span.CopyTo(span)
stream.Position <- stream.Position + length

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we still need to set the position here too?

@TIHanTIHanJul 8, 2021

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Yea as TryFromByteMemory version does not reset the stream's position once it's copied.

@TIHan
TIHan merged commit 5148e8d into dotnet:mainJul 8, 2021
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@TIHan@vzarytovskii