From 11145e278bea9154c518bd21c839ac044cde3fbd Mon Sep 17 00:00:00 2001 From: Booklordofthedings Date: Thu, 15 Aug 2024 20:01:16 +0200 Subject: [PATCH] more performance stuff, new tests, and toString implemented --- BeefProj.toml | 1 + src/Bofa.bf | 129 +++++++++++++++++++++++++++ src/BofaParser.bf | 31 +++---- src/Extension.bf | 10 +++ src/Parser/BofaParserInsert.bf | 36 ++++++++ src/Program.bf | 39 --------- src/Testing/Consistency.bf | 45 ++++++++++ src/Testing/Default.bf | 22 +++-- src/Testing/Fuzzing.bf | 45 ++++++++++ src/Testing/Profiling.bf | 155 +++++++++++++++++++++++++++------ 10 files changed, 421 insertions(+), 92 deletions(-) create mode 100644 src/Extension.bf delete mode 100644 src/Program.bf create mode 100644 src/Testing/Consistency.bf create mode 100644 src/Testing/Fuzzing.bf diff --git a/BeefProj.toml b/BeefProj.toml index ddfd45a..0d15fdc 100644 --- a/BeefProj.toml +++ b/BeefProj.toml @@ -2,4 +2,5 @@ FileVersion = 1 [Project] Name = "Bofa" +TargetType = "BeefLib" StartupObject = "Bofa.Program" diff --git a/src/Bofa.bf b/src/Bofa.bf index 8544b43..06c0b26 100644 --- a/src/Bofa.bf +++ b/src/Bofa.bf @@ -1,6 +1,7 @@ namespace Bofa; using System; +using System.Collections; class Bofa { @@ -12,6 +13,134 @@ class Bofa public StringView Typename; public BofaValue Value; + public override void ToString(String strBuffer) + { + int64 depth = 0; + Bofa target = this; + List<(int64, Bofa)> toProcess = new .(); + defer delete toProcess; + + repeat + { + + target.ToString(strBuffer, depth); + + if(target.Type == .Array) + { + depth++; + for(var i in Value.Array.Reversed) + toProcess.Add((depth, i)); + } + else if(target.Type == .Object) + { + depth++; + for(var i in Value.Object) + toProcess.Add((depth, i.value)); + } + + if(toProcess.IsEmpty) + target = null; + else + { + var t = toProcess.PopBack(); + depth = t.0; + target = t.1; + strBuffer.Append('\n'); + } + } while(target != null); + } + + private void ToString(String strBuffer, int64 pDepth) + { + strBuffer.Append(' ', pDepth); + switch(Type) + { + case .Array: + strBuffer.Append(scope $"a {Name}"); + case .BigInteger: + strBuffer.Append(scope $"bi {Name} {Value.BigInteger}"); + case .BigNumber: + strBuffer.Append(scope $"bn {Name} {Value.BigNumber}"); + case .Boolean: + strBuffer.Append(scope $"b {Name} {Value.Boolean ? "true" : "false"}"); + case .Custom: + strBuffer.Append(scope $"c {Typename} {Name} {Value.Custom}"); + case .Integer: + strBuffer.Append(scope $"i {Name} {Value.Integer}"); + case .Line: + strBuffer.Append(scope $"l {Name} {Value.Line}"); + case .Number: + strBuffer.Append(scope $"n {Name} {Value.Number}"); + case .Object: + strBuffer.Append(scope $"o {Name}"); + case .Text: + strBuffer.Append(scope $"t {Name} "); + for(var i in Value.Text.Split('\n', .None)) + { + strBuffer.Append(i); + if(@i.HasMore) + { + strBuffer.Append('\n'); + strBuffer.Append(' ', pDepth); + strBuffer.Append("- " ); + } + + } + } + } + + ///Deletes this object without filling the stack + public void DeepDeletion() + { + if(this.Type == .Array || this.Type == .Object) + { + List toDelete = new .(); + List toProcess = new .(); + + toDelete.Add(this); + Bofa target = this; + repeat + { + if(target.Type == .Object) + { + for(var i in target.Value.Object) + { + toDelete.Add(i.value); + toProcess.Add(i.value); + } + target.Value.Object.Clear(); + } + else if(target.Type == .Array) + { + for(var i in target.Value.Array) + { + toDelete.Add(i); + toProcess.Add(i); + } + target.Value.Array.Clear(); + } + + if(toProcess.Count > 0 && target != null) + target = toProcess.PopBack(); + else + target = null; + } while(target != null); + + for(var i in toDelete.Reversed) + delete i; + + delete toDelete; + delete toProcess; + } + else + { + if(_line != null) + delete _line; + if(Type == .Text) + delete Value.Text; + } + } + public ~this() { if(_line != null) diff --git a/src/BofaParser.bf b/src/BofaParser.bf index 57db108..b8b7593 100644 --- a/src/BofaParser.bf +++ b/src/BofaParser.bf @@ -9,18 +9,16 @@ using Bofa.Parser; class BofaParser { - public static void Parse(StringView pToParse, List pResult, List pErrors) + public static void Parse(StringView pToParse, Dictionary pResult, List pErrors) { StringStream stream = new .(pToParse, .Reference); defer delete stream; Parse(stream, pResult, pErrors); } - public static void Parse(Stream pToParse, List pResult, List pErrors) + public static void Parse(Stream pToParse, Dictionary pResult, List pErrors) { - HashSet lowLevelObjects = new .(100); - defer delete lowLevelObjects; - + Bofa last = null; if (pToParse == null || pResult == null) { //Cannot parse what doesnt exist pErrors.Add(-1); @@ -36,6 +34,7 @@ class BofaParser String l = new .(); if (reader.ReadLine(l) case .Err) { //Despite data being here, we cannot read it + delete l; pErrors.Add(-2); return; } @@ -49,16 +48,18 @@ class BofaParser continue; } if(entry.Type == .Error) + { pErrors.Add(line); - if(entry.Type == .Text) - defer delete l; + continue; + } //If we are on the lowest level we can just add them here if(entry.Depth == 0 && entry.Type ==.Bofa) { - if(lowLevelObjects.Add(SHA256.Hash(cast!>(entry.Data.Bofa.Name)))) + if(!pResult.ContainsKey(entry.Data.Bofa.Name)) { - pResult.Add(entry.Data.Bofa); + last = entry.Data.Bofa; + pResult.Add(entry.Data.Bofa.Name, entry.Data.Bofa); } else { @@ -68,27 +69,29 @@ class BofaParser } else if(entry.Depth == 1 && entry.Type == .Text) { - if(pResult[pResult.Count-1].Type == .Text) - pResult[pResult.Count-1].Value.Text.Append(scope $"\n{entry.Data.Text}"); + if(last.Type == .Text) + last.Value.Text.Append(scope $"\n{entry.Data.Text}"); else pErrors.Add(entry.Line); //Bad text error } else { entry.Depth--; - if(pResult[pResult.Count-1].[Friend]_Insert(&entry) case .Err) + if(last.[Friend]_Insert(&entry) case .Err) { pErrors.Add(line); delete entry.Data.Bofa; } } - + if(entry.Type == .Text) + delete l; line++; } return; //Being done normally } + [Inline] private static ParserEntry ParseLine(String pLine, int64 pLineNumber) { StringView line = pLine; @@ -129,8 +132,6 @@ class BofaParser if (type.0 == "") //This should never be reached { Runtime.FatalError("Unreachable code reached"); - toReturn.Type = .Error; - return toReturn; } else if (type.0 == "-") { diff --git a/src/Extension.bf b/src/Extension.bf new file mode 100644 index 0000000..d0898b5 --- /dev/null +++ b/src/Extension.bf @@ -0,0 +1,10 @@ +namespace System +{ + static + { + public static mixin cast(var v) + { + *(T*)(void*)&v + } + } +} \ No newline at end of file diff --git a/src/Parser/BofaParserInsert.bf b/src/Parser/BofaParserInsert.bf index bbb6dc3..ffa18a2 100644 --- a/src/Parser/BofaParserInsert.bf +++ b/src/Parser/BofaParserInsert.bf @@ -6,8 +6,43 @@ using System; extension Bofa { + [Inline] private Result _Insert(ParserEntry* pToAdd) { + Bofa target = this; + while(!(pToAdd.Depth == 0 || (pToAdd.Type == .Text && pToAdd.Depth == 1))) + { + if(target == null || !(target.Type == .Object || target.Type == .Array)) + return .Err; + + pToAdd.Depth--; + target = target._lastObject; + } + + if(pToAdd.Type == .Text && target.Type == .Text) + { + target.Value.Text.Append(scope $"\n{pToAdd.Data.Text}"); + //delete pToAdd.Data.Text; + return .Ok; + } + else if(target.Type == .Object) + { + if(target.Value.Object.ContainsKey(pToAdd.Data.Bofa.Name)) + return .Err; + target.Value.Object.Add(pToAdd.Data.Bofa.Name, pToAdd.Data.Bofa); + target._lastObject = pToAdd.Data.Bofa; + return .Ok; + + } + else if(target.Type == .Array) + { + target.Value.Array.Add(pToAdd.Data.Bofa); + target._lastObject = pToAdd.Data.Bofa; + return .Ok; + } + return .Err; + + /* //See if we can insert and do so if(pToAdd.Depth == 0 || (pToAdd.Type == .Text && pToAdd.Depth == 1)) { @@ -41,5 +76,6 @@ extension Bofa pToAdd.Depth--; return _lastObject._Insert(pToAdd); + */ } } \ No newline at end of file diff --git a/src/Program.bf b/src/Program.bf deleted file mode 100644 index efd7a93..0000000 --- a/src/Program.bf +++ /dev/null @@ -1,39 +0,0 @@ -using System; - -namespace Bofa -{ - - class Program - { - public static void Main() - { - Bofa.Testing.Profiling.Profiling_Normal_Large(); - } - } -} - - - -namespace System.Security.Cryptography -{ - extension SHA256Hash : IHashable - { - public int GetHashCode() - { - return this.mHash[0]; - } - } - - -} - -namespace System -{ - static - { - public static mixin cast(var v) - { - *(T*)(void*)&v - } - } -} \ No newline at end of file diff --git a/src/Testing/Consistency.bf b/src/Testing/Consistency.bf new file mode 100644 index 0000000..9d2f1ed --- /dev/null +++ b/src/Testing/Consistency.bf @@ -0,0 +1,45 @@ +namespace Bofa.Testing; + +using Bofa; + +using System; +using System.Collections; + +class Consistency +{ + [Test(Name="Reverse simple")] + public static void Standart() + { + String content = """ + l one line of text + b bool true + t textgoes here + - Text addendum + n tone 12.5 + bn biggie 6.56456e+10 + i int 234345 + bi bint 38432847329847329 + o object + b content true + a array + b content true + b content true + """; + Dictionary output = new .(); + List errors = new .(); + BofaParser.Parse(content, output, errors); + + String s = scope .(); + for(var i in output) + { + i.value.ToString(s); + s.Append('\n'); + } + s.RemoveFromEnd(1); + + Test.Assert(content == s); + + DeleteDictionaryAndValues!(output); + delete errors; + } +} \ No newline at end of file diff --git a/src/Testing/Default.bf b/src/Testing/Default.bf index 977114a..9b708c7 100644 --- a/src/Testing/Default.bf +++ b/src/Testing/Default.bf @@ -29,16 +29,14 @@ class Default b content true b content true """; - List output = new .(); + Dictionary output = new .(); List errors = new .(); BofaParser.Parse(content, output, errors); Test.Assert(output.Count == 9); - Test.Assert(output[7].Value.Object.Count == 1); - Test.Assert(output[8].Value.Array.Count == 2); Test.Assert(errors.Count == 0); - DeleteContainerAndItems!(output); + DeleteDictionaryAndValues!(output); delete errors; } @@ -53,14 +51,14 @@ class Default i integer 21324 bi biginteger 565765765765765 """; - List output = new .(); + Dictionary output = new .(); List errors = new .(); BofaParser.Parse(content, output, errors); Test.Assert(output.Count == 5); Test.Assert(errors.Count == 0); - DeleteContainerAndItems!(output); + DeleteDictionaryAndValues!(output); delete errors; } @@ -76,14 +74,14 @@ class Default - that stuff has to be appended - but thats fine """; - List output = new .(); + Dictionary output = new .(); List errors = new .(); BofaParser.Parse(content, output, errors); Test.Assert(output.Count == 1); Test.Assert(errors.Count == 0); - DeleteContainerAndItems!(output); + DeleteDictionaryAndValues!(output); delete errors; } @@ -102,14 +100,14 @@ class Default o even deeper n member 1 """; - List output = new .(); + Dictionary output = new .(); List errors = new .(); BofaParser.Parse(content, output, errors); Test.Assert(output.Count == 2); Test.Assert(errors.Count == 0); - DeleteContainerAndItems!(output); + DeleteDictionaryAndValues!(output); delete errors; } @@ -125,14 +123,14 @@ class Default n member 1 n member 2 """; - List output = new .(); + Dictionary output = new .(); List errors = new .(); BofaParser.Parse(content, output, errors); Test.Assert(output.Count == 2); Test.Assert(errors.Count == 2); - DeleteContainerAndItems!(output); + DeleteDictionaryAndValues!(output); delete errors; } } \ No newline at end of file diff --git a/src/Testing/Fuzzing.bf b/src/Testing/Fuzzing.bf new file mode 100644 index 0000000..4374964 --- /dev/null +++ b/src/Testing/Fuzzing.bf @@ -0,0 +1,45 @@ +namespace Bofa.Testing; + +using Bofa; + +using System; +using System.Collections; + +class Fuzzing +{ + [Test(Name = "Fuzzing - 10 * 1000000 random characters")] + public static void Fuzzin_Random() + { + for (int i < 10) + { + String toParse = new .(10 * 1500); + for (int ii < 250000) + toParse.Append(cast!(gRand.NextI32())); + + Dictionary output = new .(); + List errors = new .(); + BofaParser.Parse(toParse, output, errors); + DeleteDictionaryAndValues!(output); + delete errors; + delete toParse; + } + } + + [Test(Name = "Fuzzing - 10 * 10000000 random characters")] + public static void Fuzzin_Random_Large() + { + for (int i < 10) + { + String toParse = new .(10 * 1500); + for (int ii < 2500000) + toParse.Append(cast!(gRand.NextI32())); + + Dictionary output = new .(); + List errors = new .(); + BofaParser.Parse(toParse, output, errors); + DeleteDictionaryAndValues!(output); + delete errors; + delete toParse; + } + } +} \ No newline at end of file diff --git a/src/Testing/Profiling.bf b/src/Testing/Profiling.bf index ca906ae..f7ebcd0 100644 --- a/src/Testing/Profiling.bf +++ b/src/Testing/Profiling.bf @@ -12,28 +12,32 @@ class Profiling { for (int i < 1000) { - String content = """ -l one line of text -b bool true -# saoidsaodsad -t text goes here -- Text addendum -n tone 12.5 -#husdhfiudshfds -bn biggie 65645645645.6 -i int 234345 -bi bint 38432847329847329 -o object -b content true -a array -b content true -b content true + String toParse = scope .(); + toParse.Append(scope $""" + l one line of text + b bool true + # saoidsaodsad + t text goes here + - Text addendum + n tone 12.5 + #husdhfiudshfds + bn biggie 65645645645.6 + i int 234345 + bi bint 38432847329847329 + o object + b content true + a array + b content true + b content true -"""; - List output = new .(); + """); + + + + Dictionary output = new .(); List errors = new .(); - BofaParser.Parse(content, output, errors); - DeleteContainerAndItems!(output); + BofaParser.Parse(toParse, output, errors); + DeleteDictionaryAndValues!(output); delete errors; } } @@ -67,10 +71,10 @@ b content true """); } - List output = new .(); + Dictionary output = new .(); List errors = new .(); BofaParser.Parse(toParse, output, errors); - DeleteContainerAndItems!(output); + DeleteDictionaryAndValues!(output); delete errors; delete toParse; } @@ -78,11 +82,11 @@ b content true - [Test(Name = "Profiling: normal 1 * 600000")] + [Test(Name = "Profiling: normal 1 * 300000")] public static void Profiling_Normal_Large() { String toParse = new .(10 * 150000); - for (int ii < 40000) + for (int ii < 20000) { toParse.Append(scope $""" l one{ii} line of text @@ -105,11 +109,110 @@ b content true """); } - List output = new .(); + Dictionary output = new .(); List errors = new .(); BofaParser.Parse(toParse, output, errors); - DeleteContainerAndItems!(output); + DeleteDictionaryAndValues!(output); delete errors; delete toParse; } + + [Test(Name = "Profiling: Depth testing 100 * 100")] + public static void Profiling_Depth_Testing() + { + for (int i < 100) + { + String toParse = scope .(); + for(int ii < 100) + { + toParse.Append(' ', ii); + toParse.Append(scope $"o obj\n"); + } + + Dictionary output = new .(); + List errors = new .(); + BofaParser.Parse(toParse, output, errors); + DeleteDictionaryAndValues!(output); + delete errors; + } + } + [Test(Name = "Profiling: Depth testing 100 * 1000")] + public static void Profiling_Depth_Testing_Medium() + { + for (int i < 100) + { + String toParse = scope .(); + for(int ii < 1000) + { + toParse.Append(' ', ii); + toParse.Append(scope $"o obj\n"); + } + + Dictionary output = new .(); + List errors = new .(); + BofaParser.Parse(toParse, output, errors); + DeleteDictionaryAndValues!(output); + delete errors; + } + } + + [Test(Name = "Profiling: Depth testing 10 * 5000")] + public static void Profiling_Depth_Testing_Large() + { + for (int i < 10) + { + String toParse = scope .(); + for(int ii < 5000) + { + toParse.Append(' ', ii); + toParse.Append(scope $"o obj\n"); + } + + Dictionary output = new .(); + List errors = new .(); + BofaParser.Parse(toParse, output, errors); + output["obj"].DeepDeletion(); + delete output; + delete errors; + } + } + + [Test(Name = "Profiling: Texts 10 * 1000")] + public static void Profiling_Texts() + { + for (int i < 10) + { + String toParse = new .(10 * 1500); + toParse.Append("t text goes here"); + for (int ii < 1000) + toParse.Append("- Addendum"); + + Dictionary output = new .(); + List errors = new .(); + BofaParser.Parse(toParse, output, errors); + DeleteDictionaryAndValues!(output); + delete errors; + delete toParse; + } + } + + [Test(Name = "Profiling: Large Texts 10 * 100000")] + public static void Profiling_Texts_Large() + { + for (int i < 10) + { + String toParse = new .(10 * 1500); + toParse.Append("t text goes here"); + for (int ii < 100000) + toParse.Append("- Addendum"); + + Dictionary output = new .(); + List errors = new .(); + BofaParser.Parse(toParse, output, errors); + DeleteDictionaryAndValues!(output); + delete errors; + delete toParse; + } + } + } \ No newline at end of file