Skip to content

Commit

Permalink
Save byte array allocations (#15630)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros authored Apr 2, 2024
1 parent 52ec367 commit aa0b8c3
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.IO.Hashing;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -204,7 +204,7 @@ private static string CalculateHash(params string[] parts)

foreach (var part in parts)
{
hash.Append(Encoding.UTF8.GetBytes(part));
hash.Append(MemoryMarshal.AsBytes<char>(part));
}

return Convert.ToHexString(hash.GetCurrentHash());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace OrchardCore.Queries.Sql.ViewModels
{
public class AdminQueryViewModel
{
public static string MatchAllQueryBase64 { get; } = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(@"SELECT * FROM ContentItemIndex"));

public string DecodedQuery { get; set; }
public string Parameters { get; set; } = "";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@inject IContentManager ContentManager

@{
var matchAllQuery = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(@"SELECT * FROM ContentItemIndex"));
var matchAllQuery = OrchardCore.Queries.Sql.ViewModels.AdminQueryViewModel.MatchAllQueryBase64;
}

<style asp-name="codemirror"></style>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
Expand Down Expand Up @@ -90,16 +88,14 @@ public static JsonObject ToJsonObject(this IDictionary<string, string> configura
return configuration.ToJsonObject();
}

public static async Task<IDictionary<string, string>> ToConfigurationDataAsync(this JsonObject jConfiguration)
public static Task<IDictionary<string, string>> ToConfigurationDataAsync(this JsonObject jConfiguration)
{
if (jConfiguration is null)
{
return new Dictionary<string, string>();
return Task.FromResult< IDictionary<string, string>>(new Dictionary<string, string>());
}

var configurationString = jConfiguration.ToJsonString(JOptions.Default);
using var ms = new MemoryStream(Encoding.UTF8.GetBytes(configurationString));

return await JsonConfigurationParser.ParseAsync(ms);
return Task.FromResult(JsonConfigurationParser.Parse(configurationString));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,42 @@ private JsonConfigurationParser() { }
private readonly Dictionary<string, string?> _data = new(StringComparer.OrdinalIgnoreCase);
private readonly Stack<string> _paths = new();

public static IDictionary<string, string?> Parse(Stream input)
=> new JsonConfigurationParser().ParseStream(input);
public static IDictionary<string, string?> Parse(Stream utf8Json)
=> new JsonConfigurationParser().ParseStream(utf8Json);

public static Task<IDictionary<string, string?>> ParseAsync(Stream input)
=> new JsonConfigurationParser().ParseStreamAsync(input);
public static IDictionary<string, string?> Parse(string document)
=> new JsonConfigurationParser().ParseDocument(document);

private Dictionary<string, string?> ParseStream(Stream input)
public static Task<IDictionary<string, string?>> ParseAsync(Stream utf8Json)
=> new JsonConfigurationParser().ParseStreamAsync(utf8Json);

private Dictionary<string, string?> ParseStream(Stream utf8Json)
{
var jsonDocumentOptions = new JsonDocumentOptions
try
{
CommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true,
};
using (var doc = JsonDocument.Parse(utf8Json, JOptions.Document))
{
if (doc.RootElement.ValueKind != JsonValueKind.Object)
{
throw new FormatException($"Top-level JSON element must be an object. Instead, '{doc.RootElement.ValueKind}' was found.");
}

VisitObjectElement(doc.RootElement);
}

return _data;
}
catch (JsonException e)
{
throw new FormatException("Could not parse the JSON document.", e);
}
}

private Dictionary<string, string?> ParseDocument(string document)
{
try
{
using (var reader = new StreamReader(input))
using (var doc = JsonDocument.Parse(reader.ReadToEnd(), jsonDocumentOptions))
using (var doc = JsonDocument.Parse(document, JOptions.Document))
{
if (doc.RootElement.ValueKind != JsonValueKind.Object)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace OrchardCore.FileStorage.AzureBlob
/// </remarks>
public class BlobFileStore : IFileStore
{
private static readonly byte[] MarkerFileContent = "This is a directory marker file used by Orchard Core."u8.ToArray();
private const string DirectoryMarkerFileName = "OrchardCore.Media.txt";

private readonly BlobStorageOptions _options;
Expand Down Expand Up @@ -439,7 +440,7 @@ private async Task CreateDirectoryAsync(string path)
var placeholderBlob = GetBlobReference(this.Combine(path, DirectoryMarkerFileName));

// Create a directory marker file to make this directory appear when listing directories.
using var stream = new MemoryStream(Encoding.UTF8.GetBytes("This is a directory marker file used by Orchard Core."));
using var stream = new MemoryStream(MarkerFileContent);
await placeholderBlob.UploadAsync(stream);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,8 @@ public DefaultDocumentSerializer()

private static readonly byte[] _gZipHeaderBytes = [0x1f, 0x8b];

internal static bool IsCompressed(byte[] data)
{
if (data.Length < _gZipHeaderBytes.Length)
{
return false;
}

for (var i = 0; i < _gZipHeaderBytes.Length; i++)
{
if (data[i] != _gZipHeaderBytes[i])
{
return false;
}
}

return true;
}
internal static bool IsCompressed(byte[] data) =>
data.Length < _gZipHeaderBytes.Length && data[0..1] == _gZipHeaderBytes;

internal static byte[] Compress(byte[] data)
{
Expand All @@ -70,6 +55,11 @@ internal static byte[] Compress(byte[] data)
input.CopyTo(gzip);
}

if (output.TryGetBuffer(out var buffer))
{
return buffer.Array;
}

return output.ToArray();
}

Expand All @@ -82,6 +72,11 @@ internal static byte[] Decompress(byte[] data)
gzip.CopyTo(output);
}

if (output.TryGetBuffer(out var buffer))
{
return buffer.Array;
}

return output.ToArray();
}
}
Expand Down

0 comments on commit aa0b8c3

Please sign in to comment.