Skip to content

Commit

Permalink
Roslyn implementation of ISourceText (#1123)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAngryByrd committed Jun 30, 2023
1 parent c784751 commit 87a2e9f
Show file tree
Hide file tree
Showing 30 changed files with 1,083 additions and 441 deletions.
4 changes: 2 additions & 2 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
]
},
"fantomas": {
"version": "6.0.0",
"version": "6.1.0",
"commands": [
"fantomas"
]
}
}
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ coverage
test/FsAutoComplete.Tests.Lsp/TestResults/

.tool-versions
BenchmarkDotNet.Artifacts/
6 changes: 6 additions & 0 deletions FsAutoComplete.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "FsAutoComplete.DependencyMa
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "build", "build\build.fsproj", "{400D56D0-28C9-4210-AA30-BD688122E298}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "benchmarks", "benchmarks\benchmarks.fsproj", "{0CD029D8-B39E-4CBE-A190-C84A7A811180}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -65,6 +67,10 @@ Global
{400D56D0-28C9-4210-AA30-BD688122E298}.Debug|Any CPU.Build.0 = Debug|Any CPU
{400D56D0-28C9-4210-AA30-BD688122E298}.Release|Any CPU.ActiveCfg = Release|Any CPU
{400D56D0-28C9-4210-AA30-BD688122E298}.Release|Any CPU.Build.0 = Release|Any CPU
{0CD029D8-B39E-4CBE-A190-C84A7A811180}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0CD029D8-B39E-4CBE-A190-C84A7A811180}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0CD029D8-B39E-4CBE-A190-C84A7A811180}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0CD029D8-B39E-4CBE-A190-C84A7A811180}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
16 changes: 16 additions & 0 deletions benchmarks/Program.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Benchmarks
open System
open BenchmarkDotNet
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Running
open System.Security.Cryptography




module EntryPoint =

[<EntryPoint>]
let main argv =
let summary = BenchmarkRunner.Run<SourceText_LineChanges_Benchmarks>();
0
69 changes: 69 additions & 0 deletions benchmarks/SourceTextBenchmarks.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
namespace Benchmarks

open System
open FSharp.Data.Adaptive
open Microsoft.CodeAnalysis.Text
type FileVersion = int


module Helpers =
open FsAutoComplete.LspHelpers
open FSharp.UMX
open System.Collections.Generic

let fileContents = IO.File.ReadAllText(@"C:\Users\jimmy\Repositories\public\TheAngryByrd\span-playground\Romeo and Juliet by William Shakespeare.txt")

let initNamedText () =
FsAutoComplete.NamedText(UMX.tag "lol", fileContents)

let initRoslynSourceText () =
SourceText.From(fileContents)


let convertToTextSpan (sourceText : SourceText, range : Ionide.LanguageServerProtocol.Types.Range) =
let start = sourceText.Lines.[max 0 (range.Start.Line)].Start + range.Start.Character
let endPosition =
sourceText.Lines.[min (range.End.Line) (sourceText.Lines.Count - 1)].Start
+ range.End.Character
TextSpan(start, endPosition - start)

let addToSourceText (sourceText : SourceText, range : Ionide.LanguageServerProtocol.Types.Range, text : string) =
let textSpan = convertToTextSpan(sourceText, range)
let newText = sourceText.WithChanges([| TextChange(textSpan, text) |])
newText

let addToSourceTextMany (sourceText : SourceText, spans : IEnumerable<Ionide.LanguageServerProtocol.Types.Range * string>) =
let textSpans = spans |> Seq.map (fun (range, text) -> TextChange(convertToTextSpan(sourceText, range), text)) |> Seq.toArray
let newText = sourceText.WithChanges(textSpans)
newText

let addToNamedText (namedText : FsAutoComplete.NamedText, range : Ionide.LanguageServerProtocol.Types.Range, text : string) =
let range = protocolRangeToRange (UMX.untag namedText.FileName) range
match namedText.ModifyText(range, text) with | Ok x -> x | Error e -> failwith e

open BenchmarkDotNet
open BenchmarkDotNet.Attributes
open Helpers
open BenchmarkDotNet.Jobs
[<MemoryDiagnoser>]
[<SimpleJob(RuntimeMoniker.Net70)>]
type SourceText_LineChanges_Benchmarks ()=

[<Params(1, 15, 50, 100, 1000)>]
member val public N = 0 with get, set

[<Benchmark(Baseline = true)>]
member this.Named_Text_changeText_everyUpdate () =
let mutable file = initNamedText ()
file <- addToNamedText(file, { Start = { Line = 0; Character = 5 }; End = { Line = 0; Character = 5 } }, "World")
for i in 1..this.N do
file <- addToNamedText(file, { Start = { Line = 0; Character = 10 }; End = { Line = 0; Character = 10 } }, "\nLOL")
file.Lines |> Seq.toArray |> ignore

[<Benchmark>]
member this.Roslyn_Text_changeText_everyUpdate () =
let mutable file = initRoslynSourceText ()
file <- addToSourceText(file, { Start = { Line = 0; Character = 5 }; End = { Line = 0; Character = 5 } }, "World")
for i in 1..this.N do
file <- addToSourceText(file, { Start = { Line = 0; Character = 10 }; End = { Line = 0; Character = 10 } }, "\nLOL")
file.Lines |> Seq.toArray |> ignore
15 changes: 15 additions & 0 deletions benchmarks/benchmarks.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<Compile Include="SourceTextBenchmarks.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\FsAutoComplete\FsAutoComplete.fsproj" />
</ItemGroup>
<Import Project="..\.paket\Paket.Restore.targets" />
</Project>
4 changes: 4 additions & 0 deletions benchmarks/paket.references
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FSharp.Core
BenchmarkDotNet
Microsoft.CodeAnalysis
FSharp.Data.Adaptive
2 changes: 2 additions & 0 deletions paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ strategy: min
lowest_matching: true


nuget BenchmarkDotNet 0.13.5
nuget Fantomas.Client >= 0.9
nuget FSharp.Compiler.Service >= 43.7.300
nuget Ionide.ProjInfo >= 0.61.3
Expand All @@ -21,6 +22,7 @@ nuget Microsoft.Build >= 17.2 copy_local:false
nuget Microsoft.Build.Framework >= 17.4 copy_local:false
nuget Microsoft.Build.Utilities.Core >= 17.4 copy_local:false
nuget Microsoft.Build.Tasks.Core >= 17.4 copy_local: false
nuget Microsoft.CodeAnalysis 4.5.0
nuget Nuget.Frameworks copy_local: false
nuget FSharp.Analyzers.SDK
nuget ICSharpCode.Decompiler
Expand Down
Loading

0 comments on commit 87a2e9f

Please sign in to comment.