optimize movie recording loop a bit, do not create a new instance of log entry generator every frame, avoids a lot of memory allocations with mnemonic lookups that do not change for the lifetime of the movie, also a slight optikmization in Bk2 controller, co-authored iwth morilli

This commit is contained in:
adelikat 2023-05-24 11:18:42 -05:00
parent 9daac2c26f
commit 05f8747321
3 changed files with 22 additions and 4 deletions

View File

@ -55,8 +55,9 @@ namespace BizHawk.Client.Common
public void SetFrom(IController source)
{
foreach (var button in Definition.BoolButtons)
for (int index = 0; index < Definition.BoolButtons.Count; index++)
{
string button = Definition.BoolButtons[index];
_myBoolButtons[button] = source.IsPressed(button);
}

View File

@ -6,15 +6,25 @@ using BizHawk.Emulation.Common;
namespace BizHawk.Client.Common
{
// Designed to be able to last the lifetime of an IMovie
internal class Bk2LogEntryGenerator : ILogEntryGenerator
{
private readonly string _systemId;
private readonly IController _source;
private readonly Dictionary<string, char> _mnemonics = new();
public Bk2LogEntryGenerator(string systemId, IController source)
{
_systemId = systemId;
_source = source;
foreach (var group in _source.Definition.ControlsOrdered.Where(static c => c.Count is not 0))
{
foreach (var button in group)
{
_mnemonics.Add(button, Bk2MnemonicLookup.Lookup(button, _systemId));
}
}
}
public bool IsEmpty => EmptyEntry == GenerateLogEntry();
@ -76,7 +86,7 @@ namespace BizHawk.Client.Common
else if (_source.Definition.BoolButtons.Contains(button))
{
sb.Append(!createEmpty && _source.IsPressed(button)
? Bk2MnemonicLookup.Lookup(button, _systemId)
? _mnemonics[button]
: '.');
}
}

View File

@ -6,7 +6,7 @@ namespace BizHawk.Client.Common
public partial class Bk2Movie : BasicMovieInfo, IMovie
{
private Bk2Controller _adapter;
private Bk2LogEntryGenerator _logGenerator;
public Bk2Movie(IMovieSession session, string filename) : base(filename)
{
Session = session;
@ -34,7 +34,14 @@ namespace BizHawk.Client.Common
public ILogEntryGenerator LogGeneratorInstance(IController source)
{
return new Bk2LogEntryGenerator(Emulator?.SystemId ?? SystemID, source);
// Hack because initial movie loading is a mess, and you will immediate create a file with an undefined controller
if (!source.Definition.Any())
{
return new Bk2LogEntryGenerator(Emulator?.SystemId ?? SystemID, source);
}
_logGenerator ??= new Bk2LogEntryGenerator(Emulator?.SystemId ?? SystemID, source);
return _logGenerator;
}
public override int FrameCount => Log.Count;