Visitar URL original
Support LSP-style units for character offsets in tagging by tclem · Pull Request #580 · github/semantic · GitHub
The Wayback Machine - https://web.archive.org/web/20250522075912/https://github.com/github/semantic/pull/580
Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Support LSP-style units for character offsets in tagging #580

Merged
merged 38 commits into from
Jul 7, 2020
Merged

Conversation

tclem
Copy link
Member

@tclem tclem commented Jun 25, 2020 •

We're looking into being able to align more closely with LSP (the Language Server Protocol) or at least a stateless subset thereof for code navigation and as part of that we've recently discovered that our representations of locations in source differ in a subtle, but important way.

The LSP ecosystem has a known eccentricity in the units of the character offset (what semantic calls column on the Pos datatype) that's used for many queries (see TextDocumentPositionParams). While all text in LSP is UTF-8 encoded, the character offset is based on a UTF-16 string representation which is in conflict with how semantic tracks this data.

In order to measure column/character offsets in source:

  • Semantic (and tree-sitter) uses UTF-8 bytes as the unit of measure.
  • LSP uses UTF-16 code units (in this case a code unit = 2 bytes).

This is only a problem when using multibyte unicode characters is source code. Please see the tests for some examples of what that might look like.

This PR is a proposal to address these issues by including an additional Span in the tags output that is in LSP-friendly units. Along the way I discovered some things about how semantic is slicing out source to produce a line of text for display that could improve and needed to change as part of this effort.

In addition I deprecated method docs (only wired up for Python). These are no longer carried around in the Tags datatype, this could be a future improvement for all languages, but no reason to do this work if nobody is using the data.

Copy link
Contributor

@patrickt patrickt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really great work @tclem—an elegant approach to a tricky problem. I left a couple nitpick comments, but I think on the whole this is ready to go, though we’ll have to push out the new semantic.proto to its various callers, correct?

Comment on lines 94 to 101
countUtf16CodeUnits :: Source -> Int
countUtf16CodeUnits = Text.foldr len 0 . Source.toText
where
len :: Char -> Int -> Int
len i acc =
let c = fromEnum i
x = if c .&. 0xFFFF == c then 1 else 2
in x + acc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get some comments here about this bit-twiddling? Also, do we need any strictness annotations?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good idea. Let me know if the latest helps with understanding.

I'd love some advice on strictness here... it's probably better to be strict? Or hard to tell without benchmarking?

@tclem
Copy link
Member Author

tclem commented Jun 30, 2020

Alright @patrickt should be fixed up and back in sync with mainline move of semantic to a sub dir.

@tclem
Copy link
Member Author

tclem commented Jul 1, 2020 •

