Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache for binary search in lines-map #640

Open
5nord opened this issue Oct 2, 2022 · 6 comments
Open

Add cache for binary search in lines-map #640

5nord opened this issue Oct 2, 2022 · 6 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@5nord
Copy link
Member

5nord commented Oct 2, 2022

The method syntax.Node.Span uses binary search to convert a file offset into a line number.
Location heavy tools, like our language server or formatter, would benefit from line-caches. For example like so:

func (t *tree) position(pos int) Position {
    if t.cachedLineBegin <= pos && pos < t.cachedLineEnd {
        line = t.cachedLine
    }
   // ...
}

To be efficient we probably need two separate line-caches, one for n.Pos() and one for n.End(). Best would be creating benchmarks comparing random position access, accessing only begin-offsets and accessing whole spans (begin- and end-offset).

@5nord 5nord added enhancement New feature or request good first issue Good for newcomers labels Oct 2, 2022
@5nord
Copy link
Member Author

5nord commented Oct 8, 2022

Added benchmarks in #646

@nitinNT: Would this maybe interest you?

@nitinNT
Copy link

nitinNT commented Oct 10, 2022

yes, i will try

@nitinNT
Copy link

nitinNT commented Oct 17, 2022

@5nord : i have started working on this issue and i have noted the things which i need TODO and here are things

  • modify tree struct present in syntax.go by adding fields (cachedLineBegin ,cachedLineEnd,cachedLine) and all of them will be int.
  • modify searchLines function in syntax.go and modify newly added fields mentioned in above step according to binary search.

am i correct in above steps?

@5nord
Copy link
Member Author

5nord commented Oct 17, 2022

Sounds good to me.

What you could do is executing the benchmarks before and after your change to see how big the improvement is.

@nitinNT
Copy link

nitinNT commented Oct 29, 2022

@5nord i have tried the above approach but it seems its not improving the performance. can you little guide me?

@5nord
Copy link
Member Author

5nord commented Oct 29, 2022

No worries. It happens sometimes that an idea is not as good as we initially thought. That's why we measure first. And a negative result can still be a valuable result 🙂

Sure I can help you. I suggest you push your PR as a draft and we'll have a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants