Skip to content

Commit

Permalink
Avoid creating another dictionary when writing named styles to styles…
Browse files Browse the repository at this point in the history
….xml
  • Loading branch information
sveinungf committed Aug 11, 2024
1 parent 8fa685f commit aca72a3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
43 changes: 13 additions & 30 deletions SpreadCheetah/MetadataXml/Styles/StylesXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ public static async ValueTask WriteAsync(
await using (stream.ConfigureAwait(false))
#endif
{
var orderedStyles = styleManager.StyleElements;
var embeddedNamedStyles = styleManager.GetEmbeddedNamedStyles();
var writer = new StylesXml(orderedStyles, embeddedNamedStyles, buffer);
var writer = new StylesXml(
styles: styleManager.StyleElements,
namedStyles: styleManager.GetEmbeddedNamedStyles(),
namedStylesDictionary: styleManager.NamedStyles,
buffer: buffer);

foreach (var success in writer)
{
Expand All @@ -46,7 +48,7 @@ public static async ValueTask WriteAsync(
private readonly List<StyleElement> _styles;
private readonly List<(string, ImmutableStyle, StyleNameVisibility)>? _namedStyles;
private readonly Dictionary<string, int>? _customNumberFormats;
private readonly Dictionary<string, int>? _embeddedNamedStyleIndexes;
private readonly Dictionary<string, (StyleId StyleId, int NamedStyleIndex)>? _namedStylesDictionary;
private readonly Dictionary<ImmutableBorder, int> _borders;
private readonly Dictionary<ImmutableFill, int> _fills;
private readonly Dictionary<ImmutableFont, int> _fonts;
Expand All @@ -62,10 +64,10 @@ public static async ValueTask WriteAsync(
private StylesXml(
List<StyleElement> styles,
List<(string, ImmutableStyle, StyleNameVisibility)>? namedStyles,
Dictionary<string, (StyleId StyleId, int NamedStyleIndex)>? namedStylesDictionary,
SpreadsheetBuffer buffer)
{
_customNumberFormats = CreateCustomNumberFormatDictionary(styles);
_embeddedNamedStyleIndexes = CreateEmbeddedStyleNameIndexesDictionary(styles, namedStyles); // TODO: Maybe not needed anymore?
_borders = CreateBorderDictionary(styles);
_fills = CreateFillDictionary(styles);
_fonts = CreateFontDictionary(styles);
Expand All @@ -77,6 +79,7 @@ private StylesXml(
_cellStylesXml = new CellStylesXmlPart(namedStyles, buffer);
_styles = styles;
_namedStyles = namedStyles;
_namedStylesDictionary = namedStylesDictionary;
}

public readonly StylesXml GetEnumerator() => this;
Expand Down Expand Up @@ -152,27 +155,6 @@ private static Dictionary<ImmutableFont, int> CreateFontDictionary(List<StyleEle
return uniqueFonts;
}

private static Dictionary<string, int>? CreateEmbeddedStyleNameIndexesDictionary(
List<StyleElement> styles,
List<(string, ImmutableStyle, StyleNameVisibility)>? namedStyles)
{
if (namedStyles is null)
return null;

Dictionary<string, int>? result = null;

foreach (var (_, name, _) in styles)
{
if (name is null)
continue;

result ??= new(StringComparer.OrdinalIgnoreCase);
result[name] = namedStyles.FindIndex(x => name.Equals(x.Item1, StringComparison.OrdinalIgnoreCase));
}

return result;
}

public bool MoveNext()
{
Current = _next switch
Expand Down Expand Up @@ -239,12 +221,13 @@ private bool TryWriteCellXfsEntries()

for (; _nextIndex < styles.Count; ++_nextIndex)
{
var (style, embeddedName, _) = _styles[_nextIndex];
var embeddedStyleIndex = embeddedName is not null
? _embeddedNamedStyleIndexes?.GetValueOrDefault(embeddedName)
var (style, name, visibility) = _styles[_nextIndex];
int? embeddedNamedStyleIndex = name is not null && visibility is not null
&& _namedStylesDictionary is { } namedStyles && namedStyles.TryGetValue(name, out var value)
? value.NamedStyleIndex
: null;

if (!xfXml.TryWrite(style, embeddedStyleIndex))
if (!xfXml.TryWrite(style, embeddedNamedStyleIndex))
return false;
}

Expand Down
17 changes: 11 additions & 6 deletions SpreadCheetah/Styling/Internal/StyleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ internal sealed class StyleManager
{
private readonly NumberFormat? _defaultDateTimeFormat;
private readonly Dictionary<ImmutableStyle, int> _styleDictionary = [];
private Dictionary<string, StyleId>? _namedStyleDictionary;

public List<StyleElement> StyleElements { get; } = [];
public DefaultStyling? DefaultStyling { get; }
public List<StyleElement> StyleElements { get; } = [];
public Dictionary<string, (StyleId StyleId, int NamedStyleIndex)>? NamedStyles { get; private set; }

public StyleManager(NumberFormat? defaultDateTimeFormat)
{
Expand Down Expand Up @@ -54,7 +54,7 @@ public bool TryAddNamedStyle(string name, Style style, StyleNameVisibility? visi
{
styleId = null;

var namedStyles = _namedStyleDictionary ??= new(StringComparer.OrdinalIgnoreCase);
var namedStyles = NamedStyles ??= new(StringComparer.OrdinalIgnoreCase);
if (namedStyles.ContainsKey(name))
return false;

Expand All @@ -74,15 +74,20 @@ public bool TryAddNamedStyle(string name, Style style, StyleNameVisibility? visi
}

styleId = new StyleId(id, dateTimeId);
namedStyles[name] = styleId;
namedStyles[name] = (styleId, namedStyles.Count);
return true;
}

public StyleId? GetStyleIdOrDefault(string name) => _namedStyleDictionary?.GetValueOrDefault(name);
public StyleId? GetStyleIdOrDefault(string name)
{
return NamedStyles is { } namedStyles && namedStyles.TryGetValue(name, out var value)
? value.StyleId
: null;
}

public List<(string, ImmutableStyle, StyleNameVisibility)>? GetEmbeddedNamedStyles()
{
if (_namedStyleDictionary is null)
if (NamedStyles is null)
return null;

List<(string, ImmutableStyle, StyleNameVisibility)>? result = null;
Expand Down

0 comments on commit aca72a3

Please sign in to comment.