Skip to content

Commit 92af06f

Browse files
authored
chore: Skip unnecessary work for empty or null sequences (#1601)
1 parent dc28b26 commit 92af06f

File tree

5 files changed

+50
-10
lines changed

5 files changed

+50
-10
lines changed

Testcontainers.slnx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
<Project Path="src/Testcontainers.DynamoDb/Testcontainers.DynamoDb.csproj"/>
2727
<Project Path="src/Testcontainers.Elasticsearch/Testcontainers.Elasticsearch.csproj"/>
2828
<Project Path="src/Testcontainers.EventHubs/Testcontainers.EventHubs.csproj"/>
29-
<Project Path="src/Testcontainers.EventStoreDb/Testcontainers.EventStoreDb.csproj"/>
3029
<Project Path="src/Testcontainers.FakeGcsServer/Testcontainers.FakeGcsServer.csproj"/>
3130
<Project Path="src/Testcontainers.FirebirdSql/Testcontainers.FirebirdSql.csproj"/>
3231
<Project Path="src/Testcontainers.Firestore/Testcontainers.Firestore.csproj"/>

src/Testcontainers/Builders/BuildConfiguration.cs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,29 @@ public static IEnumerable<T> Combine<T>(
4242
return Array.Empty<T>();
4343
}
4444

45-
if (newValue == null || oldValue == null)
45+
if (newValue == null)
4646
{
47-
return newValue ?? oldValue;
47+
return oldValue;
48+
}
49+
50+
if (oldValue == null)
51+
{
52+
return newValue;
53+
}
54+
55+
if (newValue is ICollection<T> collection && collection.Count == 0)
56+
{
57+
return oldValue;
58+
}
59+
60+
if (oldValue is not ICollection<T>)
61+
{
62+
oldValue = oldValue.ToArray();
63+
}
64+
65+
if (newValue is not ICollection<T>)
66+
{
67+
newValue = newValue.ToArray();
4868
}
4969

5070
return oldValue.Concat(newValue).ToArray();
@@ -68,9 +88,19 @@ public static IReadOnlyList<T> Combine<T>(
6888
return Array.Empty<T>();
6989
}
7090

71-
if (newValue == null || oldValue == null)
91+
if (newValue == null)
7292
{
73-
return newValue ?? oldValue;
93+
return oldValue;
94+
}
95+
96+
if (oldValue == null)
97+
{
98+
return newValue;
99+
}
100+
101+
if (newValue.Count == 0)
102+
{
103+
return oldValue;
74104
}
75105

76106
return oldValue.Concat(newValue).ToArray();
@@ -128,9 +158,19 @@ public static IReadOnlyDictionary<TKey, TValue> Combine<TKey, TValue>(
128158
return new ReadOnlyDictionary<TKey, TValue>(new Dictionary<TKey, TValue>());
129159
}
130160

131-
if (newValue == null || oldValue == null)
161+
if (newValue == null)
132162
{
133-
return newValue ?? oldValue;
163+
return oldValue;
164+
}
165+
166+
if (oldValue == null)
167+
{
168+
return newValue;
169+
}
170+
171+
if (newValue.Count == 0)
172+
{
173+
return oldValue;
134174
}
135175

136176
var result = new Dictionary<TKey, TValue>(oldValue.Count + newValue.Count);

src/Testcontainers/Configurations/Containers/ContainerConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class ContainerConfiguration : ResourceConfiguration<CreateContainerParam
3939
/// <param name="outputConsumer">The output consumer.</param>
4040
/// <param name="waitStrategies">The wait strategies.</param>
4141
/// <param name="startupCallback">The startup callback.</param>
42+
/// <param name="connectionStringProvider">The connection string provider.</param>
4243
/// <param name="autoRemove">A value indicating whether Docker removes the container after it exits or not.</param>
4344
/// <param name="privileged">A value indicating whether the privileged flag is set or not.</param>
4445
public ContainerConfiguration(

tests/Testcontainers.Commons/DockerfileParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public Dictionary<string, string> Parse()
3838
var stages = fromMatches
3939
.Select(match => new
4040
{
41-
Stage = match.Value.Split(separator, options).Skip(1).DefaultIfEmpty(string.Empty).First(),
41+
Stage = match.Value.Split(separator, options).Skip(1).FirstOrDefault(string.Empty),
4242
Image = match.Groups[imageGroup].Value,
4343
})
4444
.ToDictionary(

tests/Testcontainers.Commons/TestSession.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace DotNet.Testcontainers.Commons;
33
[PublicAPI]
44
public static class TestSession
55
{
6-
private static readonly ConcurrentDictionary<string, Dictionary<string, string>> Cache = new ConcurrentDictionary<string, Dictionary<string, string>>();
6+
private static readonly ConcurrentDictionary<string, Dictionary<string, string>> Stages = new ConcurrentDictionary<string, Dictionary<string, string>>();
77

88
public static readonly string TempDirectoryPath = Path.Combine(Path.GetTempPath(), "testcontainers-tests", Guid.NewGuid().ToString("D"));
99

@@ -16,7 +16,7 @@ public static string GetImageFromDockerfile(string relativeFilePath = "Dockerfil
1616
{
1717
var absoluteFilePath = Path.GetFullPath(relativeFilePath);
1818

19-
var stages = Cache.GetOrAdd(absoluteFilePath, filePath => new DockerfileParser(filePath).Parse());
19+
var stages = Stages.GetOrAdd(absoluteFilePath, filePath => new DockerfileParser(filePath).Parse());
2020

2121
if (stages.TryGetValue(stage, out var image))
2222
{

0 commit comments

Comments
 (0)