Skip to content

Commit 8ef29cf

Browse files
committed
Introduce a ValueLazy to reduce allocations
1 parent ce0210f commit 8ef29cf

File tree

171 files changed

+720
-654
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

171 files changed

+720
-654
lines changed

sources/ClangSharp/Cursors/Cursor.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ namespace ClangSharp;
1313
[DebuggerDisplay("{Handle.DebuggerDisplayString,nq}")]
1414
public unsafe class Cursor : IEquatable<Cursor>
1515
{
16-
private readonly Lazy<string> _kindSpelling;
17-
private readonly Lazy<Cursor?> _lexicalParentCursor;
18-
private readonly Lazy<Cursor?> _semanticParentCursor;
19-
private readonly Lazy<string> _spelling;
20-
private readonly Lazy<TranslationUnit> _translationUnit;
16+
private readonly ValueLazy<string> _kindSpelling;
17+
private readonly ValueLazy<Cursor?> _lexicalParentCursor;
18+
private readonly ValueLazy<Cursor?> _semanticParentCursor;
19+
private readonly ValueLazy<string> _spelling;
20+
private readonly ValueLazy<TranslationUnit> _translationUnit;
2121
private List<Cursor>? _cursorChildren;
2222

2323
private protected Cursor(CXCursor handle, CXCursorKind expectedCursorKind)
@@ -28,11 +28,11 @@ private protected Cursor(CXCursor handle, CXCursorKind expectedCursorKind)
2828
}
2929
Handle = handle;
3030

31-
_kindSpelling = new Lazy<string>(Handle.KindSpelling.ToString);
32-
_lexicalParentCursor = new Lazy<Cursor?>(() => !Handle.LexicalParent.IsNull ? TranslationUnit.GetOrCreate<Cursor>(Handle.LexicalParent) : null);
33-
_semanticParentCursor = new Lazy<Cursor?>(() => !Handle.SemanticParent.IsNull ? TranslationUnit.GetOrCreate<Cursor>(Handle.SemanticParent) : null);
34-
_spelling = new Lazy<string>(Handle.Spelling.ToString);
35-
_translationUnit = new Lazy<TranslationUnit>(() => TranslationUnit.GetOrCreate(Handle.TranslationUnit));
31+
_kindSpelling = new ValueLazy<string>(Handle.KindSpelling.ToString);
32+
_lexicalParentCursor = new ValueLazy<Cursor?>(() => !Handle.LexicalParent.IsNull ? TranslationUnit.GetOrCreate<Cursor>(Handle.LexicalParent) : null);
33+
_semanticParentCursor = new ValueLazy<Cursor?>(() => !Handle.SemanticParent.IsNull ? TranslationUnit.GetOrCreate<Cursor>(Handle.SemanticParent) : null);
34+
_spelling = new ValueLazy<string>(Handle.Spelling.ToString);
35+
_translationUnit = new ValueLazy<TranslationUnit>(() => TranslationUnit.GetOrCreate(Handle.TranslationUnit));
3636
}
3737

3838
public IReadOnlyList<Cursor> CursorChildren

sources/ClangSharp/Cursors/Decls/BindingDecl.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ namespace ClangSharp;
99

