From 0089bde117c42a6ca2ff622ebade3f401b611e02 Mon Sep 17 00:00:00 2001 From: Adrian Conlon <98398945+AdrianConlon@users.noreply.github.com> Date: Thu, 6 Jun 2024 21:30:49 +0100 Subject: [PATCH] Switch entirely to fast lookups. Around 10 times faster than original code! --- M6502/M6502.Symbols/File.cs | 12 +++++++--- M6502/M6502.Symbols/IdentifiableSection.cs | 11 +-------- M6502/M6502.Symbols/Line.cs | 13 +++++++---- M6502/M6502.Symbols/NamedSection.cs | 10 +-------- M6502/M6502.Symbols/Parser.cs | 26 +++++----------------- M6502/M6502.Symbols/Scope.cs | 19 ++-------------- M6502/M6502.Symbols/Segment.cs | 14 +++++++++--- M6502/M6502.Symbols/Span.cs | 11 +++++++-- M6502/M6502.Symbols/Symbol.cs | 17 +++++--------- 9 files changed, 53 insertions(+), 80 deletions(-) diff --git a/M6502/M6502.Symbols/File.cs b/M6502/M6502.Symbols/File.cs index f16c8bc..ef035e6 100644 --- a/M6502/M6502.Symbols/File.cs +++ b/M6502/M6502.Symbols/File.cs @@ -7,10 +7,9 @@ // file id=0,name="sudoku.s",size=9141,mtime=0x6027C7F0,mod=0 public class File : NamedSection { - public int Size => this.TakeInteger("size"); - - public long ModificationTime => this.TakeLong("mtime"); + public int Size { get; private set; } + public long ModificationTime { get; private set; } public Symbols.Module Module => this.TakeModuleReference(); public File() @@ -19,6 +18,13 @@ _ = this._hex_long_keys.Add("mtime"); _ = this._integer_keys.Add("mod"); } + + public override void Parse(Parser parent, Dictionary entries) + { + base.Parse(parent, entries); + this.Size = this.TakeInteger("size"); + this.ModificationTime = this.TakeLong("mtime"); + } } } } diff --git a/M6502/M6502.Symbols/IdentifiableSection.cs b/M6502/M6502.Symbols/IdentifiableSection.cs index 58ce649..e425016 100644 --- a/M6502/M6502.Symbols/IdentifiableSection.cs +++ b/M6502/M6502.Symbols/IdentifiableSection.cs @@ -1,6 +1,4 @@ -#define FAST_ID_LOOKUP - -namespace EightBit +namespace EightBit { namespace Files { @@ -8,21 +6,14 @@ namespace EightBit { public class IdentifiableSection : Section { -#if FAST_ID_LOOKUP public int ID { get; private set; } -#else - public int ID => this.TakeInteger("id"); -#endif - protected IdentifiableSection() => _ = this._integer_keys.Add("id"); -#if FAST_ID_LOOKUP public override void Parse(Parser parent, Dictionary entries) { base.Parse(parent, entries); this.ID = this.TakeInteger("id"); } -#endif #region Foreign key constraints diff --git a/M6502/M6502.Symbols/Line.cs b/M6502/M6502.Symbols/Line.cs index f92f6c2..383bd7e 100644 --- a/M6502/M6502.Symbols/Line.cs +++ b/M6502/M6502.Symbols/Line.cs @@ -9,12 +9,10 @@ { public Symbols.File File => this.TakeFileReference(); - public int LineNumber => this.TakeInteger("line"); - + public int LineNumber { get; private set; } public Symbols.Type Type => this.TakeTypeReference(); - public int Count => this.TakeInteger("count"); - + public int? Count { get; private set; } public List Spans => this.TakeSpanReferences(); public Line() @@ -25,6 +23,13 @@ _ = this._integer_keys.Add("count"); _ = this._multiple_keys.Add("span"); } + + public override void Parse(Parser parent, Dictionary entries) + { + base.Parse(parent, entries); + this.LineNumber = this.TakeInteger("line"); + this.Count = this.MaybeTakeInteger("count"); + } } } } diff --git a/M6502/M6502.Symbols/NamedSection.cs b/M6502/M6502.Symbols/NamedSection.cs index 651f8f7..a2b5927 100644 --- a/M6502/M6502.Symbols/NamedSection.cs +++ b/M6502/M6502.Symbols/NamedSection.cs @@ -1,6 +1,4 @@ -#define FAST_NAME_LOOKUP - -namespace EightBit +namespace EightBit { namespace Files { @@ -8,21 +6,15 @@ namespace EightBit { public class NamedSection : IdentifiableSection { -#if FAST_NAME_LOOKUP public string? Name { get; private set; } -#else - public string Name => this.TakeString("name"); -#endif protected NamedSection() => _ = this._string_keys.Add("name"); -#if FAST_NAME_LOOKUP public override void Parse(Parser parent, Dictionary entries) { base.Parse(parent, entries); this.Name = this.TakeString("name"); } -#endif } } } diff --git a/M6502/M6502.Symbols/Parser.cs b/M6502/M6502.Symbols/Parser.cs index 7011099..8ac4040 100644 --- a/M6502/M6502.Symbols/Parser.cs +++ b/M6502/M6502.Symbols/Parser.cs @@ -1,7 +1,4 @@ -#define BINARY_SCOPE_SEARCH -//#define LINEAR_SCOPE_SEARCH - -namespace EightBit +namespace EightBit { namespace Files { @@ -170,7 +167,6 @@ namespace EightBit private static bool AddressContained(int address, Scope scope) => AddressContained(address, AddressRange(scope)); -#if BINARY_SCOPE_SEARCH private int LocateScope(int address) { var low = 0; @@ -205,27 +201,14 @@ namespace EightBit // If we reach here, then element was not present return -1; } -#endif public Scope? LookupScope(int address) { -#if BINARY_SCOPE_SEARCH var index = this.LocateScope(address); return index == -1 ? null : this.AddressableScopes[index]; -#endif -#if LINEAR_SCOPE_SEARCH - foreach (var scope in this.AddressableScopes) - { - if (AddressContained(address, scope)) - { - return scope; - } - } - return null; -#endif } -#endregion + #endregion #region Scope evaluation @@ -253,7 +236,7 @@ namespace EightBit { var scope = scopes[i]; var name = scope.Name; - Debug.Assert(name.Length > 0); + Debug.Assert(!string.IsNullOrEmpty(name)); returned += name; var last = i == 0; if (!last) @@ -272,6 +255,7 @@ namespace EightBit } var prefix = BuildNamespace(symbol); var name = symbol.Name; + Debug.Assert(!string.IsNullOrEmpty(name)); return string.IsNullOrEmpty(prefix) ? name : $"{prefix}.{name}"; } @@ -297,7 +281,7 @@ namespace EightBit #endregion -#endregion + #endregion #region Metadata lookup diff --git a/M6502/M6502.Symbols/Scope.cs b/M6502/M6502.Symbols/Scope.cs index 146dde1..921dd95 100644 --- a/M6502/M6502.Symbols/Scope.cs +++ b/M6502/M6502.Symbols/Scope.cs @@ -1,7 +1,4 @@ -#define FAST_SYMBOL_LOOKUP -#define FAST_SIZE_LOOKUP - -namespace EightBit +namespace EightBit { namespace Files { @@ -13,18 +10,12 @@ namespace EightBit public class Scope : NamedSection { public Symbols.Module Module => this.TakeModuleReference(); - public string? Type => this.MaybeTakeString("type"); -#if FAST_SIZE_LOOKUP - public int Size; -#else - public int Size => this.TakeInteger("size"); -#endif + public int Size { get; private set; } public Scope? Parent => this.MaybeTakeParentReference(); -#if FAST_SYMBOL_LOOKUP private bool _symbolAvailable; private Symbols.Symbol? _symbol; public Symbols.Symbol? Symbol @@ -39,9 +30,6 @@ namespace EightBit return this._symbol; } } -#else - public Symbols.Symbol? Symbol => this.MaybeTakeSymbolReference(); -#endif public List Spans => this.TakeSpanReferences(); public Scope() @@ -54,14 +42,11 @@ namespace EightBit _ = this._multiple_keys.Add("span"); } -#if FAST_SIZE_LOOKUP public override void Parse(Parser parent, Dictionary entries) { base.Parse(parent, entries); this.Size = this.TakeInteger("size"); } -#endif - } } } diff --git a/M6502/M6502.Symbols/Segment.cs b/M6502/M6502.Symbols/Segment.cs index cd76274..edc2d21 100644 --- a/M6502/M6502.Symbols/Segment.cs +++ b/M6502/M6502.Symbols/Segment.cs @@ -7,12 +7,12 @@ // seg id=1,name="RODATA",start=0x00F471,size=0x0000,addrsize=absolute,type=ro,oname="sudoku.65b",ooffs=1137 public class Segment : NamedSection { - public int Start => this.TakeInteger("start"); - public int Size => this.TakeInteger("size"); + public int Start { get; private set; } + public int Size { get; private set; } public string AddressSize => this.TakeString("addrsize"); public string Type => this.TakeString("type"); public string OName => this.TakeString("oname"); - public int OOFFS => this.TakeInteger("ooffs"); // ?? Offsets, perhaps? + public int? OOFFS { get; private set; } // ?? Offsets, perhaps? public Segment() { @@ -23,6 +23,14 @@ _ = this._string_keys.Add("oname"); _ = this._integer_keys.Add("ooffs"); } + + public override void Parse(Parser parent, Dictionary entries) + { + base.Parse(parent, entries); + this.Start = this.TakeInteger("start"); + this.Size = this.TakeInteger("size"); + this.OOFFS = this.MaybeTakeInteger("ooffs"); + } } } } diff --git a/M6502/M6502.Symbols/Span.cs b/M6502/M6502.Symbols/Span.cs index b3bc0c2..39ae0d0 100644 --- a/M6502/M6502.Symbols/Span.cs +++ b/M6502/M6502.Symbols/Span.cs @@ -8,8 +8,8 @@ public class Span : IdentifiableSection { public Symbols.Segment Segment => this.TakeSegmentReference(); - public int Start => this.TakeInteger("start"); - public int Size => this.TakeInteger("size"); + public int Start { get; private set; } + public int Size { get; private set; } public Symbols.Type Type => this.TakeTypeReference(); public Span() @@ -19,6 +19,13 @@ _ = this._integer_keys.Add("size"); _ = this._integer_keys.Add("type"); } + + public override void Parse(Parser parent, Dictionary entries) + { + base.Parse(parent, entries); + this.Start = this.TakeInteger("start"); + this.Size = this.TakeInteger("size"); + } } } } diff --git a/M6502/M6502.Symbols/Symbol.cs b/M6502/M6502.Symbols/Symbol.cs index b543171..d489022 100644 --- a/M6502/M6502.Symbols/Symbol.cs +++ b/M6502/M6502.Symbols/Symbol.cs @@ -1,6 +1,4 @@ -#define FAST_VALUE_LOOKUP - -namespace EightBit +namespace EightBit { namespace Files { @@ -13,7 +11,7 @@ namespace EightBit { public string AddressSize => this.TakeString("addrsize"); - public int Size => this.TakeInteger("size"); + public int? Size { get; private set; } public Symbols.Scope Scope => this.TakeScopeReference(); @@ -21,11 +19,7 @@ namespace EightBit public List References => this.TakeLineReferences("ref"); // Guess -#if FAST_VALUE_LOOKUP - public int Value; -#else - public int Value => this.TakeInteger("val"); -#endif + public int Value { get; private set; } public Symbols.Segment Segment => this.TakeSegmentReference(); @@ -46,9 +40,10 @@ namespace EightBit public override void Parse(Parser parent, Dictionary entries) { base.Parse(parent, entries); -#if FAST_VALUE_LOOKUP + this.Value = this.TakeInteger("val"); -#endif + this.Size = this.MaybeTakeInteger("size"); + if (this.Type is "lab") { this._parent?.AddLabel(this);