Skip to content

Semantic tokens: decode each answer once, for large files #2347

Done

Description

@lagergren

Gap 7 of #2334. #2338 fixes the rest of what that plan needs before semantic tokens drop "experimental"; this is one of the improvements that come after it.

What happens

Code references are to main at 7c6d061.

  • Each pass decodes the answer again. Every highlighting pass decodes the whole token array, even when the answer hasn't changed (SemanticTokensData.highlight). It looks up the colors for each token, and on the PSI path it allocates an array as long as the document (LSPSemanticTokensHighlightVisitor, new LazyHighlightInfo[document.getTextLength()]).
  • The view provider keeps a map entry per character. For TEXT, TextMate and plugin file types without a language, the semantic tokens file view provider keeps one ConcurrentHashMap entry per character of every token, and each pass fills it again (LSPSemanticTokensFileViewProviderHelper).
  • The data is boxed. It arrives as List<Integer>, five integers per token.

Proposal

  • Decode once per answer, into sorted int[] arrays (start, end, key index), and keep them with the answer.
  • Reuse those arrays in every pass and in the view provider, with a binary search instead of a map entry per character.
  • Drop the array as long as the document.

Test

A benchmark-style test with a file of 50,000 tokens:

  • the time to decode it;
  • the allocations;
  • that a second pass without changes doesn't decode again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions