-
Notifications
You must be signed in to change notification settings - Fork 568
Use structs for element factory #1844
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,29 +4,12 @@ | |
using System; | ||
using System.Diagnostics; | ||
|
||
namespace DocumentFormat.OpenXml.Framework.Metadata | ||
{ | ||
[DebuggerDisplay("{QName,nq}")] | ||
internal sealed class ElementFactory | ||
{ | ||
private readonly Func<OpenXmlElement> _factory; | ||
|
||
public ElementFactory(in OpenXmlSchemaType type, Func<OpenXmlElement> factory) | ||
{ | ||
Type = type; | ||
_factory = factory; | ||
} | ||
|
||
public OpenXmlSchemaType Type { get; } | ||
namespace DocumentFormat.OpenXml.Framework.Metadata; | ||
|
||
public OpenXmlElement Create() => _factory(); | ||
|
||
public static ElementFactory Create<T>() | ||
where T : OpenXmlElement, new() | ||
{ | ||
var instance = new T(); | ||
[DebuggerDisplay("{QName,nq}")] | ||
internal readonly struct ElementFactory(OpenXmlSchemaType type, Func<OpenXmlElement> factory) | ||
{ | ||
Comment on lines
+10
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing ElementFactory from a class to a struct introduces value semantics that can lead to unintended copying; please verify that all consumers handle these semantics correctly and that removing the generic Create() method is intentional.
Suggested change
internal readonly struct ElementFactory(OpenXmlSchemaType type, Func<OpenXmlElement> factory)
{
internal class ElementFactory
{
private readonly OpenXmlSchemaType type;
private readonly Func<OpenXmlElement> factory;
public ElementFactory(OpenXmlSchemaType type, Func<OpenXmlElement> factory)
{
this.type = type;
this.factory = factory;
}
Copilot uses AI. Check for mistakes. |
||
public OpenXmlSchemaType Type => type; | ||
|
||
return new ElementFactory(instance.Metadata.Type, static () => new T()); | ||
} | ||
} | ||
public OpenXmlElement Create() => factory(); | ||
} |
92 changes: 30 additions & 62 deletions
src/DocumentFormat.OpenXml.Framework/Framework/Metadata/ElementFactoryCollection.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,49 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace DocumentFormat.OpenXml.Framework.Metadata | ||
namespace DocumentFormat.OpenXml.Framework.Metadata; | ||
|
||
/// <summary> | ||
/// A lookup that identifies properties on an <see cref="OpenXmlElement"/> and caches the schema information | ||
/// from those elements. | ||
/// </summary> | ||
internal class ElementFactoryCollection | ||
{ | ||
/// <summary> | ||
/// A lookup that identifies properties on an <see cref="OpenXmlElement"/> and caches the schema information | ||
/// from those elements. | ||
/// </summary> | ||
internal class ElementFactoryCollection | ||
{ | ||
public static readonly ElementFactoryCollection Empty = new(Enumerable.Empty<ElementFactory>()); | ||
public static readonly ElementFactoryCollection Empty = new([]); | ||
|
||
private readonly ElementFactory[] _data; | ||
private readonly List<ElementFactory> _data; | ||
|
||
public ElementFactoryCollection(IEnumerable<ElementFactory> lookup) | ||
{ | ||
var array = lookup.ToArray(); | ||
|
||
Array.Sort(array, ElementChildNameComparer.Instance); | ||
public ElementFactoryCollection(List<ElementFactory> lookup) | ||
{ | ||
lookup.Sort(ElementChildNameComparer.Instance); | ||
_data = lookup; | ||
} | ||
|
||
_data = array; | ||
public OpenXmlElement? Create(in OpenXmlQualifiedName qname) | ||
{ | ||
if (_data.Count == 0) | ||
{ | ||
return null; | ||
} | ||
|
||
public int Count => _data.Length; | ||
// This is on a hot-path and using a dictionary adds substantial time to the lookup. Most child lists are small, so using a sorted | ||
// list to store them with a binary search improves overall performance. | ||
var idx = _data.BinarySearch(new ElementFactory(new(qname, default), null!), ElementChildNameComparer.Instance); | ||
|
||
public IEnumerable<ElementFactory> Elements => _data; | ||
|
||
public OpenXmlElement? Create(in OpenXmlQualifiedName qname) | ||
if (idx < 0) | ||
{ | ||
if (_data.Length == 0) | ||
{ | ||
return null; | ||
} | ||
|
||
// This is on a hot-path and using a dictionary adds substantial time to the lookup. Most child lists are small, so using a sorted | ||
// list to store them with a binary search improves overall performance. | ||
var idx = Array.BinarySearch(_data, new ElementFactory(new(qname, default), null!), ElementChildNameComparer.Instance); | ||
|
||
if (idx < 0) | ||
{ | ||
return null; | ||
} | ||
|
||
return _data[idx].Create(); | ||
return null; | ||
} | ||
|
||
private class ElementChildNameComparer : IComparer<ElementFactory> | ||
{ | ||
public static IComparer<ElementFactory> Instance { get; } = new ElementChildNameComparer(); | ||
|
||
private ElementChildNameComparer() | ||
{ | ||
} | ||
|
||
public int Compare(ElementFactory? x, ElementFactory? y) | ||
{ | ||
if (x is null && y is null) | ||
{ | ||
return 0; | ||
} | ||
|
||
if (x is null) | ||
{ | ||
return -1; | ||
} | ||
return _data[idx].Create(); | ||
} | ||
|
||
if (y is null) | ||
{ | ||
return 1; | ||
} | ||
private sealed class ElementChildNameComparer : IComparer<ElementFactory> | ||
{ | ||
public static IComparer<ElementFactory> Instance { get; } = new ElementChildNameComparer(); | ||
|
||
return x.Type.Name.CompareTo(y.Type.Name); | ||
} | ||
} | ||
public int Compare(ElementFactory x, ElementFactory y) => x.Type.Name.CompareTo(y.Type.Name); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.