Skip to content

Commit

Permalink
Revert "Performance: Fix MemoryStream with un-accesible buffer (#1515)"
Browse files Browse the repository at this point in the history
This reverts commit 01e39d4.
  • Loading branch information
Jake Willey committed May 14, 2020
1 parent 6297647 commit e47b600
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 25 deletions.
5 changes: 2 additions & 3 deletions Microsoft.Azure.Cosmos/src/FeedIteratorCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,11 @@ 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, writable: false, publiclyVisible: true);
rewrittenMemoryStream = new MemoryStream(rewrittenSegment.Array, index: rewrittenSegment.Offset, count: rewrittenSegment.Count);
}
else
{
byte[] toArray = result.ToArray();
rewrittenMemoryStream = new MemoryStream(toArray, index: 0, count: toArray.Length, writable: false, publiclyVisible: true);
rewrittenMemoryStream = new MemoryStream(result.ToArray());
}

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

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

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

internal static IEnumerable<T> GetResources<T>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,6 @@ 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 e47b600

Please sign in to comment.