-
Notifications
You must be signed in to change notification settings - Fork 160
Create benchmark for Core.RemoveObjectFromList() implementation
#476
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
Draft
jozefizso
wants to merge
3
commits into
releases/netoffice_v1.9.10
Choose a base branch
from
dev/448_benchmark_RemoveObjectFromList
base: releases/netoffice_v1.9.10
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 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 |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| name: benchmarks | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| pull_request: | ||
| push: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| env: | ||
| DOTNET_NOLOGO: 1 | ||
| DOTNET_CLI_TELEMETRY_OPTOUT: 1 | ||
| DOTNET_GENERATE_ASPNET_CERTIFICATE: 0 | ||
| ContinuousIntegrationBuild: true | ||
| CONFIGURATION: Release | ||
|
|
||
| jobs: | ||
| build: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v6 | ||
|
|
||
| - name: Setup dotnet | ||
| uses: actions/setup-dotnet@v5 | ||
| with: | ||
| dotnet-version: '10.0' | ||
|
|
||
| - name: Build benchmarks | ||
| id: build | ||
| run: dotnet build ./Tests/NetOffice.Benchmarks/NetOffice.Benchmarks.csproj -c '${{ env.CONFIGURATION }}' |
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 |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Collections.Concurrent; | ||
| using System.Linq; | ||
| using NetOffice; | ||
|
|
||
| namespace NetOffice.Benchmarks | ||
| { | ||
| /// <summary> | ||
| /// Base interface for Core collection variants to enable polymorphic benchmarking | ||
| /// </summary> | ||
| internal interface ICoreCollectionVariant | ||
| { | ||
| void AddObject(ICOMObject obj); | ||
| bool RemoveObject(ICOMObject obj); | ||
| void Clear(); | ||
| int Count { get; } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Variant 1: Current implementation using List (baseline) | ||
| /// Time: O(n) per removal | ||
| /// Memory: O(n) | ||
| /// </summary> | ||
| internal class ListCoreVariant : ICoreCollectionVariant | ||
| { | ||
| private readonly List<ICOMObject> _globalObjectList = new List<ICOMObject>(); | ||
| private readonly object _lock = new object(); | ||
|
|
||
| public void AddObject(ICOMObject obj) | ||
| { | ||
| lock (_lock) | ||
| { | ||
| _globalObjectList.Add(obj); | ||
| } | ||
| } | ||
|
|
||
| public bool RemoveObject(ICOMObject obj) | ||
| { | ||
| lock (_lock) | ||
| { | ||
| return _globalObjectList.Remove(obj); | ||
| } | ||
| } | ||
|
|
||
| public void Clear() | ||
| { | ||
| lock (_lock) | ||
| { | ||
| _globalObjectList.Clear(); | ||
| } | ||
| } | ||
|
|
||
| public int Count | ||
| { | ||
| get | ||
| { | ||
| lock (_lock) | ||
| { | ||
| return _globalObjectList.Count; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Variant 2: HashSet implementation (proposed solution) | ||
| /// Time: O(1) per removal (average) | ||
| /// Memory: O(n) with higher constant factor | ||
| /// Requires proper GetHashCode() and Equals() implementation | ||
| /// </summary> | ||
| internal class HashSetCoreVariant : ICoreCollectionVariant | ||
| { | ||
| private readonly HashSet<ICOMObject> _globalObjectList = new HashSet<ICOMObject>(); | ||
| private readonly object _lock = new object(); | ||
|
|
||
| public void AddObject(ICOMObject obj) | ||
| { | ||
| lock (_lock) | ||
| { | ||
| _globalObjectList.Add(obj); | ||
| } | ||
| } | ||
|
|
||
| public bool RemoveObject(ICOMObject obj) | ||
| { | ||
| lock (_lock) | ||
| { | ||
| return _globalObjectList.Remove(obj); | ||
| } | ||
| } | ||
|
|
||
| public void Clear() | ||
| { | ||
| lock (_lock) | ||
| { | ||
| _globalObjectList.Clear(); | ||
| } | ||
| } | ||
|
|
||
| public int Count | ||
| { | ||
| get | ||
| { | ||
| lock (_lock) | ||
| { | ||
| return _globalObjectList.Count; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Variant 3: Dictionary keyed by IntPtr (alternative) | ||
| /// Time: O(1) per removal by key | ||
| /// Memory: O(n) | ||
| /// Benefit: Can key by COM pointer for guaranteed uniqueness | ||
| /// </summary> | ||
| internal class DictionaryCoreVariant : ICoreCollectionVariant | ||
| { | ||
| private readonly Dictionary<int, ICOMObject> _globalObjectList = new Dictionary<int, ICOMObject>(); | ||
| private readonly object _lock = new object(); | ||
|
|
||
| public void AddObject(ICOMObject obj) | ||
| { | ||
| lock (_lock) | ||
| { | ||
| int key = obj.GetHashCode(); | ||
| _globalObjectList[key] = obj; | ||
| } | ||
| } | ||
|
|
||
| public bool RemoveObject(ICOMObject obj) | ||
| { | ||
| lock (_lock) | ||
| { | ||
| int key = obj.GetHashCode(); | ||
| return _globalObjectList.Remove(key); | ||
| } | ||
| } | ||
|
|
||
| public void Clear() | ||
| { | ||
| lock (_lock) | ||
| { | ||
| _globalObjectList.Clear(); | ||
| } | ||
| } | ||
|
|
||
| public int Count | ||
| { | ||
| get | ||
| { | ||
| lock (_lock) | ||
| { | ||
| return _globalObjectList.Count; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Variant 4: ConcurrentDictionary (lock-free alternative) | ||
| /// Time: O(1) per removal (average) | ||
| /// Memory: O(n) with higher overhead | ||
| /// Benefit: Reduces lock contention with built-in thread-safety | ||
| /// </summary> | ||
| internal class ConcurrentDictionaryCoreVariant : ICoreCollectionVariant | ||
| { | ||
| private readonly ConcurrentDictionary<int, ICOMObject> _globalObjectList = | ||
| new ConcurrentDictionary<int, ICOMObject>(); | ||
|
|
||
| public void AddObject(ICOMObject obj) | ||
| { | ||
| int key = obj.GetHashCode(); | ||
| _globalObjectList[key] = obj; | ||
| } | ||
|
|
||
| public bool RemoveObject(ICOMObject obj) | ||
| { | ||
| int key = obj.GetHashCode(); | ||
| return _globalObjectList.TryRemove(key, out _); | ||
| } | ||
|
|
||
| public void Clear() | ||
| { | ||
| _globalObjectList.Clear(); | ||
| } | ||
|
|
||
| public int Count => _globalObjectList.Count; | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When two distinct COM wrappers return the same
GetHashCode()value, this dictionary variant overwrites the first entry and later removes by hash alone; the concurrent variant uses the same pattern.GetHashCode()is not a unique key, and the realCOMObjectimplementation delegates tobase.GetHashCode()inSource/NetOffice/COMObject.cs, so collisions are possible even thoughMockCOMObjectmakes them unlikely. This makes the benchmark compare a collection that can drop proxies under collision inputs rather than preserving the current list semantics.Useful? React with 👍 / 👎.