Skip to content

Commit 0541960

Browse files
committed
Cache deserialized ComProjects in the tests instead of declarations
Previously, we statucally cached serialized declarations we loadedfrom file in tests to avoid loading several MB of text files multiple times in different tests. This is no longer sensible because we derserialize ComProjects now and generate the declarations based on them.
1 parent de6f994 commit 0541960

8 files changed

+65
-49
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Rubberduck.VBEditor;
2+
3+
namespace Rubberduck.Parsing.ComReflection
4+
{
5+
public interface IComProjectDeserializer
6+
{
7+
ComProject DeserializeProject(ReferenceInfo reference);
8+
bool SerializedVersionExists(ReferenceInfo reference);
9+
}
10+
}
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
using Rubberduck.VBEditor;
2-
3-
namespace Rubberduck.Parsing.ComReflection
1+
namespace Rubberduck.Parsing.ComReflection
42
{
5-
public interface IComProjectSerializationProvider
3+
public interface IComProjectSerializationProvider : IComProjectDeserializer
64
{
75
string Target { get; }
86
void SerializeProject(ComProject project);
9-
ComProject DeserializeProject(ReferenceInfo reference);
10-
bool SerializedVersionExists(ReferenceInfo reference);
117
}
128
}

Rubberduck.Parsing/ComReflection/SerializedReferencedDeclarationsCollector.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ namespace Rubberduck.Parsing.ComReflection
66
{
77
public class SerializedReferencedDeclarationsCollector : ReferencedDeclarationsCollectorBase
88
{
9-
private readonly IComProjectSerializationProvider _serializer;
9+
private readonly IComProjectDeserializer _deserializer;
1010

11-
public SerializedReferencedDeclarationsCollector(string serializedDeclarationsPath = null)
11+
public SerializedReferencedDeclarationsCollector(IComProjectDeserializer deserializer)
1212
{
13-
_serializer = new XmlComProjectSerializer(serializedDeclarationsPath);
13+
_deserializer = deserializer;
1414
}
1515

1616
public override IReadOnlyCollection<Declaration> CollectedDeclarations(ReferenceInfo reference)
1717
{
18-
if (!_serializer.SerializedVersionExists(reference))
18+
if (!_deserializer.SerializedVersionExists(reference))
1919
{
2020
return new List<Declaration>();
2121
}
@@ -25,7 +25,7 @@ public override IReadOnlyCollection<Declaration> CollectedDeclarations(Reference
2525

2626
private IReadOnlyCollection<Declaration> LoadDeclarationsFromProvider(ReferenceInfo reference)
2727
{
28-
var type = _serializer.DeserializeProject(reference);
28+
var type = _deserializer.DeserializeProject(reference);
2929
return LoadDeclarationsFromComProject(type);
3030
}
3131
}

Rubberduck.Parsing/Rubberduck.Parsing.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
<Compile Include="Common\ParsingStageTimer.cs" />
149149
<Compile Include="ComReflection\ComLibraryProvider.cs" />
150150
<Compile Include="ComReflection\IComLibraryProvider.cs" />
151+
<Compile Include="ComReflection\IComProjectDeserializer.cs" />
151152
<Compile Include="ComReflection\IComProjectSerializationProvider.cs" />
152153
<Compile Include="ComReflection\LibraryReferencedDeclarationsCollector.cs" />
153154
<Compile Include="ComReflection\ReferencedDeclarationsCollector.cs" />

RubberduckTests/Mocks/MockParser.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ public static SynchronousParseCoordinator Create(IVBE vbe, RubberduckParserState
7272
var supertypeClearer = new SynchronousSupertypeClearer(state);
7373
var parserStateManager = new SynchronousParserStateManager(state);
7474
var referenceRemover = new SynchronousReferenceRemover(state, moduleToModuleReferenceManager);
75-
var baseReferencedDeclarationsCollector = new SerializedReferencedDeclarationsCollector(path);
76-
var referencedDeclarationsCollector = new StaticCachingReferencedDeclarationsCollectorDecorator(baseReferencedDeclarationsCollector);
75+
var baseComDeserializer = new XmlComProjectSerializer(path);
76+
var comDeserializer = new StaticCachingComDeserializerDecorator(baseComDeserializer);
77+
var referencedDeclarationsCollector = new SerializedReferencedDeclarationsCollector(comDeserializer);
7778
var comSynchronizer = new SynchronousCOMReferenceSynchronizer(
7879
state,
7980
parserStateManager,
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Collections.Generic;
2+
using Rubberduck.Parsing.ComReflection;
3+
using Rubberduck.VBEditor;
4+
5+
namespace RubberduckTests.Mocks
6+
{
7+
public class StaticCachingComDeserializerDecorator : IComProjectDeserializer
8+
{
9+
private static readonly Dictionary<ReferenceInfo, ComProject> CachedReferences = new Dictionary<ReferenceInfo, ComProject>();
10+
11+
private static readonly object _lockObject = new object();
12+
13+
private IComProjectDeserializer _baseDeserializer;
14+
15+
public StaticCachingComDeserializerDecorator(IComProjectDeserializer baseDeserializer)
16+
{
17+
_baseDeserializer = baseDeserializer;
18+
}
19+
20+
public bool SerializedVersionExists(ReferenceInfo reference)
21+
{
22+
lock(_lockObject)
23+
{
24+
return CachedReferences.TryGetValue(reference, out _) || _baseDeserializer.SerializedVersionExists(reference);
25+
}
26+
}
27+
28+
public ComProject DeserializeProject(ReferenceInfo reference)
29+
{
30+
lock (_lockObject)
31+
{
32+
if (CachedReferences.TryGetValue(reference, out var cachedProject))
33+
{
34+
return cachedProject;
35+
}
36+
37+
var comProject = _baseDeserializer.DeserializeProject(reference);
38+
CachedReferences[reference] = comProject;
39+
return comProject;
40+
}
41+
}
42+
}
43+
}

RubberduckTests/Mocks/StaticCachingReferencedDeclarationsCollectorDecorator.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

RubberduckTests/RubberduckTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
<Compile Include="CodeStringExtensions.cs" />
130130
<Compile Include="Mocks\MockVbeEvents.cs" />
131131
<Compile Include="Inspections\SheetAccessedUsingStringInspectionTests.cs" />
132-
<Compile Include="Mocks\StaticCachingReferencedDeclarationsCollectorDecorator.cs" />
132+
<Compile Include="Mocks\StaticCachingComDeserializerDecorator.cs" />
133133
<Compile Include="ParserState\ParserStateTests.cs" />
134134
<Compile Include="Parsing\Coordination\TokenStreamParserTests.cs" />
135135
<Compile Include="Parsing\VBACodeStringParserTests.cs" />

0 commit comments

Comments
 (0)