Skip to content

The call time of the static ProxyGenerator's CreateClassProxyType method is gradually increasing. #486

@maliming

Description

@maliming

⚠️ Use of a single ProxyGenerator's instance: If you have a long running process (web site, windows service, etc.) and you have to create many dynamic proxies, you should make sure to reuse the same ProxyGenerator instance. If not, be aware that you will then bypass the caching mechanism. Side effects are high CPU usage and constant increase in memory consumption.

Castle.Core version 4.4.0

I have a lot of mvc controllers for net core 3.1(netcoreapp3.1).

public abstract class MyControllerBase : Controller
{
}

public class Test0Controller : MyControllerBase
{
	[HttpGet]
	[Route("Get")]
	public string TestMethod()
	{
		return "test";
	}
}

public class Test1Controller : MyControllerBase
{
	[HttpGet]
	[Route("Get")]
	public string TestMethod()
	{
		return "test";
	}
}

//public class Test200Controller : MyControllerBase

But the result of static ProxyGenerator and creating a new ProxyGenerator is different.
The call time of the static ProxyGenerator's CreateClassProxyType method is gradually increasing.

public class Program
{
	private static readonly ProxyGenerator ProxyGenerator = new ProxyGenerator();
	
	public static void Main(string[] args)
	{
		foreach (var type in Assembly.GetExecutingAssembly().GetTypes().Where(x => typeof(MyControllerBase).IsAssignableFrom(x)))
		{
			var stopwatch1 = Stopwatch.StartNew();
			ProxyGenerator.ProxyBuilder.CreateClassProxyType(
				type,
				Type.EmptyTypes,
				ProxyGenerationOptions.Default);
			stopwatch1.Stop();

			var stopwatch2 = Stopwatch.StartNew();
			new ProxyGenerator().ProxyBuilder.CreateClassProxyType(
				type,
				Type.EmptyTypes,
				ProxyGenerationOptions.Default);
			stopwatch2.Stop();

			
			Console.WriteLine("static ProxyGenerator=>ElapsedMilliseconds:" + stopwatch1.ElapsedMilliseconds + "\t" + "new ProxyGenerator=>ElapsedMilliseconds:" + stopwatch2.ElapsedMilliseconds);
		}
	
	}
}

Console output:

static ProxyGenerator=>ElapsedMilliseconds:86   new ProxyGenerator=>ElapsedMilliseconds:64
static ProxyGenerator=>ElapsedMilliseconds:136  new ProxyGenerator=>ElapsedMilliseconds:62
static ProxyGenerator=>ElapsedMilliseconds:154  new ProxyGenerator=>ElapsedMilliseconds:65
static ProxyGenerator=>ElapsedMilliseconds:209  new ProxyGenerator=>ElapsedMilliseconds:59
static ProxyGenerator=>ElapsedMilliseconds:221  new ProxyGenerator=>ElapsedMilliseconds:59
static ProxyGenerator=>ElapsedMilliseconds:250  new ProxyGenerator=>ElapsedMilliseconds:58
static ProxyGenerator=>ElapsedMilliseconds:274  new ProxyGenerator=>ElapsedMilliseconds:76
static ProxyGenerator=>ElapsedMilliseconds:295  new ProxyGenerator=>ElapsedMilliseconds:55
static ProxyGenerator=>ElapsedMilliseconds:318  new ProxyGenerator=>ElapsedMilliseconds:55
static ProxyGenerator=>ElapsedMilliseconds:352  new ProxyGenerator=>ElapsedMilliseconds:56
static ProxyGenerator=>ElapsedMilliseconds:376  new ProxyGenerator=>ElapsedMilliseconds:54
static ProxyGenerator=>ElapsedMilliseconds:405  new ProxyGenerator=>ElapsedMilliseconds:55
static ProxyGenerator=>ElapsedMilliseconds:413  new ProxyGenerator=>ElapsedMilliseconds:55
static ProxyGenerator=>ElapsedMilliseconds:450  new ProxyGenerator=>ElapsedMilliseconds:55
static ProxyGenerator=>ElapsedMilliseconds:471  new ProxyGenerator=>ElapsedMilliseconds:56
static ProxyGenerator=>ElapsedMilliseconds:513  new ProxyGenerator=>ElapsedMilliseconds:59
static ProxyGenerator=>ElapsedMilliseconds:528  new ProxyGenerator=>ElapsedMilliseconds:56
static ProxyGenerator=>ElapsedMilliseconds:565  new ProxyGenerator=>ElapsedMilliseconds:57
static ProxyGenerator=>ElapsedMilliseconds:588  new ProxyGenerator=>ElapsedMilliseconds:57
static ProxyGenerator=>ElapsedMilliseconds:611  new ProxyGenerator=>ElapsedMilliseconds:56
static ProxyGenerator=>ElapsedMilliseconds:643  new ProxyGenerator=>ElapsedMilliseconds:57
static ProxyGenerator=>ElapsedMilliseconds:690  new ProxyGenerator=>ElapsedMilliseconds:57
static ProxyGenerator=>ElapsedMilliseconds:687  new ProxyGenerator=>ElapsedMilliseconds:56
static ProxyGenerator=>ElapsedMilliseconds:722  new ProxyGenerator=>ElapsedMilliseconds:63
static ProxyGenerator=>ElapsedMilliseconds:748  new ProxyGenerator=>ElapsedMilliseconds:55
static ProxyGenerator=>ElapsedMilliseconds:761  new ProxyGenerator=>ElapsedMilliseconds:56
static ProxyGenerator=>ElapsedMilliseconds:799  new ProxyGenerator=>ElapsedMilliseconds:57
static ProxyGenerator=>ElapsedMilliseconds:830  new ProxyGenerator=>ElapsedMilliseconds:56
static ProxyGenerator=>ElapsedMilliseconds:847  new ProxyGenerator=>ElapsedMilliseconds:57
static ProxyGenerator=>ElapsedMilliseconds:886  new ProxyGenerator=>ElapsedMilliseconds:58
static ProxyGenerator=>ElapsedMilliseconds:957  new ProxyGenerator=>ElapsedMilliseconds:56
static ProxyGenerator=>ElapsedMilliseconds:979  new ProxyGenerator=>ElapsedMilliseconds:56
static ProxyGenerator=>ElapsedMilliseconds:1008 new ProxyGenerator=>ElapsedMilliseconds:56
static ProxyGenerator=>ElapsedMilliseconds:1036 new ProxyGenerator=>ElapsedMilliseconds:57
static ProxyGenerator=>ElapsedMilliseconds:1052 new ProxyGenerator=>ElapsedMilliseconds:56
static ProxyGenerator=>ElapsedMilliseconds:1094 new ProxyGenerator=>ElapsedMilliseconds:56

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions