Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/benchmarks.yml
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 }}'
192 changes: 192 additions & 0 deletions Tests/NetOffice.Benchmarks/CoreVariants.cs
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;
Comment on lines +128 to +129

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve distinct objects when hash codes collide

When two distinct COM wrappers return the same legal 32-bit hash code, assigning by obj.GetHashCode() overwrites the first wrapper, and subsequently removing either wrapper removes the shared entry. The mock's monotonically unique hash codes hide this semantic failure, so the benchmark can recommend a dictionary variant that would lose objects from Core's disposal tracking; use an identity-safe key rather than treating hash codes as unique IDs.

Useful? React with 馃憤聽/ 馃憥.

}
}

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;
}
}
Loading
Loading