Counting utf16 code units is computationally expensive (and extra work semantic wasn't doing before). There are some unavoidable reasons for this. Since all data in semantic is utf8 encoded the only way to get a character offset in utf16 code units for a particular location is to a) find the beginning of the line which means searching for things like \n and b) count forward in the new units.

In my measurements, finding the beginning of the line is relatively fast (using minimized js as the stress test since the code is all on one line). This part of the algorithm uses elemIndex on the ByteString (implemented with memchr(3)) and then take and drop are both O(1) so slicing out that source is relatively efficient.

file: jquery-3.5.1.min.js
(Loc (Range 73754 73757) (Span (Pos 2 73670) (Pos 2 73673))) -- NB: Range is a byte range, Span is 1-indexed
benchmarked surroundingLine
time                 2.964 μs   (2.933 μs .. 3.010 μs)
                     0.998 R²   (0.996 R² .. 0.999 R²)
mean                 3.005 μs   (2.985 μs .. 3.034 μs)
std dev              82.92 ns   (61.65 ns .. 124.8 ns)
variance introduced by outliers: 11% (moderately inflated)

The slow part is counting:

benchmarked countUtf16CodeUnits
time                 3.517 ms   (3.437 ms .. 3.618 ms)
                     0.996 R²   (0.994 R² .. 0.998 R²)
mean                 3.540 ms   (3.498 ms .. 3.595 ms)
std dev              154.6 μs   (113.0 μs .. 202.0 μs)
variance introduced by outliers: 23% (moderately inflated)

Reading the original algorithm again, there are a few improvements we can make:

  • Don't do Source.toText multiple times as this decodes a utf8 bytestring to Text.
  • But that requires we re-write the counting algorithm to operate on the raw utf8 encoded ByteString ^.
  • Use StrictData and some carefully placed strictness annotations
  • Inline countUtf16CodeUnits

After all that (and lots of intermediate experimentation) the numbers have come down a bit (same benchmark): 183μs vs 3.5ms.

benchmarked countUtf16CodeUnits
time                 182.9 μs   (179.6 μs .. 187.1 μs)
                     0.997 R²   (0.996 R² .. 0.999 R²)
mean                 178.6 μs   (177.4 μs .. 180.3 μs)
std dev              4.745 μs   (3.798 μs .. 6.490 μs)
variance introduced by outliers: 11% (moderately inflated)

This cuts time to generate symbols in a large minified JavaScript file in half, but you can see that we're still way off the original baseline.

Benchmarking symbol generation with counting utf16 code units on two largish files:

baseline semantic (not counting these units at all)
❯ time semantic parse --symbols ../github/app/models/user.rb &>/dev/null
semantic parse --symbols ../github/app/models/user.rb &> /dev/null 0.27s user 0.18s system 191% cpu 0.239 total
❯ time semantic parse --symbols ~/Downloads/jquery-3.5.1.min.js &>/dev/null
semantic parse --symbols ~/Downloads/jquery-3.5.1.min.js &> /dev/null 0.64s user 0.40s system 298% cpu 0.347 total

baseline utf16 cu counting
❯ time cabal exec semantic -- parse --symbols ../github/app/models/user.rb &>/dev/null
cabal run exe:semantic -- parse --symbols ../github/app/models/user.rb &> 1.43s user 0.35s system 182% cpu 0.975 total
❯ time cabal exec semantic -- parse --symbols ~/Downloads/jquery-3.5.1.min.js &>/dev/null
cabal exec semantic -- parse --symbols ~/Downloads/jquery-3.5.1.min.js &> 16.39s user 4.03s system 206% cpu 9.909 total

Enable StrictData
❯ time cabal exec semantic -- parse --symbols ../github/app/models/user.rb &>/dev/null
cabal exec semantic -- parse --symbols ../github/app/models/user.rb &> 1.23s user 0.43s system 174% cpu 0.952 total
❯ time cabal exec semantic -- parse --symbols ~/Downloads/jquery-3.5.1.min.js &>/dev/null
cabal exec semantic -- parse --symbols ~/Downloads/jquery-3.5.1.min.js &> 15.90s user 4.65s system 194% cpu 10.556 total

Use BangPatterns
❯ time cabal exec semantic -- parse --symbols ../github/app/models/user.rb &>/dev/null
cabal exec semantic -- parse --symbols ../github/app/models/user.rb &> 1.40s user 0.36s system 182% cpu 0.965 total
❯ time cabal exec semantic -- parse --symbols ~/Downloads/jquery-3.5.1.min.js &>/dev/null
cabal exec semantic -- parse --symbols ~/Downloads/jquery-3.5.1.min.js &> 11.96s user 3.77s system 173% cpu 9.092 total

Inline countUtf16CodeUnits
❯ time cabal exec semantic -- parse --symbols ../github/app/models/user.rb &>/dev/null
cabal exec semantic -- parse --symbols ../github/app/models/user.rb &> 1.40s user 0.35s system 183% cpu 0.954 total
❯ time cabal exec semantic -- parse --symbols ~/Downloads/jquery-3.5.1.min.js &>/dev/null
cabal exec semantic -- parse --symbols ~/Downloads/jquery-3.5.1.min.js &> 12.55s user 2.98s system 181% cpu 8.570 total

Count directly in ByteString (plus inline and strictness changes)
❯ time cabal exec semantic -- parse --symbols ../github/app/models/user.rb &>/dev/null
cabal exec semantic -- parse --symbols ../github/app/models/user.rb &> 1.35s user 0.35s system 180% cpu 0.943 total
❯ time cabal exec semantic -- parse --symbols ~/Downloads/jquery-3.5.1.min.js &>/dev/null
cabal exec semantic -- parse --symbols ~/Downloads/jquery-3.5.1.min.js &> 6.33s user 1.02s system 133% cpu 5.500 total

@tclem
Copy link
Member Author

tclem commented Jul 1, 2020

Alright, @joshvera and I made some major progress on this today and we're now within striking range of the baseline so I'm going to leave this alone and call it good!

The critical optimizations were all around the idea of just doing this extra work once per file. We updated the algorithm to cache previous calculations of the surrounding source line and then the utf16 code unit count. As an example of the later... if a bunch of identifiers are all on the same line, we use cached counts of the computed offsets to avoid re-counting the row over and over again.

Caching calculations for slicing out surrounding line and counting utf16 code units
❯ time cabal exec semantic -- parse --symbols ../github/app/models/user.rb &>/dev/null
cabal exec semantic -- parse --symbols ../github/app/models/user.rb &> 1.57s user 0.29s system 187% cpu 0.988 total
⑁ semantic/ on unicode-math ❯ time cabal exec semantic -- parse --symbols ~/Downloads/jquery-3.5.1.min.js &>/dev/null
cabal exec semantic -- parse --symbols ~/Downloads/jquery-3.5.1.min.js &> 2.89s user 0.74s system 218% cpu 1.662 total

Benchmarks show that were maybe 30% off the baseline now:

**baseline, no counting of utf16**
benchmarked tagging/jquery
time                 969.0 ms   (897.0 ms .. 1.088 s)
                     0.986 R²   (0.977 R² .. 0.996 R²)
mean                 1.045 s    (967.6 ms .. 1.222 s)
std dev              186.9 ms   (80.20 ms .. 306.3 ms)
variance introduced by outliers: 58% (severely inflated)

benchmarking tagging/sinatra ... took 17.33 s, total 56 iterations
benchmarked tagging/sinatra
time                 309.0 ms   (278.0 ms .. 335.5 ms)
                     0.979 R²   (0.947 R² .. 0.998 R²)
mean                 312.9 ms   (303.0 ms .. 335.8 ms)
std dev              23.43 ms   (13.55 ms .. 36.01 ms)
variance introduced by outliers: 19% (moderately inflated)

**after joshvera/tclem magic**
benchmarked tagging/jquery
time                 1.359 s    (1.294 s .. 1.434 s)
                     0.995 R²   (0.987 R² .. 1.000 R²)
mean                 1.316 s    (1.300 s .. 1.356 s)
std dev              42.98 ms   (17.87 ms .. 67.31 ms)

benchmarking tagging/sinatra ... took 18.33 s, total 56 iterations
benchmarked tagging/sinatra
time                 325.2 ms   (275.2 ms .. 370.7 ms)
                     0.961 R²   (0.919 R² .. 0.998 R²)
mean                 338.0 ms   (322.0 ms .. 356.8 ms)
std dev              30.81 ms   (24.04 ms .. 39.75 ms)
variance introduced by outliers: 28% (moderately inflated)

tclem and others added 4 commits July 1, 2020 17:02
Co-Authored-By: Rebecca Valentine <bekavalentine@users.noreply.github.com>
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
Co-Authored-By: Rebecca Valentine <bekavalentine@users.noreply.github.com>
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
Co-Authored-By: Rebecca Valentine <bekavalentine@users.noreply.github.com>
Co-Authored-By: Max Brunsfeld <maxbrunsfeld@gmail.com>
@tclem
Copy link
Member Author

tclem commented Jul 6, 2020

Just to roundtrip, I cut a branch and re-ran our benchmarks against master:

master benchmarks

benchmarking tagging/jquery ... took 39.98 s, total 56 iterations
benchmarked tagging/jquery
time                 710.1 ms   (655.3 ms .. 794.5 ms)
                     0.984 R²   (0.962 R² .. 0.999 R²)
mean                 725.7 ms   (703.8 ms .. 752.9 ms)
std dev              41.30 ms   (23.41 ms .. 62.58 ms)
variance introduced by outliers: 18% (moderately inflated)

benchmarking tagging/sinatra ... took 11.65 s, total 56 iterations
benchmarked tagging/sinatra
time                 200.1 ms   (176.9 ms .. 241.4 ms)
                     0.938 R²   (0.811 R² .. 0.996 R²)
mean                 209.3 ms   (200.1 ms .. 228.3 ms)
std dev              22.75 ms   (11.30 ms .. 38.74 ms)
variance introduced by outliers: 38% (moderately inflated)

Current state on this branch: fe69bdf

benchmarking tagging/jquery ... took 59.28 s, total 56 iterations
benchmarked tagging/jquery
time                 1.040 s    (954.4 ms .. 1.131 s)
                     0.989 R²   (0.973 R² .. 1.000 R²)
mean                 1.083 s    (1.051 s .. 1.104 s)
std dev              47.10 ms   (29.88 ms .. 71.44 ms)

benchmarking tagging/sinatra ... took 13.12 s, total 56 iterations
benchmarked tagging/sinatra
time                 248.8 ms   (234.4 ms .. 263.5 ms)
                     0.994 R²   (0.980 R² .. 0.999 R²)
mean                 231.4 ms   (223.4 ms .. 237.8 ms)
std dev              13.32 ms   (8.937 ms .. 20.60 ms)
variance introduced by outliers: 18% (moderately inflated)

@tclem
Copy link
Member Author

tclem commented Jul 7, 2020

One more minor optimization to grabbing the source line. I'm going to call this good for the moment.

benchmarking tagging/jquery ... took 49.62 s, total 56 iterations
benchmarked tagging/jquery
time                 929.1 ms   (868.6 ms .. 1.012 s)
                     0.993 R²   (0.979 R² .. 1.000 R²)
mean                 894.3 ms   (881.0 ms .. 923.6 ms)
std dev              33.70 ms   (17.74 ms .. 53.54 ms)

benchmarking tagging/sinatra ... took 13.80 s, total 56 iterations
benchmarked tagging/sinatra
time                 242.6 ms   (232.8 ms .. 250.6 ms)
                     0.996 R²   (0.984 R² .. 1.000 R²)
mean                 251.0 ms   (244.7 ms .. 254.8 ms)
std dev              7.893 ms   (5.308 ms .. 12.37 ms)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants