Skip to content

Replaced SynchronizedDictionary with ConcurrentDictionary#746

Open
snakefoot wants to merge 1 commit into
castleproject:masterfrom
snakefoot:ConcurrentDictionary
Open

Replaced SynchronizedDictionary with ConcurrentDictionary#746
snakefoot wants to merge 1 commit into
castleproject:masterfrom
snakefoot:ConcurrentDictionary

Conversation

@snakefoot
Copy link
Copy Markdown
Contributor

@snakefoot snakefoot commented May 20, 2026

Replaced home-grown SynchronizedDictionary with ConcurrentDictionary.

Believe the special GetOrAddWithoutTakingLock-method was there because SynchronizedDictionary used non-recursive ReaderWriterLockSlim (faster than recursive). The factory-lock has been changed to a standard monitor-lock with recursion support, so special method is no longer needed.

Made a small optimization in GetAllInterfaces where the performance test from #744 arrived with empty-array, which triggered HashSet-allocation. Now it just returns empty-array without HashSet-allocation.

Made a small optimization in EnsureValidBaseType by skipping constructor-reflection for known typeof(object)

See also: #743

@snakefoot snakefoot force-pushed the ConcurrentDictionary branch 5 times, most recently from 2c7ea56 to c139a0d Compare May 20, 2026 19:26
@snakefoot
Copy link
Copy Markdown
Contributor Author

snakefoot commented May 20, 2026

Took the performance-test from #744 (Removed the Task.Delay logic). And this PullRequest changed the performance time from 250ms to 185ms on my machine.

So it seems the existing ReaderWriterLockSlim is doing an okay job of handling concurrency. And this pull-request will not explode any performance barriers. But I think using ConcurrentDictionary is making the code prettier and less exotic.

// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.DynamicProxy.Tests
{
	using System;
	using System.Diagnostics;
	using System.Linq;
	using System.Reflection;
	using System.Security.Permissions;
	using System.Threading;
	using System.Threading.Tasks;

	using Castle.Core.Logging;
	using Castle.DynamicProxy.Generators;
	using Castle.DynamicProxy.Tests.Classes;

	using NUnit.Framework;

	[TestFixture]
	public class ProxyGeneratorPerfTest
	{
		[Test]
		public async Task Perf_Under_Heavy_Load()
		{
			var generator = new ProxyGenerator();
			int stressLevel = Environment.ProcessorCount;

			Type[] types = [typeof(I1), typeof(I2), typeof(I3), typeof(I4), typeof(I5)];
			var tasks = new Task[stressLevel];

			ManualResetEvent manualResetEvent = new ManualResetEvent(false);

			for (int i = 0; i < stressLevel; i++)
			{
				var iCopy = i;

				tasks[i] = Task.Run(() => {
					manualResetEvent.WaitOne();
					for (int i =0; i < 50000; i++)
						generator.CreateInterfaceProxyWithoutTarget(types[iCopy % types.Length]);
				});
			}

			manualResetEvent.Set();

			var sw = Stopwatch.StartNew();
			await Task.WhenAll(tasks);
			sw.Stop();
			var elapsed = sw.ElapsedMilliseconds;
			Console.WriteLine(elapsed);
		}
	}

	internal interface I1
	{
		void M1();
		void M2();
	}

	internal interface I2
	{
		void M1();
		void M2();
	}

	internal interface I3
	{
		void M1();
		void M2();
	}

	internal interface I4
	{
		void M1();
		void M2();
	}

	internal interface I5
	{
		void M1();
		void M2();
	}
}

Profile before:
image
Profile after:
image

@snakefoot snakefoot force-pushed the ConcurrentDictionary branch 5 times, most recently from 28037e0 to e1e61dc Compare May 20, 2026 21:00
@snakefoot snakefoot force-pushed the ConcurrentDictionary branch from e1e61dc to 3be20d6 Compare May 20, 2026 21:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant