From 45d90fe647344c58ff91dd2f8c970166f78d93fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Mi=CC=81s=CC=8Cek?= Date: Tue, 17 Dec 2024 13:05:09 +0100 Subject: [PATCH] compile time optimisation - avoids allocs during parsing --- .../Syntax/PhpSourceUnit.cs | 7 +----- .../Syntax/SourceLineBreaks.cs | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 src/Peachpie.CodeAnalysis/Syntax/SourceLineBreaks.cs diff --git a/src/Peachpie.CodeAnalysis/Syntax/PhpSourceUnit.cs b/src/Peachpie.CodeAnalysis/Syntax/PhpSourceUnit.cs index 56b66851e9..ffc18740db 100644 --- a/src/Peachpie.CodeAnalysis/Syntax/PhpSourceUnit.cs +++ b/src/Peachpie.CodeAnalysis/Syntax/PhpSourceUnit.cs @@ -63,11 +63,6 @@ public override void Parse(INodesFactory factory, IErrorSink< Parse((NodesFactory)factory, errors, recovery, LanguageFeatures.Basic, Lexer.LexicalStates.INITIAL); } - static ILineBreaks CreateLineBreaks(SourceText source) - { - return Devsense.PHP.Text.LineBreaks.Create( - source.ToString(), - source.Lines.Select(line => line.EndIncludingLineBreak).ToList()); - } + static ILineBreaks CreateLineBreaks(SourceText source) => new SourceLineBreaks(source); } } diff --git a/src/Peachpie.CodeAnalysis/Syntax/SourceLineBreaks.cs b/src/Peachpie.CodeAnalysis/Syntax/SourceLineBreaks.cs new file mode 100644 index 0000000000..7d88223a78 --- /dev/null +++ b/src/Peachpie.CodeAnalysis/Syntax/SourceLineBreaks.cs @@ -0,0 +1,22 @@ +using Devsense.PHP.Text; +using Microsoft.CodeAnalysis.Text; + +namespace Peachpie.CodeAnalysis.Syntax +{ + /// + /// Implements of . + /// + sealed class SourceLineBreaks : LineBreaks + { + private readonly SourceText/*!*/_source; + + public SourceLineBreaks(SourceText source) : base(source.Length) + { + _source = source; + } + + public override int EndOfLineBreak(int index) => _source.Lines[index].EndIncludingLineBreak; + + public override int Count => _source.Lines.Count - 1; + } +} \ No newline at end of file