-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[ty] Enable LRU collection for parsed module #21749
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
Changes from all commits
81f6ec1
82b56fe
11571a9
e983f18
18e2e50
cd78a05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,46 @@ | ||
| use std::sync::{LazyLock, Mutex}; | ||
| use std::cell::RefCell; | ||
|
|
||
| use get_size2::{GetSize, StandardTracker}; | ||
| use ordermap::{OrderMap, OrderSet}; | ||
|
|
||
| thread_local! { | ||
| pub static TRACKER: RefCell<Option<StandardTracker>>= const { RefCell::new(None) }; | ||
| } | ||
|
Comment on lines
+6
to
+8
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would appreciate a comment explaining what on earth is going on here. Also, interesting that this no longer needs to be multi-threaded..?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was never multi-threaded. It's just that it was a |
||
|
|
||
| struct TrackerGuard(Option<StandardTracker>); | ||
|
|
||
| impl Drop for TrackerGuard { | ||
| fn drop(&mut self) { | ||
| TRACKER.set(self.0.take()); | ||
| } | ||
| } | ||
|
|
||
| pub fn attach_tracker<R>(tracker: StandardTracker, f: impl FnOnce() -> R) -> R { | ||
| let prev = TRACKER.replace(Some(tracker)); | ||
| let _guard = TrackerGuard(prev); | ||
| f() | ||
| } | ||
|
|
||
| fn with_tracker<F, R>(f: F) -> R | ||
| where | ||
| F: FnOnce(Option<&mut StandardTracker>) -> R, | ||
| { | ||
| TRACKER.with(|tracker| { | ||
| let mut tracker = tracker.borrow_mut(); | ||
| f(tracker.as_mut()) | ||
| }) | ||
| } | ||
|
|
||
| /// Returns the memory usage of the provided object, using a global tracker to avoid | ||
| /// double-counting shared objects. | ||
| pub fn heap_size<T: GetSize>(value: &T) -> usize { | ||
| static TRACKER: LazyLock<Mutex<StandardTracker>> = | ||
| LazyLock::new(|| Mutex::new(StandardTracker::new())); | ||
|
|
||
| value | ||
| .get_heap_size_with_tracker(&mut *TRACKER.lock().unwrap()) | ||
| .0 | ||
| with_tracker(|tracker| { | ||
| if let Some(tracker) = tracker { | ||
| value.get_heap_size_with_tracker(tracker).0 | ||
| } else { | ||
| value.get_heap_size() | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| /// An implementation of [`GetSize::get_heap_size`] for [`OrderSet`]. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -379,6 +379,16 @@ pub(crate) fn symbols_for_file_global_only(db: &dyn Db, file: File) -> FlatSymbo | |
| global_only: true, | ||
| }; | ||
| visitor.visit_body(&module.syntax().body); | ||
|
|
||
| if file | ||
| .path(db) | ||
| .as_system_path() | ||
| .is_none_or(|path| !db.project().is_file_included(db, path)) | ||
| { | ||
| // Eagerly clear ASTs of third party files. | ||
| parsed.clear(); | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean that each time completions are requested, every file will need to be re-parsed? I checked out this PR and tried it out. I used a single Python source file containing just And with your branch I get: So there still seems to be significant caching happening?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
We still cache The main drawback of this is if we had any other query that iterates over all third-party modules, parses them, and does stuff. But I don't think there's any such query?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I see now. Yeah this is great. And no, I'm not aware of any other queries that do this. I agree we'd have to be careful introducing one. Maybe add a comment here warning of that eventuality? |
||
|
|
||
| FlatSymbols { | ||
| symbols: visitor.symbols, | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.