Skip to content

Commit

Permalink
Use FrozenDictionary for better performance (thanks Graham!)
Browse files Browse the repository at this point in the history
  • Loading branch information
onizet committed Dec 16, 2024
1 parent 89f4e5f commit e62d833
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion examples/Benchmark/Benchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async Task ParseWithSpan()
using (MemoryStream generatedDocument = new MemoryStream())
using (WordprocessingDocument package = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = package.MainDocumentPart;
MainDocumentPart? mainPart = package.MainDocumentPart;
if (mainPart == null)
{
mainPart = package.AddMainDocumentPart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
* PARTICULAR PURPOSE.
*/
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Linq;
using AngleSharp.Html.Dom;
Expand All @@ -26,7 +27,7 @@ abstract class NumberingExpressionBase(IHtmlElement node) : BlockElementExpressi
public const int MaxLevel = 8;
protected const int Indentation = 360;
public const string HeadingNumberingName = "decimal-heading-multi";
private static readonly IDictionary<string, AbstractNum> predefinedNumberingLists = InitKnownLists();
private static readonly IReadOnlyDictionary<string, AbstractNum> predefinedNumberingLists = InitKnownLists();
/// <summary>Contains the list of templated list along with the AbstractNumbId</summary>
private Dictionary<string, int>? knownAbsNumIds;
/// <summary>Contains the list of numbering instance.</summary>
Expand Down Expand Up @@ -218,7 +219,7 @@ private void InitNumberingIds(ParsingContext context)
/// <summary>
/// Predefined template of lists.
/// </summary>
private static Dictionary<string, AbstractNum> InitKnownLists()
private static IReadOnlyDictionary<string, AbstractNum> InitKnownLists()
{
var knownAbstractNums = new Dictionary<string, AbstractNum>();

Expand Down Expand Up @@ -292,6 +293,6 @@ private static Dictionary<string, AbstractNum> InitKnownLists()
knownAbstractNums.Add(listName, abstractNum);
}

return knownAbstractNums;
return knownAbstractNums.ToFrozenDictionary();
}
}
5 changes: 5 additions & 0 deletions src/Html2OpenXml/HtmlToOpenXml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
<Reference Include="System.Net.Http" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="System.Collections.Immutable" Version="9.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Collections.Immutable" Version="9.0.0" />
</ItemGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
Expand Down
25 changes: 16 additions & 9 deletions src/Html2OpenXml/IO/ImageHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* EMF Specifications: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-emf/ae7e7437-cfe5-485e-84ea-c74b51b000be
*/
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -35,16 +36,22 @@ public enum FileType { Unrecognized, Bitmap, Gif, Png, Jpeg, Emf, Xml }

private static readonly byte[] pngSignatureBytes = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];

private static readonly Dictionary<byte[], FileType> imageFormatDecoders = new()
private static readonly IReadOnlyDictionary<byte[], FileType> imageFormatDecoders = InitKnownFormatDecoders();

private static IReadOnlyDictionary<byte[], FileType> InitKnownFormatDecoders()
{
{ new byte[] { 0x42, 0x4D }, FileType.Bitmap },
{ Encoding.UTF8.GetBytes("GIF87a"), FileType.Gif },
{ Encoding.UTF8.GetBytes("GIF89a"), FileType.Gif }, // animated gif
{ pngSignatureBytes, FileType.Png },
{ new byte[] { 0xff, 0xd8 }, FileType.Jpeg },
{ new byte[] { 0x1, 0, 0, 0 }, FileType.Emf },
{ Encoding.UTF8.GetBytes("<?xml "), FileType.Xml }, // Xml so potentially Svg
};
var decoders = new Dictionary<byte[], FileType>() {
{ new byte[] { 0x42, 0x4D }, FileType.Bitmap },
{ Encoding.UTF8.GetBytes("GIF87a"), FileType.Gif },
{ Encoding.UTF8.GetBytes("GIF89a"), FileType.Gif }, // animated gif
{ pngSignatureBytes, FileType.Png },
{ new byte[] { 0xff, 0xd8 }, FileType.Jpeg },
{ new byte[] { 0x1, 0, 0, 0 }, FileType.Emf },
{ Encoding.UTF8.GetBytes("<?xml "), FileType.Xml }, // Xml so potentially Svg
};

return decoders.ToFrozenDictionary();
}

private static readonly int MaxMagicBytesLength = imageFormatDecoders
.Keys.OrderByDescending(x => x.Length).First().Length;
Expand Down
7 changes: 4 additions & 3 deletions src/Html2OpenXml/Primitives/HtmlColor.Named.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Frozen;
using System.Collections.Generic;

namespace HtmlToOpenXml;
Expand All @@ -8,7 +9,7 @@ namespace HtmlToOpenXml;
/// </summary>
partial struct HtmlColor
{
private static readonly Dictionary<string, HtmlColor> namedColors = InitKnownColors();
private static readonly IReadOnlyDictionary<string, HtmlColor> namedColors = InitKnownColors();

private static HtmlColor GetNamedColor (ReadOnlySpan<char> name)
{
Expand All @@ -21,7 +22,7 @@ private static HtmlColor GetNamedColor (ReadOnlySpan<char> name)
return color;
}

private static Dictionary<string, HtmlColor> InitKnownColors()
private static IReadOnlyDictionary<string, HtmlColor> InitKnownColors()
{
var colors = new Dictionary<string, HtmlColor>()
{
Expand Down Expand Up @@ -168,6 +169,6 @@ private static Dictionary<string, HtmlColor> InitKnownColors()
{ "transparent", FromArgb(0, 0, 0, 0) }
};

return colors;
return colors.ToFrozenDictionary();
}
}

0 comments on commit e62d833

Please sign in to comment.