Skip to content

Speed up the loading of Imperator pops #2007

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions ImperatorToCK3/Imperator/Pops/Pop.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
using commonItems.Collections;
using commonItems;
using commonItems.Collections;

namespace ImperatorToCK3.Imperator.Pops;

public partial class Pop : IIdentifiable<ulong> {
public class Pop : IIdentifiable<ulong> {
public ulong Id { get; } = 0;
public string Type { get; set; } = "";
public string Culture { get; set; } = "";
public string Religion { get; set; } = "";
public Pop(ulong id) {
Id = id;
}

public static Pop Parse(string idString, BufferedReader reader) {
var newPop = new Pop(ulong.Parse(idString));

var parser = new Parser();
parser.RegisterKeyword("type", r => newPop.Type = r.GetString());
parser.RegisterKeyword("culture", r => newPop.Culture = r.GetString());
parser.RegisterKeyword("religion", r => newPop.Religion = r.GetString());
parser.RegisterRegex(CommonRegexes.Catchall, ParserHelpers.IgnoreAndLogItem);
parser.ParseStream(reader);

return newPop;
}
}
54 changes: 39 additions & 15 deletions ImperatorToCK3/Imperator/Pops/PopCollection.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,54 @@
using commonItems;
using commonItems.Collections;
using System.Collections.Generic;
using System.Threading.Channels;
using System.Threading.Tasks;

namespace ImperatorToCK3.Imperator.Pops;

public sealed class PopCollection : IdObjectCollection<ulong, Pop> {
public sealed class PopCollection : ConcurrentIdObjectCollection<ulong, Pop> {
public void LoadPopsFromBloc(BufferedReader blocReader) {
var blocParser = new Parser();
blocParser.RegisterKeyword("population", LoadPops);
blocParser.IgnoreAndLogUnregisteredItems();
blocParser.ParseStream(blocReader);
}

public void LoadPops(BufferedReader reader) {
var parser = new Parser();
RegisterKeys(parser);
parser.ParseStream(reader);
}
private void RegisterKeys(Parser parser) {
parser.RegisterRegex(CommonRegexes.Integer, (reader, thePopId) => {
var popStr = reader.GetStringOfItem().ToString();
if (!popStr.Contains('{')) {
return;
}
var tempStream = new BufferedReader(popStr);
var pop = Pop.Parse(thePopId, tempStream);
Add(pop);
// Load pops using the producer-consumer pattern.

var channel = Channel.CreateUnbounded<KeyValuePair<string, string>>();
var channelWriter = channel.Writer;
var channelReader = channel.Reader;

var producerTask = Task.Run(() => {
var parser = new Parser();
parser.RegisterRegex(CommonRegexes.Integer, (popReader, thePopId) => {
var popStr = popReader.GetStringOfItem().ToString();
if (!popStr.Contains('{')) {
return;
}

if (!channelWriter.TryWrite(new(thePopId, popStr))) {
Logger.Warn($"Failed to enqueue pop {thePopId} for processing.");
}
});
parser.RegisterRegex(CommonRegexes.Catchall, ParserHelpers.IgnoreAndLogItem);
parser.ParseStream(reader);

channelWriter.Complete();
});
parser.RegisterRegex(CommonRegexes.Catchall, ParserHelpers.IgnoreAndLogItem);

var consumerTasks = new List<Task>();
for (var i = 0; i < 5; ++i) {
consumerTasks.Add(Task.Run(async () => {
await foreach (var (popIdStr, popDataStr) in channelReader.ReadAllAsync()) {
var pop = Pop.Parse(popIdStr, new BufferedReader(popDataStr));
Add(pop);
}
}));
}

Task.WaitAll(producerTask, Task.WhenAll(consumerTasks));
}
}
20 changes: 0 additions & 20 deletions ImperatorToCK3/Imperator/Pops/PopFactory.cs

This file was deleted.

Loading