-
Notifications
You must be signed in to change notification settings - Fork 404
[circt-lsp-verilog] "Debounce" onDidChange calls; update in worker #9046
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
Merged
Merged
Changes from all commits
Commits
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
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
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
146 changes: 146 additions & 0 deletions
146
lib/Tools/circt-verilog-lsp-server/Utils/PendingChanges.cpp
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,146 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "PendingChanges.h" | ||
|
|
||
| namespace circt { | ||
| namespace lsp { | ||
|
|
||
| /// Factory: build from server options. Keep mapping 1:1 for clarity. | ||
| DebounceOptions | ||
| DebounceOptions::fromLSPOptions(const circt::lsp::LSPServerOptions &opts) { | ||
| DebounceOptions d; | ||
| d.disableDebounce = opts.disableDebounce; | ||
| d.debounceMinMs = opts.debounceMinMs; | ||
| d.debounceMaxMs = opts.debounceMaxMs; | ||
| return d; | ||
| } | ||
|
|
||
| void PendingChangesMap::abort() { | ||
| std::scoped_lock lock(mu); | ||
| pending.clear(); | ||
| pool.wait(); | ||
| } | ||
|
|
||
| void PendingChangesMap::erase(llvm::StringRef key) { | ||
| std::scoped_lock lock(mu); | ||
| pending.erase(key); | ||
| } | ||
|
|
||
| void PendingChangesMap::erase(const llvm::lsp::URIForFile &uri) { | ||
| auto file = uri.file(); | ||
| if (!file.empty()) | ||
| erase(file); | ||
| } | ||
|
|
||
| void PendingChangesMap::debounceAndUpdate( | ||
| const llvm::lsp::DidChangeTextDocumentParams ¶ms, | ||
| DebounceOptions options, | ||
| std::function<void(std::unique_ptr<PendingChanges>)> cb) { | ||
| enqueueChange(params); | ||
| debounceAndThen(params, options, std::move(cb)); | ||
| } | ||
|
|
||
| void PendingChangesMap::enqueueChange( | ||
| const llvm::lsp::DidChangeTextDocumentParams ¶ms) { | ||
| // Key by normalized LSP file path. If your pipeline allows multiple | ||
| // spellings (symlinks/case), normalize upstream or canonicalize here. | ||
| const auto now = std::chrono::steady_clock::now(); | ||
| const std::string key = params.textDocument.uri.file().str(); | ||
|
|
||
| std::scoped_lock lock(mu); | ||
| PendingChanges &pending = getOrCreateEntry(key); | ||
|
|
||
| pending.changes.insert(pending.changes.end(), params.contentChanges.begin(), | ||
| params.contentChanges.end()); | ||
| pending.version = params.textDocument.version; | ||
| pending.lastChangeTime = now; | ||
|
|
||
| // If this was the first insert after a flush, record start of burst. | ||
| if (pending.changes.size() == params.contentChanges.size()) | ||
| pending.firstChangeTime = now; | ||
| } | ||
|
|
||
| void PendingChangesMap::debounceAndThen( | ||
| const llvm::lsp::DidChangeTextDocumentParams ¶ms, | ||
| DebounceOptions options, | ||
| std::function<void(std::unique_ptr<PendingChanges>)> cb) { | ||
| const std::string key = params.textDocument.uri.file().str(); | ||
| const auto scheduleTime = std::chrono::steady_clock::now(); | ||
|
|
||
| // If debounce is disabled, run on main thread | ||
| if (options.disableDebounce) { | ||
| std::scoped_lock lock(mu); | ||
| auto it = pending.find(key); | ||
| if (it == pending.end()) | ||
| return cb(nullptr); | ||
| return cb(takeAndErase(it)); | ||
| } | ||
|
|
||
| // If debounced, run entirely on the pool; do not block the LSP thread. | ||
| tasks.async([this, key, scheduleTime, options, cb = std::move(cb)]() { | ||
| // Simple timer: sleep min-quiet before checking. We rely on the fact | ||
| // that newer edits can arrive while we sleep, updating lastChangeTime. | ||
| if (options.debounceMinMs > 0) | ||
| std::this_thread::sleep_for( | ||
| std::chrono::milliseconds(options.debounceMinMs)); | ||
|
|
||
| std::unique_ptr<PendingChanges> | ||
| result; // decided under lock, callback after | ||
|
|
||
| { | ||
| std::scoped_lock lock(mu); | ||
| auto it = pending.find(key); | ||
| if (it != pending.end()) { | ||
| PendingChanges &pc = it->second; | ||
| const auto now = std::chrono::steady_clock::now(); | ||
|
|
||
| // quietSinceSchedule: if no newer edits arrived after we scheduled | ||
| // this task, then we consider the burst "quiet" and flush now. | ||
| const bool quietSinceSchedule = (pc.lastChangeTime <= scheduleTime); | ||
|
|
||
| // Apply max-burst cap if configured: force a flush once the total | ||
| // time since first change exceeds the cap. | ||
| bool maxWaitExpired = false; | ||
| if (options.debounceMaxMs > 0) { | ||
| const auto elapsedMs = | ||
| std::chrono::duration_cast<std::chrono::milliseconds>( | ||
| now - pc.firstChangeTime) | ||
| .count(); | ||
| maxWaitExpired = | ||
| static_cast<uint64_t>(elapsedMs) >= options.debounceMaxMs; | ||
| } | ||
|
|
||
| if (quietSinceSchedule || maxWaitExpired) | ||
| result = takeAndErase(it); // flush now | ||
| // else: newer edits arrived; obsolete -> result stays null | ||
| } | ||
| } | ||
|
|
||
| // Invoke outside the lock to avoid deadlocks and allow heavy work. | ||
| cb(std::move(result)); // nullptr => obsolete (no flush) | ||
| }); | ||
| } | ||
|
|
||
| PendingChanges &PendingChangesMap::getOrCreateEntry(std::string_view key) { | ||
| auto it = pending.find(key); | ||
| if (it != pending.end()) | ||
| return it->second; | ||
| auto inserted = pending.try_emplace(key); | ||
| return inserted.first->second; | ||
| } | ||
|
|
||
| std::unique_ptr<PendingChanges> | ||
| PendingChangesMap::takeAndErase(llvm::StringMap<PendingChanges>::iterator it) { | ||
| auto out = std::make_unique<PendingChanges>(std::move(it->second)); | ||
| pending.erase(it); | ||
| return out; | ||
| } | ||
|
|
||
| } // namespace lsp | ||
| } // namespace circt |
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.
Uh oh!
There was an error while loading. Please reload this page.