From 3c4eecc388aa1d05dcc1c258ed833e1ba415ddf2 Mon Sep 17 00:00:00 2001 From: b3b00 Date: Tue, 29 Oct 2024 10:43:24 +0100 Subject: [PATCH] 3.4.1-alpha1 : performance --- .../visitor/EBNFSyntaxTreeVisitor.cs | 41 ++++++++++++------- src/sly/sly.csproj | 4 +- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/sly/parser/generator/visitor/EBNFSyntaxTreeVisitor.cs b/src/sly/parser/generator/visitor/EBNFSyntaxTreeVisitor.cs index 8f0b264a..dfa60f2e 100644 --- a/src/sly/parser/generator/visitor/EBNFSyntaxTreeVisitor.cs +++ b/src/sly/parser/generator/visitor/EBNFSyntaxTreeVisitor.cs @@ -4,6 +4,7 @@ using sly.lexer; using sly.parser.parser; using sly.parser.syntax.tree; +using System; namespace sly.parser.generator.visitor { @@ -87,51 +88,59 @@ private SyntaxVisitorResult Visit(SyntaxNode node, object context = var result = SyntaxVisitorResult.NoneResult(); if (node.Visitor != null || node.IsByPassNode) { + int parametersArrayLength = node.Children.Count + (context is NoContext ? 0 : 1); + var parameters = new object[parametersArrayLength]; - - var args = new List(); + int parametersCount = 0; foreach (var n in node.Children) { var v = Visit(n, context); - - if (v.IsToken) + if (v.IsToken && !n.Discarded) { - if (!n.Discarded) args.Add(v.TokenResult); + parameters[parametersCount] = v.TokenResult; + parametersCount++; } else if (v.IsValue) { - args.Add(v.ValueResult); + parameters[parametersCount] = v.ValueResult; + parametersCount++; } else if (v.IsOption) { - args.Add(v.OptionResult); + parameters[parametersCount] = v.OptionResult; + parametersCount++; } else if (v.IsOptionGroup) { - args.Add(v.OptionGroupResult); + parameters[parametersCount] = v.OptionGroupResult; + parametersCount++; } else if (v.IsGroup) { - args.Add(v.GroupResult); + parameters[parametersCount] = v.GroupResult; + parametersCount++; } else if (v.IsTokenList) { - args.Add(v.TokenListResult); + parameters[parametersCount] = v.TokenListResult; + parametersCount++; } else if (v.IsValueList) { - args.Add(v.ValueListResult); + parameters[parametersCount] = v.ValueListResult; + parametersCount++; } else if (v.IsGroupList) { - args.Add(v.GroupListResult); + parameters[parametersCount] = v.GroupListResult; + parametersCount++; } } if (node.IsByPassNode) { - result = SyntaxVisitorResult.NewValue((OUT)args[0]); + result = SyntaxVisitorResult.NewValue((OUT)parameters[0]); } else { @@ -140,11 +149,13 @@ private SyntaxVisitorResult Visit(SyntaxNode node, object context = { if (!(context is NoContext)) { - args.Add(context); + parameters[parametersCount] = context; + parametersCount++; } method = node.Visitor; - var t = method.Invoke(ParserVsisitorInstance, args.ToArray()); + Array.Resize(ref parameters, parametersCount); + var t = method.Invoke(ParserVsisitorInstance, parameters); var res = (OUT) t; result = SyntaxVisitorResult.NewValue(res); } diff --git a/src/sly/sly.csproj b/src/sly/sly.csproj index c3ad3cfd..7d59c7ec 100644 --- a/src/sly/sly.csproj +++ b/src/sly/sly.csproj @@ -6,10 +6,10 @@ $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb #LY is a parser generator halfway between parser combinators and parser generator like ANTLR b3b00 - 3.4.0 + 3.4.1-alpha1 https://github.com/b3b00/sly https://github.com/b3b00/sly - 3.4.0 + 3.4.1-alpha1 Library