1010
public sealed class BindingDecl : ValueDecl
1111
{
12-
private readonly Lazy<Expr> _binding;
13-
private readonly Lazy<ValueDecl> _decomposedDecl;
14-
private readonly Lazy<VarDecl> _holdingVar;
12+
private readonly ValueLazy<Expr> _binding;
13+
private readonly ValueLazy<ValueDecl> _decomposedDecl;
14+
private readonly ValueLazy<VarDecl> _holdingVar;
1515

1616
internal BindingDecl(CXCursor handle) : base(handle, CXCursor_UnexposedDecl, CX_DeclKind_Binding)
1717
{
18-
_binding = new Lazy<Expr>(() => TranslationUnit.GetOrCreate<Expr>(Handle.BindingExpr));
19-
_decomposedDecl = new Lazy<ValueDecl>(() => TranslationUnit.GetOrCreate<ValueDecl>(Handle.DecomposedDecl));
20-
_holdingVar = new Lazy<VarDecl>(() => TranslationUnit.GetOrCreate<VarDecl>(Handle.GetSubDecl(0)));
18+
_binding = new ValueLazy<Expr>(() => TranslationUnit.GetOrCreate<Expr>(Handle.BindingExpr));
19+
_decomposedDecl = new ValueLazy<ValueDecl>(() => TranslationUnit.GetOrCreate<ValueDecl>(Handle.DecomposedDecl));
20+
_holdingVar = new ValueLazy<VarDecl>(() => TranslationUnit.GetOrCreate<VarDecl>(Handle.GetSubDecl(0)));
2121
}
2222

2323
public Expr Binding => _binding.Value;

sources/ClangSharp/Cursors/Decls/BlockDecl.Capture.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ public sealed class Capture
1010
{
1111
private readonly Decl _parentDecl;
1212
private readonly uint _index;
13-
private readonly Lazy<Expr> _copyExpr;
14-
private readonly Lazy<VarDecl> _variable;
13+
private readonly ValueLazy<Expr> _copyExpr;
14+
private readonly ValueLazy<VarDecl> _variable;
1515

1616
internal Capture(Decl parentDecl, uint index)
1717
{
1818
_parentDecl = parentDecl;
1919
_index = index;
2020

21-
_copyExpr = new Lazy<Expr>(() => _parentDecl.TranslationUnit.GetOrCreate<Expr>(_parentDecl.Handle.GetCaptureCopyExpr(_index)));
22-
_variable = new Lazy<VarDecl>(() => _parentDecl.TranslationUnit.GetOrCreate<VarDecl>(_parentDecl.Handle.GetCaptureVariable(_index)));
21+
_copyExpr = new ValueLazy<Expr>(() => _parentDecl.TranslationUnit.GetOrCreate<Expr>(_parentDecl.Handle.GetCaptureCopyExpr(_index)));
22+
_variable = new ValueLazy<VarDecl>(() => _parentDecl.TranslationUnit.GetOrCreate<VarDecl>(_parentDecl.Handle.GetCaptureVariable(_index)));
2323
}
2424

2525
public Expr CopyExpr => _copyExpr.Value;

sources/ClangSharp/Cursors/Decls/BlockDecl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ namespace ClangSharp;
1010

1111
public sealed partial class BlockDecl : Decl, IDeclContext
1212
{
13-
private readonly Lazy<Decl> _blockManglingContextDecl;
13+
private readonly ValueLazy<Decl> _blockManglingContextDecl;
1414
private readonly LazyList<Capture> _captures;
1515
private readonly LazyList<ParmVarDecl> _parameters;
1616

1717
internal BlockDecl(CXCursor handle) : base(handle, CXCursor_UnexposedDecl, CX_DeclKind_Block)
1818
{
19-
_blockManglingContextDecl = new Lazy<Decl>(() => TranslationUnit.GetOrCreate<Decl>(Handle.BlockManglingContextDecl));
19+
_blockManglingContextDecl = new ValueLazy<Decl>(() => TranslationUnit.GetOrCreate<Decl>(Handle.BlockManglingContextDecl));
2020
_captures = LazyList.Create<Capture>(Handle.NumCaptures, (i) => new Capture(this, unchecked((uint)i)));
2121
_parameters = LazyList.Create<ParmVarDecl>(Handle.NumArguments, (i) => TranslationUnit.GetOrCreate<ParmVarDecl>(Handle.GetArgument(unchecked((uint)i))));
2222
}

sources/ClangSharp/Cursors/Decls/CXXConstructorDecl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ namespace ClangSharp;
1010

1111
public sealed class CXXConstructorDecl : CXXMethodDecl
1212
{
13-
private readonly Lazy<CXXConstructorDecl> _inheritedConstructor;
13+
private readonly ValueLazy<CXXConstructorDecl> _inheritedConstructor;
1414
private readonly LazyList<Expr> _initExprs;
1515

1616
internal CXXConstructorDecl(CXCursor handle) : base(handle, CXCursor_Constructor, CX_DeclKind_CXXConstructor)
1717
{
18-
_inheritedConstructor = new Lazy<CXXConstructorDecl>(() => TranslationUnit.GetOrCreate<CXXConstructorDecl>(Handle.InheritedConstructor));
18+
_inheritedConstructor = new ValueLazy<CXXConstructorDecl>(() => TranslationUnit.GetOrCreate<CXXConstructorDecl>(Handle.InheritedConstructor));
1919
_initExprs = LazyList.Create<Expr>(Handle.NumExprs , (i) => TranslationUnit.GetOrCreate<Expr>(Handle.GetExpr(unchecked((uint)i))));
2020
}
2121

sources/ClangSharp/Cursors/Decls/CXXConversionDecl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ namespace ClangSharp;
99

1010
public sealed class CXXConversionDecl : CXXMethodDecl
1111
{
12-
private readonly Lazy<Type> _conversionType;
12+
private readonly ValueLazy<Type> _conversionType;
1313

1414
internal CXXConversionDecl(CXCursor handle) : base(handle, CXCursor_ConversionFunction, CX_DeclKind_CXXConversion)
1515
{
16-
_conversionType = new Lazy<Type>(() => TranslationUnit.GetOrCreate<Type>(Handle.TypeOperand));
16+
_conversionType = new ValueLazy<Type>(() => TranslationUnit.GetOrCreate<Type>(Handle.TypeOperand));
1717
}
1818

1919
public new CXXConversionDecl CanonicalDecl => (CXXConversionDecl)base.CanonicalDecl;

sources/ClangSharp/Cursors/Decls/CXXDeductionGuideDecl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ namespace ClangSharp;
99

1010
public sealed class CXXDeductionGuideDecl : FunctionDecl
1111
{
12-
private readonly Lazy<TemplateDecl> _deducedTemplate;
12+
private readonly ValueLazy<TemplateDecl> _deducedTemplate;
1313

1414
internal CXXDeductionGuideDecl(CXCursor handle) : base(handle, CXCursor_UnexposedDecl, CX_DeclKind_CXXDeductionGuide)
1515
{
16-
_deducedTemplate = new Lazy<TemplateDecl>(() => TranslationUnit.GetOrCreate<TemplateDecl>(handle.TemplatedDecl));
16+
_deducedTemplate = new ValueLazy<TemplateDecl>(() => TranslationUnit.GetOrCreate<TemplateDecl>(handle.TemplatedDecl));
1717
}
1818

1919
public bool IsExplicit => !Handle.IsImplicit;

sources/ClangSharp/Cursors/Decls/CXXDestructorDecl.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ namespace ClangSharp;
99

1010
public sealed class CXXDestructorDecl : CXXMethodDecl
1111
{
12-
private readonly Lazy<FunctionDecl> _operatorDelete;
13-
private readonly Lazy<Expr> _operatorDeleteThisArg;
12+
private readonly ValueLazy<FunctionDecl> _operatorDelete;
13+
private readonly ValueLazy<Expr> _operatorDeleteThisArg;
1414

1515
internal CXXDestructorDecl(CXCursor handle) : base(handle, CXCursor_Destructor, CX_DeclKind_CXXDestructor)
1616
{
17-
_operatorDelete = new Lazy<FunctionDecl>(() => TranslationUnit.GetOrCreate<FunctionDecl>(Handle.GetSubDecl(0)));
18-
_operatorDeleteThisArg = new Lazy<Expr>(() => TranslationUnit.GetOrCreate<Expr>(Handle.GetExpr(0)));
17+
_operatorDelete = new ValueLazy<FunctionDecl>(() => TranslationUnit.GetOrCreate<FunctionDecl>(Handle.GetSubDecl(0)));
18+
_operatorDeleteThisArg = new ValueLazy<Expr>(() => TranslationUnit.GetOrCreate<Expr>(Handle.GetExpr(0)));
1919
}
2020

2121
public new CXXDestructorDecl CanonicalDecl => (CXXDestructorDecl)base.CanonicalDecl;

sources/ClangSharp/Cursors/Decls/CXXMethodDecl.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ namespace ClangSharp;
1111
public class CXXMethodDecl : FunctionDecl
1212
{
1313
private readonly LazyList<CXXMethodDecl> _overriddenMethods;
14-
private readonly Lazy<Type> _thisType;
15-
private readonly Lazy<Type> _thisObjectType;
14+
private readonly ValueLazy<Type> _thisType;
15+
private readonly ValueLazy<Type> _thisObjectType;
1616

1717
internal CXXMethodDecl(CXCursor handle) : this(handle, CXCursor_CXXMethod, CX_DeclKind_CXXMethod)
1818
{
@@ -26,8 +26,8 @@ private protected CXXMethodDecl(CXCursor handle, CXCursorKind expectedCursorKind
2626
}
2727

2828
_overriddenMethods = LazyList.Create<CXXMethodDecl>(Handle.NumMethods, (i) => TranslationUnit.GetOrCreate<CXXMethodDecl>(Handle.GetMethod(unchecked((uint)i))));
29-
_thisType = new Lazy<Type>(() => TranslationUnit.GetOrCreate<Type>(Handle.ThisType));
30-
_thisObjectType = new Lazy<Type>(() => TranslationUnit.GetOrCreate<Type>(Handle.ThisObjectType));
29+
_thisType = new ValueLazy<Type>(() => TranslationUnit.GetOrCreate<Type>(Handle.ThisType));
30+
_thisObjectType = new ValueLazy<Type>(() => TranslationUnit.GetOrCreate<Type>(Handle.ThisObjectType));
3131
}
3232

3333
public new CXXMethodDecl CanonicalDecl => (CXXMethodDecl)base.CanonicalDecl;

sources/ClangSharp/Cursors/Decls/CXXRecordDecl.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ public class CXXRecordDecl : RecordDecl
1212
{
1313
private readonly LazyList<CXXBaseSpecifier> _bases;
1414
private readonly LazyList<CXXConstructorDecl> _ctors;
15-
private readonly Lazy<FunctionTemplateDecl> _dependentLambdaCallOperator;
16-
private readonly Lazy<ClassTemplateDecl> _describedClassTemplate;
17-
private readonly Lazy<CXXDestructorDecl?> _destructor;
15+
private readonly ValueLazy<FunctionTemplateDecl> _dependentLambdaCallOperator;
16+
private readonly ValueLazy<ClassTemplateDecl> _describedClassTemplate;
17+
private readonly ValueLazy<CXXDestructorDecl?> _destructor;
1818
private readonly LazyList<FriendDecl> _friends;
19-
private readonly Lazy<CXXRecordDecl> _instantiatedFromMemberClass;
20-
private readonly Lazy<CXXMethodDecl> _lambdaCallOperator;
21-
private readonly Lazy<Decl> _lambdaContextDecl;
22-
private readonly Lazy<CXXMethodDecl> _lambdaStaticInvoker;
19+
private readonly ValueLazy<CXXRecordDecl> _instantiatedFromMemberClass;
20+
private readonly ValueLazy<CXXMethodDecl> _lambdaCallOperator;
21+
private readonly ValueLazy<Decl> _lambdaContextDecl;
22+
private readonly ValueLazy<CXXMethodDecl> _lambdaStaticInvoker;
2323
private readonly LazyList<CXXMethodDecl> _methods;
24-
private readonly Lazy<CXXRecordDecl> _templateInstantiationPattern;
24+
private readonly ValueLazy<CXXRecordDecl> _templateInstantiationPattern;
2525
private readonly LazyList<CXXBaseSpecifier> _vbases;
2626

2727
internal CXXRecordDecl(CXCursor handle) : this(handle, handle.Kind, CX_DeclKind_CXXRecord)
@@ -37,19 +37,19 @@ private protected CXXRecordDecl(CXCursor handle, CXCursorKind expectedCursorKind
3737

3838
_bases = LazyList.Create<CXXBaseSpecifier>(Handle.NumBases, (i) => TranslationUnit.GetOrCreate<CXXBaseSpecifier>(Handle.GetBase(unchecked((uint)i))));
3939
_ctors = LazyList.Create<CXXConstructorDecl>(Handle.NumCtors, (i) => TranslationUnit.GetOrCreate<CXXConstructorDecl>(Handle.GetCtor(unchecked((uint)i))));
40-
_dependentLambdaCallOperator = new Lazy<FunctionTemplateDecl>(() => TranslationUnit.GetOrCreate<FunctionTemplateDecl>(Handle.DependentLambdaCallOperator));
41-
_describedClassTemplate = new Lazy<ClassTemplateDecl>(() => TranslationUnit.GetOrCreate<ClassTemplateDecl>(Handle.DescribedCursorTemplate));
42-
_destructor = new Lazy<CXXDestructorDecl?>(() => {
40+
_dependentLambdaCallOperator = new ValueLazy<FunctionTemplateDecl>(() => TranslationUnit.GetOrCreate<FunctionTemplateDecl>(Handle.DependentLambdaCallOperator));
41+
_describedClassTemplate = new ValueLazy<ClassTemplateDecl>(() => TranslationUnit.GetOrCreate<ClassTemplateDecl>(Handle.DescribedCursorTemplate));
42+
_destructor = new ValueLazy<CXXDestructorDecl?>(() => {
4343
var destructor = Handle.Destructor;
4444
return destructor.IsNull ? null : TranslationUnit.GetOrCreate<CXXDestructorDecl>(Handle.Destructor);
4545
});
4646
_friends = LazyList.Create<FriendDecl>(Handle.NumFriends, (i) => TranslationUnit.GetOrCreate<FriendDecl>(Handle.GetFriend(unchecked((uint)i))));
47-
_instantiatedFromMemberClass = new Lazy<CXXRecordDecl>(() => TranslationUnit.GetOrCreate<CXXRecordDecl>(Handle.InstantiatedFromMember));
48-
_lambdaCallOperator = new Lazy<CXXMethodDecl>(() => TranslationUnit.GetOrCreate<CXXMethodDecl>(Handle.LambdaCallOperator));
49-
_lambdaContextDecl = new Lazy<Decl>(() => TranslationUnit.GetOrCreate<Decl>(Handle.LambdaContextDecl));
50-
_lambdaStaticInvoker = new Lazy<CXXMethodDecl>(() => TranslationUnit.GetOrCreate<CXXMethodDecl>(Handle.LambdaStaticInvoker));
47+
_instantiatedFromMemberClass = new ValueLazy<CXXRecordDecl>(() => TranslationUnit.GetOrCreate<CXXRecordDecl>(Handle.InstantiatedFromMember));
48+
_lambdaCallOperator = new ValueLazy<CXXMethodDecl>(() => TranslationUnit.GetOrCreate<CXXMethodDecl>(Handle.LambdaCallOperator));
49+
_lambdaContextDecl = new ValueLazy<Decl>(() => TranslationUnit.GetOrCreate<Decl>(Handle.LambdaContextDecl));
50+
_lambdaStaticInvoker = new ValueLazy<CXXMethodDecl>(() => TranslationUnit.GetOrCreate<CXXMethodDecl>(Handle.LambdaStaticInvoker));
5151
_methods = LazyList.Create<CXXMethodDecl>(Handle.NumMethods, (i) => TranslationUnit.GetOrCreate<CXXMethodDecl>(Handle.GetMethod(unchecked((uint)i))));
52-
_templateInstantiationPattern = new Lazy<CXXRecordDecl>(() => TranslationUnit.GetOrCreate<CXXRecordDecl>(Handle.TemplateInstantiationPattern));
52+
_templateInstantiationPattern = new ValueLazy<CXXRecordDecl>(() => TranslationUnit.GetOrCreate<CXXRecordDecl>(Handle.TemplateInstantiationPattern));
5353
_vbases = LazyList.Create<CXXBaseSpecifier>(Handle.NumVBases, (i) => TranslationUnit.GetOrCreate<CXXBaseSpecifier>(Handle.GetVBase(unchecked((uint)i))));
5454
}
5555

0 commit comments

Comments
 (0)