Skip to content

Commit

Permalink
compile time optimisation - avoids allocs during parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmisek committed Dec 17, 2024
1 parent cb43456 commit 45d90fe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
7 changes: 1 addition & 6 deletions src/Peachpie.CodeAnalysis/Syntax/PhpSourceUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ public override void Parse(INodesFactory<LangElement, Span> 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);
}
}
22 changes: 22 additions & 0 deletions src/Peachpie.CodeAnalysis/Syntax/SourceLineBreaks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Devsense.PHP.Text;
using Microsoft.CodeAnalysis.Text;

namespace Peachpie.CodeAnalysis.Syntax
{
/// <summary>
/// Implements <see cref="ILineBreaks"/> of <see cref="SourceText"/>.
/// </summary>
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;
}
}

0 comments on commit 45d90fe

Please sign in to comment.