Skip to content

Commit f011b2c

Browse files
committed
Make more SimpleAST nodes singletons (fewer allocs)
1 parent 26bc908 commit f011b2c

14 files changed

+62
-39
lines changed

src/Castle.Core/DynamicProxy/Contributors/ClassProxySerializableContributor.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2026 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -187,7 +187,8 @@ private void GenerateSerializationConstructor(ClassEmitter emitter)
187187
typeof(object),
188188
getValue)));
189189
}
190-
ctor.CodeBuilder.AddStatement(new ReturnStatement());
190+
191+
ctor.CodeBuilder.AddStatement(ReturnStatement.Instance);
191192
}
192193

193194
private bool VerifyIfBaseImplementsGetObjectData(Type baseType, MetaType model, out MetaMethod getObjectData)

src/Castle.Core/DynamicProxy/Contributors/ProxyTargetAccessorContributor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2026 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -63,7 +63,7 @@ public void Generate(ClassEmitter emitter)
6363
new ThrowStatement(typeof(InvalidOperationException), "Cannot change the target of the class proxy."));
6464
}
6565

66-
dynProxySetTarget.CodeBuilder.AddStatement(new ReturnStatement());
66+
dynProxySetTarget.CodeBuilder.AddStatement(ReturnStatement.Instance);
6767

6868
var getInterceptors = emitter.CreateMethod(nameof(IProxyTargetAccessor.GetInterceptors), typeof(IInterceptor[]));
6969

src/Castle.Core/DynamicProxy/Contributors/SerializableContributor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2026 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -125,7 +125,7 @@ protected void ImplementGetObjectData(ClassEmitter emitter)
125125

126126
CustomizeGetObjectData(getObjectData.CodeBuilder, info, getObjectData.Arguments[1], emitter);
127127

128-
getObjectData.CodeBuilder.AddStatement(new ReturnStatement());
128+
getObjectData.CodeBuilder.AddStatement(ReturnStatement.Instance);
129129
}
130130

131131
protected virtual void AddAddValueInvocation(ArgumentReference serializationInfo, MethodEmitter getObjectData,

src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2026 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -156,7 +156,7 @@ protected void CheckNotGenericTypeDefinitions(IEnumerable<Type> types, string ar
156156

157157
protected void CompleteInitCacheMethod(CodeBuilder constCodeBuilder)
158158
{
159-
constCodeBuilder.AddStatement(new ReturnStatement());
159+
constCodeBuilder.AddStatement(ReturnStatement.Instance);
160160
}
161161

162162
protected virtual void CreateFields(ClassEmitter emitter)
@@ -281,7 +281,7 @@ protected void GenerateConstructor(ClassEmitter emitter, ConstructorInfo baseCon
281281
constructor.CodeBuilder.AddStatement(new ConstructorInvocationStatement(emitter.BaseType));
282282
}
283283

284-
constructor.CodeBuilder.AddStatement(new ReturnStatement());
284+
constructor.CodeBuilder.AddStatement(ReturnStatement.Instance);
285285
}
286286

287287
protected void GenerateConstructors(ClassEmitter emitter, Type baseType, params FieldReference[] fields)
@@ -339,7 +339,7 @@ protected void GenerateParameterlessConstructor(ClassEmitter emitter, Type baseC
339339

340340
constructor.CodeBuilder.AddStatement(new ConstructorInvocationStatement(defaultConstructor));
341341

342-
constructor.CodeBuilder.AddStatement(new ReturnStatement());
342+
constructor.CodeBuilder.AddStatement(ReturnStatement.Instance);
343343
}
344344

345345
protected ConstructorEmitter GenerateStaticConstructor(ClassEmitter emitter)

src/Castle.Core/DynamicProxy/Generators/Emitters/ConstructorEmitter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2026 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -76,7 +76,7 @@ public virtual void Generate()
7676
CodeBuilder.AddStatement(new ConstructorInvocationStatement(mainType.BaseType));
7777
}
7878

79-
CodeBuilder.AddStatement(new ReturnStatement());
79+
CodeBuilder.AddStatement(ReturnStatement.Instance);
8080
}
8181

8282
CodeBuilder.Generate(builder.GetILGenerator());

src/Castle.Core/DynamicProxy/Generators/Emitters/MethodEmitter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public virtual void Generate()
131131
{
132132
if (ReturnType == typeof(void))
133133
{
134-
CodeBuilder.AddStatement(new ReturnStatement());
134+
CodeBuilder.AddStatement(ReturnStatement.Instance);
135135
}
136136
else
137137
{

src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/EndExceptionBlockStatement.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2026 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -16,8 +16,14 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
1616
{
1717
using System.Reflection.Emit;
1818

19-
internal class EndExceptionBlockStatement : IStatement
19+
internal sealed class EndExceptionBlockStatement : IStatement
2020
{
21+
public static readonly EndExceptionBlockStatement Instance = new EndExceptionBlockStatement();
22+
23+
private EndExceptionBlockStatement()
24+
{
25+
}
26+
2127
public void Emit(ILGenerator gen)
2228
{
2329
gen.EndExceptionBlock();

src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/FinallyStatement.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2026 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -16,8 +16,14 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
1616
{
1717
using System.Reflection.Emit;
1818

19-
internal class FinallyStatement : IStatement
19+
internal sealed class FinallyStatement : IStatement
2020
{
21+
public static readonly FinallyStatement Instance = new FinallyStatement();
22+
23+
private FinallyStatement()
24+
{
25+
}
26+
2127
public void Emit(ILGenerator gen)
2228
{
2329
gen.BeginFinallyBlock();

src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/ReturnStatement.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2025 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2026 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -18,11 +18,13 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
1818
{
1919
using System.Reflection.Emit;
2020

21-
internal class ReturnStatement : IStatement
21+
internal sealed class ReturnStatement : IStatement
2222
{
23+
public static readonly ReturnStatement Instance = new ReturnStatement();
24+
2325
private readonly IExpression? expression;
2426

25-
public ReturnStatement()
27+
private ReturnStatement()
2628
{
2729
}
2830

src/Castle.Core/DynamicProxy/Generators/Emitters/SimpleAST/TryStatement.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2004-2021 Castle Project - http://www.castleproject.org/
1+
// Copyright 2004-2026 Castle Project - http://www.castleproject.org/
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -16,8 +16,14 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST
1616
{
1717
using System.Reflection.Emit;
1818

19-
internal class TryStatement : IStatement
19+
internal sealed class TryStatement : IStatement
2020
{
21+
public static readonly TryStatement Instance = new TryStatement();
22+
23+
private TryStatement()
24+
{
25+
}
26+
2127
public void Emit(ILGenerator gen)
2228
{
2329
gen.BeginExceptionBlock();

0 commit comments

Comments
 (0)