generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 31
optimize: caching + minor opts #79
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
Open
Hoblovski
wants to merge
5
commits into
main
Choose a base branch
from
feat/misc-opts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5cb3ada
refactor: split Collect into phases
Hoblovski 3a6c8f8
feat: add command line option -include
Hoblovski 7b7e911
feat: generic and transparent LSP request caching
Hoblovski 1eb5388
feat: optional profiling
Hoblovski 0ff5177
opt: group syms to speedup retrieval and filtering
Hoblovski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2025 CloudWeGo Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package collect | ||
|
||
import "github.com/cloudwego/abcoder/lang/lsp" | ||
|
||
func isFuncLike(sk lsp.SymbolKind) bool { | ||
return sk == lsp.SKFunction || sk == lsp.SKMethod | ||
// SKConstructor ? | ||
} | ||
|
||
func isTypeLike(sk lsp.SymbolKind) bool { | ||
return sk == lsp.SKClass || sk == lsp.SKStruct || sk == lsp.SKInterface || sk == lsp.SKEnum | ||
} | ||
|
||
func isVarLike(sk lsp.SymbolKind) bool { | ||
return sk == lsp.SKVariable || sk == lsp.SKConstant | ||
// sk == lsp.SKField || sk == lsp.SKProperty ? | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Copyright 2025 CloudWeGo Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package lsp | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"os" | ||
"sync" | ||
"time" | ||
|
||
"github.com/cloudwego/abcoder/lang/log" | ||
) | ||
|
||
type LSPRequestCache struct { | ||
cachePath string | ||
cacheInterval int | ||
mu sync.Mutex | ||
cache map[string]map[string]json.RawMessage // method -> params -> result | ||
cancel context.CancelFunc | ||
} | ||
|
||
func NewLSPRequestCache(path string, interval int) *LSPRequestCache { | ||
c := &LSPRequestCache{ | ||
cachePath: path, | ||
cacheInterval: interval, | ||
cache: make(map[string]map[string]json.RawMessage), | ||
} | ||
c.Init() | ||
return c | ||
} | ||
|
||
func (c *LSPRequestCache) Init() { | ||
if c.cachePath == "" { | ||
return | ||
} | ||
if err := c.loadCacheFromDisk(); err != nil { | ||
log.Error("failed to load LSP cache from disk: %v", err) | ||
} else { | ||
log.Info("LSP cache loaded from disk") | ||
} | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
c.cancel = cancel | ||
go c.PeriodicCacheSaver(ctx) | ||
} | ||
|
||
func (c *LSPRequestCache) Close() { | ||
if c.cancel != nil { | ||
c.cancel() | ||
} | ||
} | ||
|
||
func (c *LSPRequestCache) saveCacheToDisk() error { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
data, err := json.Marshal(c.cache) | ||
if err != nil { | ||
return err | ||
} | ||
return os.WriteFile(c.cachePath, data, 0644) | ||
} | ||
|
||
func (c *LSPRequestCache) loadCacheFromDisk() error { | ||
data, err := os.ReadFile(c.cachePath) | ||
if err != nil { | ||
return err | ||
} | ||
if err := json.Unmarshal(data, &c.cache); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (cli *LSPRequestCache) PeriodicCacheSaver(ctx context.Context) { | ||
go func() { | ||
ticker := time.NewTicker(time.Duration(cli.cacheInterval) * time.Second) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case <-ticker.C: | ||
if err := cli.saveCacheToDisk(); err != nil { | ||
log.Error("failed to save LSP cache to disk: %v", err) | ||
} else { | ||
log.Info("LSP cache saved to disk") | ||
} | ||
case <-ctx.Done(): | ||
log.Info("LSP cache saver cancelled, shutting down.") | ||
return | ||
} | ||
} | ||
}() | ||
} | ||
|
||
func (cli *LSPRequestCache) Get(method, params string) (json.RawMessage, bool) { | ||
cli.mu.Lock() | ||
defer cli.mu.Unlock() | ||
if methodCache, ok := cli.cache[method]; ok { | ||
if result, ok := methodCache[params]; ok { | ||
return result, true | ||
} | ||
} | ||
return nil, false | ||
} | ||
|
||
func (cli *LSPRequestCache) Set(method, params string, result json.RawMessage) { | ||
cli.mu.Lock() | ||
defer cli.mu.Unlock() | ||
methodCache, ok := cli.cache[method] | ||
if !ok { | ||
methodCache = make(map[string]json.RawMessage) | ||
cli.cache[method] = methodCache | ||
} | ||
methodCache[params] = result | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Who is the caller of these three functions?
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.
skipTokenForDependency and getSymbolByLocation in Collect.go. I thought this might help with clarifying & maintaining consistency tho.