-
Notifications
You must be signed in to change notification settings - Fork 457
Support LSP-style units for character offsets in tagging #580
Conversation
Also updates how we generate the line of source code for display
There was a problem hiding this 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?
| 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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
|
Alright @patrickt should be fixed up and back in sync with mainline move of semantic to a sub dir. |
|
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 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 The slow part is counting: Reading the original algorithm again, there are a few improvements we can make:
After all that (and lots of intermediate experimentation) the numbers have come down a bit (same benchmark): 183μs vs 3.5ms. 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) baseline utf16 cu counting Enable StrictData Use BangPatterns Inline countUtf16CodeUnits Count directly in ByteString (plus inline and strictness changes) |
|
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 Benchmarks show that were maybe 30% off the baseline now: |
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>
|
Just to roundtrip, I cut a branch and re-ran our benchmarks against master: Current state on this branch: fe69bdf |
|
One more minor optimization to grabbing the source line. I'm going to call this good for the moment. |


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
characteroffset (what semantic callscolumnon thePosdatatype) that's used for many queries (seeTextDocumentPositionParams). While all text in LSP is UTF-8 encoded, thecharacteroffset 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:
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
Spanin 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
Tagsdatatype, this could be a future improvement for all languages, but no reason to do this work if nobody is using the data.