Skip to content

Commit aec0927

Browse files
authored
More optimizations (#3030)
2 parents 30a9d2b + 6e55a06 commit aec0927

10 files changed

Lines changed: 68 additions & 47 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,5 @@ validate-with-ck3-tiger_wtwsms.ps1
4747
previous-problem-count.txt
4848

4949
# Benchmark stuff
50-
/ImperatorToCK3.Benchmarks/
5150
/BenchmarkDotNet.Artifacts/
5251
/BenchmarkSuite1/
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/bin/
2+
/obj/
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<LangVersion>latest</LangVersion>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="BenchmarkDotNet" Version="0.15.6" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\ImperatorToCK3\ImperatorToCK3.csproj" />
15+
</ItemGroup>
16+
</Project>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using BenchmarkDotNet.Running;
2+
3+
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);

ImperatorToCK3/CK3/Characters/CharacterCollection.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -284,24 +284,24 @@ private static Date GetEstimatedMarriageDate(Imperator.Characters.Character impe
284284
}
285285

286286
private static Date? GetBirthDateOfFirstCommonChild(Imperator.Characters.Character father, Imperator.Characters.Character mother) {
287-
var fatherChildren = father.Children.Values;
288-
var motherChildren = mother.Children.Values;
289-
var largerCollection = fatherChildren.Count >= motherChildren.Count ? fatherChildren : motherChildren;
290-
var smallerCollection = fatherChildren.Count < motherChildren.Count ? fatherChildren : motherChildren;
291-
var childSet = new HashSet<Imperator.Characters.Character>(largerCollection);
292-
293287
Date? firstChildBirthDate = null;
294-
foreach (var child in smallerCollection) {
295-
if (!childSet.Contains(child)) {
296-
continue;
288+
289+
if (father.Children.Count > 0 && mother.Children.Count > 0) {
290+
var smallerCollection = father.Children.Count <= mother.Children.Count ? father.Children : mother.Children;
291+
var largerCollection = father.Children.Count > mother.Children.Count ? father.Children : mother.Children;
292+
293+
foreach (var (childId, child) in smallerCollection) {
294+
if (!largerCollection.ContainsKey(childId)) {
295+
continue;
296+
}
297+
if (firstChildBirthDate is null || child.BirthDate < firstChildBirthDate) {
298+
firstChildBirthDate = child.BirthDate;
299+
}
297300
}
298-
if (firstChildBirthDate is null || child.BirthDate < firstChildBirthDate) {
299-
firstChildBirthDate = child.BirthDate;
301+
if (firstChildBirthDate is not null) {
302+
return firstChildBirthDate;
300303
}
301304
}
302-
if (firstChildBirthDate is not null) {
303-
return firstChildBirthDate;
304-
}
305305

306306
foreach (var unborn in mother.Unborns) {
307307
if (unborn.FatherId != father.Id) {

ImperatorToCK3/CK3/Characters/DNA.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Linq;
32
using System.Runtime.InteropServices;
43
using System.Text;
54

@@ -19,21 +18,17 @@ internal PaletteCoordinates(ushort x, ushort y) {
1918
}
2019

2120
public string Id { get; }
22-
21+
2322
private readonly Dictionary<string, DNAColorGeneValue> colorDNAValues;
2423
private readonly Dictionary<string, DNAGeneValue> morphDNAValues;
2524
private readonly Dictionary<string, DNAAccessoryGeneValue> accessoryDNAValues;
2625
public IReadOnlyDictionary<string, DNAAccessoryGeneValue> AccessoryDNAValues => accessoryDNAValues;
2726

2827
public IEnumerable<string> DNALines {
2928
get {
30-
var colorLines = colorDNAValues
31-
.Select(kvp => $"{kvp.Key}={{ {kvp.Value} }}");
32-
var morphGeneLines = morphDNAValues
33-
.Select(kvp => $"{kvp.Key}={{ {kvp.Value} }}");
34-
var accessoryGeneLines = accessoryDNAValues
35-
.Select(kvp => $"{kvp.Key}={{ {kvp.Value} }}");
36-
return colorLines.Concat(morphGeneLines).Concat(accessoryGeneLines);
29+
foreach (var kvp in colorDNAValues) yield return $"{kvp.Key}={{ {kvp.Value} }}";
30+
foreach (var kvp in morphDNAValues) yield return $"{kvp.Key}={{ {kvp.Value} }}";
31+
foreach (var kvp in accessoryDNAValues) yield return $"{kvp.Key}={{ {kvp.Value} }}";
3732
}
3833
}
3934

@@ -52,9 +47,12 @@ Dictionary<string, DNAAccessoryGeneValue> accessoryDNAValues
5247
public void WriteGenes(StringBuilder sb) {
5348
sb.AppendLine("\t\tgenes={");
5449

55-
foreach (var dnaLine in DNALines) {
56-
sb.AppendLine($"\t\t\t{dnaLine}");
57-
}
50+
foreach (var kvp in colorDNAValues)
51+
sb.Append("\t\t\t").Append(kvp.Key).Append("={ ").Append(kvp.Value).AppendLine(" }");
52+
foreach (var kvp in morphDNAValues)
53+
sb.Append("\t\t\t").Append(kvp.Key).Append("={ ").Append(kvp.Value).AppendLine(" }");
54+
foreach (var kvp in accessoryDNAValues)
55+
sb.Append("\t\t\t").Append(kvp.Key).Append("={ ").Append(kvp.Value).AppendLine(" }");
5856

5957
sb.AppendLine("\t\t}");
6058
}

ImperatorToCK3/CK3/Provinces/Province.cs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,13 @@ internal sealed partial class Province : IIdentifiable<ulong> {
1818
public ulong? BaseProvinceId { get; private set; }
1919

2020
public ImperatorProvince? PrimaryImperatorProvince { get; set; } = null;
21-
private readonly OrderedSet<ImperatorProvince> secondaryImperatorProvinces = new();
22-
public IImmutableSet<ImperatorProvince> SecondaryImperatorProvinces => secondaryImperatorProvinces
23-
.ToImmutableHashSet();
24-
public IImmutableSet<ImperatorProvince> ImperatorProvinces {
25-
get {
26-
IEnumerable<ImperatorProvince> toReturn = secondaryImperatorProvinces;
27-
if (PrimaryImperatorProvince is not null) {
28-
toReturn = toReturn.Append(PrimaryImperatorProvince);
29-
}
21+
private readonly OrderedSet<ImperatorProvince> secondaryImperatorProvinces = [];
22+
public IReadOnlySet<ImperatorProvince> SecondaryImperatorProvinces => secondaryImperatorProvinces;
3023

31-
return toReturn.ToImmutableHashSet();
32-
}
24+
public ImmutableHashSet<ImperatorProvince> ImperatorProvinces {
25+
get => field ??= (PrimaryImperatorProvince is null
26+
? [.. secondaryImperatorProvinces]
27+
: [PrimaryImperatorProvince, .. secondaryImperatorProvinces]);
3328
}
3429

3530
public Province(ulong id) {
@@ -330,9 +325,6 @@ private void SetHoldingFromImperator(Title.LandedTitles landedTitles) {
330325
}
331326

332327
public bool IsCountyCapital(Title.LandedTitles landedTitles) {
333-
var capitalProvIds = landedTitles
334-
.Where(t => t.CapitalBaronyProvinceId is not null)
335-
.Select(t => (ulong)t.CapitalBaronyProvinceId!);
336-
return capitalProvIds.Contains(Id);
328+
return landedTitles.CapitalBaronyProvinceIds.Contains(Id);
337329
}
338330
}

ImperatorToCK3/CK3/Religions/ReligionCollection.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace ImperatorToCK3.CK3.Religions;
1919

2020
internal sealed class ReligionCollection(Title.LandedTitles landedTitles) : IdObjectCollection<string, Religion> {
2121
private readonly Dictionary<string, OrderedSet<string>> replaceableHolySitesByFaith = [];
22+
private Dictionary<string, Faith>? faithCache;
2223
public IReadOnlyDictionary<string, OrderedSet<string>> ReplaceableHolySitesByFaith => replaceableHolySitesByFaith;
2324
public IdObjectCollection<string, HolySite> HolySites { get; } = [];
2425
public IdObjectCollection<string, DoctrineCategory> DoctrineCategories { get; } = [];
@@ -30,6 +31,7 @@ public IEnumerable<Faith> Faiths {
3031
}
3132

3233
public void LoadReligions(ModFilesystem ck3ModFS, ColorFactory colorFactory) {
34+
faithCache = null;
3335
var parser = new Parser();
3436
parser.RegisterRegex(CommonRegexes.String, (religionReader, religionId) => {
3537
var religion = new Religion(religionId, religionReader, this, colorFactory);
@@ -40,6 +42,7 @@ public void LoadReligions(ModFilesystem ck3ModFS, ColorFactory colorFactory) {
4042
}
4143

4244
public void LoadConverterFaiths(string converterFaithsPath, ColorFactory colorFactory, Hash liquidVariables) {
45+
faithCache = null;
4346
OrderedSet<Faith> loadedConverterFaiths = [];
4447

4548
var parser = new Parser();
@@ -228,13 +231,16 @@ public void LoadDoctrines(ModFilesystem ck3ModFS) {
228231
}
229232

230233
public Faith? GetFaith(string id) {
231-
foreach (Religion religion in this) {
232-
if (religion.Faiths.TryGetValue(id, out var faith)) {
233-
return faith;
234+
if (faithCache is null) {
235+
faithCache = [];
236+
foreach (var religion in this) {
237+
foreach (var faith in religion.Faiths) {
238+
faithCache[faith.Id] = faith;
239+
}
234240
}
235241
}
236242

237-
return null;
243+
return faithCache.TryGetValue(id, out var result) ? result : null;
238244
}
239245

240246
private Title? GetHolySiteBarony(HolySite holySite) {

ImperatorToCK3/CK3/Titles/LandedTitles.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,12 @@ internal sealed partial class Title {
4040
// Since titles are nested according to hierarchy we do this recursively.
4141
internal sealed class LandedTitles : TitleCollection {
4242
public Dictionary<string, object> Variables { get; } = [];
43-
43+
4444
public IEnumerable<Title> Counties => this.Where(t => t.Rank == TitleRank.county);
45+
public FrozenSet<ulong> CapitalBaronyProvinceIds => field ??= this
46+
.Where(t => t.CapitalBaronyProvinceId.HasValue)
47+
.Select(t => t.CapitalBaronyProvinceId!.Value)
48+
.ToFrozenSet();
4549

4650
public void LoadTitles(ModFilesystem ck3ModFS, CK3LocDB ck3LocDB, ColorFactory colorFactory) {
4751
Logger.Info("Loading landed titles...");

ImperatorToCK3/ImperatorToCK3.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114

115115
<ItemGroup>
116116
<InternalsVisibleTo Include="ImperatorToCK3.UnitTests" />
117+
<InternalsVisibleTo Include="ImperatorToCK3.Benchmarks" />
117118
</ItemGroup>
118119

119120
<ItemGroup>

0 commit comments

Comments
 (0)