Skip to content

Commit

Permalink
Performance: Fix MemoryStream with un-accesible buffer (#1515)
Browse files Browse the repository at this point in the history
* memorystream with buffer

* test
  • Loading branch information
ealsur authored May 14, 2020
1 parent 07ae4ce commit 01e39d4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
5 changes: 3 additions & 2 deletions Microsoft.Azure.Cosmos/src/FeedIteratorCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,12 @@ private async Task<ResponseMessage> ReadNextInternalAsync(
MemoryStream rewrittenMemoryStream;
if (MemoryMarshal.TryGetArray(result, out ArraySegment<byte> rewrittenSegment))
{
rewrittenMemoryStream = new MemoryStream(rewrittenSegment.Array, index: rewrittenSegment.Offset, count: rewrittenSegment.Count);
rewrittenMemoryStream = new MemoryStream(rewrittenSegment.Array, index: rewrittenSegment.Offset, count: rewrittenSegment.Count, writable: false, publiclyVisible: true);
}
else
{
rewrittenMemoryStream = new MemoryStream(result.ToArray());
byte[] toArray = result.ToArray();
rewrittenMemoryStream = new MemoryStream(toArray, index: 0, count: toArray.Length, writable: false, publiclyVisible: true);
}

responseMessage.Content = rewrittenMemoryStream;
Expand Down
5 changes: 3 additions & 2 deletions Microsoft.Azure.Cosmos/src/FeedRangeIteratorCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,12 @@ private async Task<ResponseMessage> ReadNextInternalAsync(
MemoryStream rewrittenMemoryStream;
if (MemoryMarshal.TryGetArray(result, out ArraySegment<byte> rewrittenSegment))
{
rewrittenMemoryStream = new MemoryStream(rewrittenSegment.Array, index: rewrittenSegment.Offset, count: rewrittenSegment.Count);
rewrittenMemoryStream = new MemoryStream(rewrittenSegment.Array, index: rewrittenSegment.Offset, count: rewrittenSegment.Count, writable: false, publiclyVisible: true);
}
else
{
rewrittenMemoryStream = new MemoryStream(result.ToArray());
byte[] toArray = result.ToArray();
rewrittenMemoryStream = new MemoryStream(toArray, index: 0, count: toArray.Length, writable: false, publiclyVisible: true);
}

responseMessage.Content = rewrittenMemoryStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ internal static MemoryStream ToStream(
resultAsArray = new ArraySegment<byte>(result.ToArray());
}

return new MemoryStream(resultAsArray.Array, resultAsArray.Offset, resultAsArray.Count);
return new MemoryStream(resultAsArray.Array, resultAsArray.Offset, resultAsArray.Count, writable: false, publiclyVisible: true);
}

internal static IEnumerable<T> GetResources<T>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ public async Task MemoryStreamBufferIsAccessibleOnResponse()
ResponseMessage response = await this.Container.CreateItemStreamAsync(streamPayload: TestCommon.SerializerCore.ToStream(testItem), partitionKey: new Cosmos.PartitionKey(testItem.status));
Assert.IsNotNull(response);
Assert.IsTrue((response.Content as MemoryStream).TryGetBuffer(out _));
FeedIterator feedIteratorQuery = this.Container.GetItemQueryStreamIterator(queryText: "SELECT * FROM c");

while (feedIteratorQuery.HasMoreResults)
{
ResponseMessage feedResponseQuery = await feedIteratorQuery.ReadNextAsync();
Assert.IsTrue((feedResponseQuery.Content as MemoryStream).TryGetBuffer(out _));
}

FeedIterator feedIterator = this.Container.GetItemQueryStreamIterator(requestOptions: new QueryRequestOptions()
{
PartitionKey = new Cosmos.PartitionKey(testItem.status)
});

while (feedIterator.HasMoreResults)
{
ResponseMessage feedResponse = await feedIterator.ReadNextAsync();
Assert.IsTrue((feedResponse.Content as MemoryStream).TryGetBuffer(out _));
}
}

[TestMethod]
Expand Down

0 comments on commit 01e39d4

Please sign in to comment.