Replaced SynchronizedDictionary with ConcurrentDictionary#746
Open
snakefoot wants to merge 1 commit into
Open
Conversation
2c7ea56 to
c139a0d
Compare
Contributor
Author
|
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();
}
} |
28037e0 to
e1e61dc
Compare
e1e61dc to
3be20d6
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.


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
GetAllInterfaceswhere the performance test from #744 arrived with empty-array, which triggeredHashSet-allocation. Now it just returns empty-array withoutHashSet-allocation.Made a small optimization in
EnsureValidBaseTypeby skipping constructor-reflection for knowntypeof(object)See also: #743