Skip to content

Commit ce0210f

Browse files
committed
Introduce a new LazyList type to make collections cheaper
1 parent 4bb8634 commit ce0210f

Some content is hidden

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

71 files changed

+722
-1205
lines changed

sources/ClangSharp/Cursors/Decls/BlockDecl.cs

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,24 @@
11
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
22

3-
using ClangSharp.Interop;
4-
using static ClangSharp.Interop.CXCursorKind;
5-
using static ClangSharp.Interop.CX_DeclKind;
63
using System;
74
using System.Collections.Generic;
5+
using ClangSharp.Interop;
6+
using static ClangSharp.Interop.CX_DeclKind;
7+
using static ClangSharp.Interop.CXCursorKind;
88

99
namespace ClangSharp;
1010

1111
public sealed partial class BlockDecl : Decl, IDeclContext
1212
{
1313
private readonly Lazy<Decl> _blockManglingContextDecl;
14-
private readonly Lazy<IReadOnlyList<Capture>> _captures;
15-
private readonly Lazy<IReadOnlyList<ParmVarDecl>> _parameters;
14+
private readonly LazyList<Capture> _captures;
15+
private readonly LazyList<ParmVarDecl> _parameters;
1616

1717
internal BlockDecl(CXCursor handle) : base(handle, CXCursor_UnexposedDecl, CX_DeclKind_Block)
1818
{
1919
_blockManglingContextDecl = new Lazy<Decl>(() => TranslationUnit.GetOrCreate<Decl>(Handle.BlockManglingContextDecl));
20-
_captures = new Lazy<IReadOnlyList<Capture>>(() => {
21-
var captureCount = Handle.NumCaptures;
22-
var captures = new List<Capture>(captureCount);
23-
24-
for (var i = 0; i < captureCount; i++)
25-
{
26-
var capture = new Capture(this, unchecked((uint)i));
27-
captures.Add(capture);
28-
}
29-
30-
return captures;
31-
});
32-
_parameters = new Lazy<IReadOnlyList<ParmVarDecl>>(() => {
33-
var parameterCount = Handle.NumArguments;
34-
var parameters = new List<ParmVarDecl>(parameterCount);
35-
36-
for (var i = 0; i < parameterCount; i++)
37-
{
38-
var parameter = TranslationUnit.GetOrCreate<ParmVarDecl>(Handle.GetArgument(unchecked((uint)i)));
39-
parameters.Add(parameter);
40-
}
41-
42-
return parameters;
43-
});
20+
_captures = LazyList.Create<Capture>(Handle.NumCaptures, (i) => new Capture(this, unchecked((uint)i)));
21+
_parameters = LazyList.Create<ParmVarDecl>(Handle.NumArguments, (i) => TranslationUnit.GetOrCreate<ParmVarDecl>(Handle.GetArgument(unchecked((uint)i))));
4422
}
4523

4624
public Decl BlockManglingContextDecl => _blockManglingContextDecl.Value;
@@ -49,7 +27,7 @@ internal BlockDecl(CXCursor handle) : base(handle, CXCursor_UnexposedDecl, CX_De
4927

5028
public bool BlockMissingReturnType => Handle.BlockMissingReturnType;
5129

52-
public IReadOnlyList<Capture> Captures => _captures.Value;
30+
public IReadOnlyList<Capture> Captures => _captures;
5331

5432
public bool CanAvoidCopyToHeap() => Handle.CanAvoidCopyToHeap;
5533

@@ -69,7 +47,7 @@ internal BlockDecl(CXCursor handle) : base(handle, CXCursor_UnexposedDecl, CX_De
6947

7048
public uint NumParams => unchecked((uint)Handle.NumArguments);
7149

72-
public IReadOnlyList<ParmVarDecl> Parameters => _parameters.Value;
50+
public IReadOnlyList<ParmVarDecl> Parameters => _parameters;
7351

7452
public bool ParamEmpty => ParamSize == 0;
7553

sources/ClangSharp/Cursors/Decls/CXXConstructorDecl.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,27 @@
33
using System;
44
using System.Collections.Generic;
55
using ClangSharp.Interop;
6-
using static ClangSharp.Interop.CXCursorKind;
76
using static ClangSharp.Interop.CX_DeclKind;
7+
using static ClangSharp.Interop.CXCursorKind;
88

99
namespace ClangSharp;
1010

1111
public sealed class CXXConstructorDecl : CXXMethodDecl
1212
{
1313
private readonly Lazy<CXXConstructorDecl> _inheritedConstructor;
14-
private readonly Lazy<IReadOnlyList<Expr>> _initExprs;
14+
private readonly LazyList<Expr> _initExprs;
1515

1616
internal CXXConstructorDecl(CXCursor handle) : base(handle, CXCursor_Constructor, CX_DeclKind_CXXConstructor)
1717
{
1818
_inheritedConstructor = new Lazy<CXXConstructorDecl>(() => TranslationUnit.GetOrCreate<CXXConstructorDecl>(Handle.InheritedConstructor));
19-
_initExprs = new Lazy<IReadOnlyList<Expr>>(() => {
20-
var numInitExprs = Handle.NumExprs;
21-
var initExprs = new List<Expr>(numInitExprs);
22-
23-
for (var i = 0; i < numInitExprs; i++)
24-
{
25-
var initExpr = TranslationUnit.GetOrCreate<Expr>(Handle.GetExpr(unchecked((uint)i)));
26-
initExprs.Add(initExpr);
27-
}
28-
29-
return initExprs;
30-
});
19+
_initExprs = LazyList.Create<Expr>(Handle.NumExprs , (i) => TranslationUnit.GetOrCreate<Expr>(Handle.GetExpr(unchecked((uint)i))));
3120
}
3221

3322
public new CXXConstructorDecl CanonicalDecl => (CXXConstructorDecl)base.CanonicalDecl;
3423

3524
public CXXConstructorDecl InheritedConstructor => _inheritedConstructor.Value;
3625

37-
public IReadOnlyList<Expr> InitExprs => _initExprs.Value;
26+
public IReadOnlyList<Expr> InitExprs => _initExprs;
3827

3928
public bool IsConvertingConstructor => Handle.CXXConstructor_IsConvertingConstructor;
4029

sources/ClangSharp/Cursors/Decls/CXXMethodDecl.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
using System;
44
using System.Collections.Generic;
55
using ClangSharp.Interop;
6-
using static ClangSharp.Interop.CXCursorKind;
76
using static ClangSharp.Interop.CX_DeclKind;
7+
using static ClangSharp.Interop.CXCursorKind;
88

99
namespace ClangSharp;
1010

1111
public class CXXMethodDecl : FunctionDecl
1212
{
13-
private readonly Lazy<IReadOnlyList<CXXMethodDecl>> _overriddenMethods;
13+
private readonly LazyList<CXXMethodDecl> _overriddenMethods;
1414
private readonly Lazy<Type> _thisType;
1515
private readonly Lazy<Type> _thisObjectType;
1616

@@ -25,19 +25,7 @@ private protected CXXMethodDecl(CXCursor handle, CXCursorKind expectedCursorKind
2525
throw new ArgumentOutOfRangeException(nameof(handle));
2626
}
2727

28-
_overriddenMethods = new Lazy<IReadOnlyList<CXXMethodDecl>>(() => {
29-
var numOverriddenMethods = Handle.NumMethods;
30-
var overriddenMethods = new List<CXXMethodDecl>(numOverriddenMethods);
31-
32-
for (var i = 0; i < numOverriddenMethods; i++)
33-
{
34-
var overriddenMethod = TranslationUnit.GetOrCreate<CXXMethodDecl>(Handle.GetMethod(unchecked((uint)i)));
35-
overriddenMethods.Add(overriddenMethod);
36-
}
37-
38-
return overriddenMethods;
39-
});
40-
28+
_overriddenMethods = LazyList.Create<CXXMethodDecl>(Handle.NumMethods, (i) => TranslationUnit.GetOrCreate<CXXMethodDecl>(Handle.GetMethod(unchecked((uint)i))));
4129
_thisType = new Lazy<Type>(() => TranslationUnit.GetOrCreate<Type>(Handle.ThisType));
4230
_thisObjectType = new Lazy<Type>(() => TranslationUnit.GetOrCreate<Type>(Handle.ThisObjectType));
4331
}
@@ -54,7 +42,7 @@ private protected CXXMethodDecl(CXCursor handle, CXCursorKind expectedCursorKind
5442

5543
public new CXXMethodDecl MostRecentDecl => (CXXMethodDecl)base.MostRecentDecl;
5644

57-
public IReadOnlyList<CXXMethodDecl> OverriddenMethods => _overriddenMethods.Value;
45+
public IReadOnlyList<CXXMethodDecl> OverriddenMethods => _overriddenMethods;
5846

5947
public new CXXRecordDecl? Parent => (CXXRecordDecl?)(base.Parent ?? ThisObjectType.AsCXXRecordDecl);
6048

sources/ClangSharp/Cursors/Decls/CXXRecordDecl.cs

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

1111
public class CXXRecordDecl : RecordDecl
1212
{
13-
private readonly Lazy<IReadOnlyList<CXXBaseSpecifier>> _bases;
14-
private readonly Lazy<IReadOnlyList<CXXConstructorDecl>> _ctors;
13+
private readonly LazyList<CXXBaseSpecifier> _bases;
14+
private readonly LazyList<CXXConstructorDecl> _ctors;
1515
private readonly Lazy<FunctionTemplateDecl> _dependentLambdaCallOperator;
1616
private readonly Lazy<ClassTemplateDecl> _describedClassTemplate;
1717
private readonly Lazy<CXXDestructorDecl?> _destructor;
18-
private readonly Lazy<IReadOnlyList<FriendDecl>> _friends;
18+
private readonly LazyList<FriendDecl> _friends;
1919
private readonly Lazy<CXXRecordDecl> _instantiatedFromMemberClass;
2020
private readonly Lazy<CXXMethodDecl> _lambdaCallOperator;
2121
private readonly Lazy<Decl> _lambdaContextDecl;
2222
private readonly Lazy<CXXMethodDecl> _lambdaStaticInvoker;
23-
private readonly Lazy<IReadOnlyList<CXXMethodDecl>> _methods;
23+
private readonly LazyList<CXXMethodDecl> _methods;
2424
private readonly Lazy<CXXRecordDecl> _templateInstantiationPattern;
25-
private readonly Lazy<IReadOnlyList<CXXBaseSpecifier>> _vbases;
25+
private readonly LazyList<CXXBaseSpecifier> _vbases;
2626

2727
internal CXXRecordDecl(CXCursor handle) : this(handle, handle.Kind, CX_DeclKind_CXXRecord)
2828
{
@@ -35,93 +35,31 @@ private protected CXXRecordDecl(CXCursor handle, CXCursorKind expectedCursorKind
3535
throw new ArgumentOutOfRangeException(nameof(handle));
3636
}
3737

38-
_bases = new Lazy<IReadOnlyList<CXXBaseSpecifier>>(() => {
39-
var numBases = Handle.NumBases;
40-
var bases = new List<CXXBaseSpecifier>(numBases);
41-
42-
for (var i = 0; i < numBases; i++)
43-
{
44-
var @base = TranslationUnit.GetOrCreate<CXXBaseSpecifier>(Handle.GetBase(unchecked((uint)i)));
45-
bases.Add(@base);
46-
}
47-
48-
return bases;
49-
});
50-
51-
_ctors = new Lazy<IReadOnlyList<CXXConstructorDecl>>(() => {
52-
var numCtors = Handle.NumCtors;
53-
var ctors = new List<CXXConstructorDecl>(numCtors);
54-
55-
for (var i = 0; i < numCtors; i++)
56-
{
57-
var ctor = TranslationUnit.GetOrCreate<CXXConstructorDecl>(Handle.GetCtor(unchecked((uint)i)));
58-
ctors.Add(ctor);
59-
}
60-
61-
return ctors;
62-
});
63-
38+
_bases = LazyList.Create<CXXBaseSpecifier>(Handle.NumBases, (i) => TranslationUnit.GetOrCreate<CXXBaseSpecifier>(Handle.GetBase(unchecked((uint)i))));
39+
_ctors = LazyList.Create<CXXConstructorDecl>(Handle.NumCtors, (i) => TranslationUnit.GetOrCreate<CXXConstructorDecl>(Handle.GetCtor(unchecked((uint)i))));
6440
_dependentLambdaCallOperator = new Lazy<FunctionTemplateDecl>(() => TranslationUnit.GetOrCreate<FunctionTemplateDecl>(Handle.DependentLambdaCallOperator));
6541
_describedClassTemplate = new Lazy<ClassTemplateDecl>(() => TranslationUnit.GetOrCreate<ClassTemplateDecl>(Handle.DescribedCursorTemplate));
6642
_destructor = new Lazy<CXXDestructorDecl?>(() => {
6743
var destructor = Handle.Destructor;
6844
return destructor.IsNull ? null : TranslationUnit.GetOrCreate<CXXDestructorDecl>(Handle.Destructor);
6945
});
70-
71-
_friends = new Lazy<IReadOnlyList<FriendDecl>>(() => {
72-
var numFriends = Handle.NumFriends;
73-
var friends = new List<FriendDecl>(numFriends);
74-
75-
for (var i = 0; i < numFriends; i++)
76-
{
77-
var friend = TranslationUnit.GetOrCreate<FriendDecl>(Handle.GetFriend(unchecked((uint)i)));
78-
friends.Add(friend);
79-
}
80-
81-
return friends;
82-
});
83-
46+
_friends = LazyList.Create<FriendDecl>(Handle.NumFriends, (i) => TranslationUnit.GetOrCreate<FriendDecl>(Handle.GetFriend(unchecked((uint)i))));
8447
_instantiatedFromMemberClass = new Lazy<CXXRecordDecl>(() => TranslationUnit.GetOrCreate<CXXRecordDecl>(Handle.InstantiatedFromMember));
8548
_lambdaCallOperator = new Lazy<CXXMethodDecl>(() => TranslationUnit.GetOrCreate<CXXMethodDecl>(Handle.LambdaCallOperator));
8649
_lambdaContextDecl = new Lazy<Decl>(() => TranslationUnit.GetOrCreate<Decl>(Handle.LambdaContextDecl));
8750
_lambdaStaticInvoker = new Lazy<CXXMethodDecl>(() => TranslationUnit.GetOrCreate<CXXMethodDecl>(Handle.LambdaStaticInvoker));
88-
89-
_methods = new Lazy<IReadOnlyList<CXXMethodDecl>>(() => {
90-
var numMethods = Handle.NumMethods;
91-
var methods = new List<CXXMethodDecl>(numMethods);
92-
93-
for (var i = 0; i < numMethods; i++)
94-
{
95-
var method = TranslationUnit.GetOrCreate<CXXMethodDecl>(Handle.GetMethod(unchecked((uint)i)));
96-
methods.Add(method);
97-
}
98-
99-
return methods;
100-
});
101-
51+
_methods = LazyList.Create<CXXMethodDecl>(Handle.NumMethods, (i) => TranslationUnit.GetOrCreate<CXXMethodDecl>(Handle.GetMethod(unchecked((uint)i))));
10252
_templateInstantiationPattern = new Lazy<CXXRecordDecl>(() => TranslationUnit.GetOrCreate<CXXRecordDecl>(Handle.TemplateInstantiationPattern));
103-
104-
_vbases = new Lazy<IReadOnlyList<CXXBaseSpecifier>>(() => {
105-
var numVBases = Handle.NumVBases;
106-
var vbases = new List<CXXBaseSpecifier>(numVBases);
107-
108-
for (var i = 0; i < numVBases; i++)
109-
{
110-
var vbase = TranslationUnit.GetOrCreate<CXXBaseSpecifier>(Handle.GetVBase(unchecked((uint)i)));
111-
vbases.Add(vbase);
112-
}
113-
114-
return vbases;
115-
});
53+
_vbases = LazyList.Create<CXXBaseSpecifier>(Handle.NumVBases, (i) => TranslationUnit.GetOrCreate<CXXBaseSpecifier>(Handle.GetVBase(unchecked((uint)i))));
11654
}
11755

11856
public bool IsAbstract => Handle.CXXRecord_IsAbstract;
11957

120-
public IReadOnlyList<CXXBaseSpecifier> Bases => _bases.Value;
58+
public IReadOnlyList<CXXBaseSpecifier> Bases => _bases;
12159

12260
public new CXXRecordDecl CanonicalDecl => (CXXRecordDecl)base.CanonicalDecl;
12361

124-
public IReadOnlyList<CXXConstructorDecl> Ctors => _ctors.Value;
62+
public IReadOnlyList<CXXConstructorDecl> Ctors => _ctors;
12563

12664
public new CXXRecordDecl? Definition => (CXXRecordDecl?)base.Definition;
12765

@@ -131,7 +69,7 @@ private protected CXXRecordDecl(CXCursor handle, CXCursorKind expectedCursorKind
13169

13270
public CXXDestructorDecl? Destructor => _destructor.Value;
13371

134-
public IReadOnlyList<FriendDecl> Friends => _friends.Value;
72+
public IReadOnlyList<FriendDecl> Friends => _friends;
13573

13674
public bool HasDefinition => Definition is not null;
13775

@@ -159,7 +97,7 @@ private protected CXXRecordDecl(CXCursor handle, CXCursorKind expectedCursorKind
15997

16098
public CXXMethodDecl LambdaStaticInvoker => _lambdaStaticInvoker.Value;
16199

162-
public IReadOnlyList<CXXMethodDecl> Methods => _methods.Value;
100+
public IReadOnlyList<CXXMethodDecl> Methods => _methods;
163101

164102
public new CXXRecordDecl MostRecentDecl => (CXXRecordDecl)base.MostRecentDecl;
165103

@@ -189,5 +127,5 @@ public CXXRecordDecl MostRecentNonInjectedDecl
189127

190128
public CXXRecordDecl TemplateInstantiationPattern => _templateInstantiationPattern.Value;
191129

192-
public IReadOnlyList<CXXBaseSpecifier> VBases => _vbases.Value;
130+
public IReadOnlyList<CXXBaseSpecifier> VBases => _vbases;
193131
}
Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,22 @@
11
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
22

3-
using ClangSharp.Interop;
4-
using static ClangSharp.Interop.CXCursorKind;
5-
using static ClangSharp.Interop.CX_DeclKind;
63
using System;
74
using System.Collections.Generic;
5+
using ClangSharp.Interop;
6+
using static ClangSharp.Interop.CX_DeclKind;
7+
using static ClangSharp.Interop.CXCursorKind;
88

99
namespace ClangSharp;
1010

1111
public sealed class CapturedDecl : Decl, IDeclContext
1212
{
1313
private readonly Lazy<ImplicitParamDecl> _contextParam;
14-
private readonly Lazy<IReadOnlyList<ImplicitParamDecl>> _parameters;
14+
private readonly LazyList<ImplicitParamDecl> _parameters;
1515

1616
internal CapturedDecl(CXCursor handle) : base(handle, CXCursor_UnexposedDecl, CX_DeclKind_Captured)
1717
{
1818
_contextParam = new Lazy<ImplicitParamDecl>(() => TranslationUnit.GetOrCreate<ImplicitParamDecl>(Handle.ContextParam));
19-
_parameters = new Lazy<IReadOnlyList<ImplicitParamDecl>>(() => {
20-
var parameterCount = Handle.NumArguments;
21-
var parameters = new List<ImplicitParamDecl>(parameterCount);
22-
23-
for (var i = 0; i < parameterCount; i++)
24-
{
25-
var parameter = TranslationUnit.GetOrCreate<ImplicitParamDecl>(Handle.GetArgument(unchecked((uint)i)));
26-
parameters.Add(parameter);
27-
}
28-
29-
return parameters;
30-
});
19+
_parameters = LazyList.Create<ImplicitParamDecl>(Handle.NumArguments, (i) => TranslationUnit.GetOrCreate<ImplicitParamDecl>(Handle.GetArgument(unchecked((uint)i))));
3120
}
3221

3322
public ImplicitParamDecl ContextParam => _contextParam.Value;
@@ -38,5 +27,5 @@ internal CapturedDecl(CXCursor handle) : base(handle, CXCursor_UnexposedDecl, CX
3827

3928
public uint NumParams => unchecked((uint)Handle.NumArguments);
4029

41-
public IReadOnlyList<ImplicitParamDecl> Parameters => _parameters.Value;
30+
public IReadOnlyList<ImplicitParamDecl> Parameters => _parameters;
4231
}

0 commit comments

Comments
 (0)