Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **Faster diagnostics while editing.** After an edit inside a function or method, only that function is type-checked again, and the rest of the file keeps its diagnostics, moved along with the text. In a 1,500-line class the type checking after such an edit is around 70 times cheaper. Saving still checks the whole file.
- **Faster analysis of methods that check many properties.** Narrowing a property or a call's result no longer walks the method again from its first line.
- **Stub packages are read once.** A large stub package such as `php-stubs/wordpress-stubs` is parsed once for its classes, functions and constants together, instead of again the first time a function is looked up. Analysing a WordPress plugin or theme takes about a quarter less time, and less memory.
- **Memory is returned right after large files are read.** Memory used while reading a large file, such as the single-file `php-stubs/wordpress-stubs`, is returned as soon as the work is done instead of staying held for about ten seconds, or for as long as the server runs. Opening a file in a WordPress plugin settles around 80 MiB instead of 120 to 160 MiB. Contributed by @stubbedev.
- **Faster analysis of by-reference parameters.** Working out what a function leaves in a variable passed by reference reads the function once, instead of once per `return` in it. Analysing WordPress takes about 6% less CPU time.
- **Faster analysis of loops.** A loop body is no longer walked again once its types have settled.
- **Faster analysis of `switch` lookup tables.** A `switch` that assigns a different literal in each case, such as a table of country or currency names, no longer slows down out of proportion to its length. One with 8,000 cases is checked in about a tenth of a second.
Expand Down
21 changes: 21 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,27 @@ pub fn configure_allocator() {
}
}

/// Returns the current thread's free pages to the OS when dropped.
///
/// mimalloc keeps the pages a thread frees in that thread's own heap and
/// only gives them back when the same thread next calls into the
/// allocator. A blocking-pool thread that finishes a burst of work (a
/// first parse of a multi-megabyte stub file, a diagnostic pass) and then
/// parks makes no such call, so everything the burst freed stays resident
/// until Tokio reaps the idle thread ten seconds later. Dropping this at
/// the end of each blocking task hands those pages back while the thread
/// is still running. No-op when mimalloc is not the active allocator.
pub(crate) struct PurgeThreadHeapOnDrop;

impl Drop for PurgeThreadHeapOnDrop {
fn drop(&mut self) {
#[cfg(all(feature = "mimalloc", target_os = "linux"))]
unsafe {
libmimalloc_sys::mi_collect(false);
}
}
}

pub mod analyse;
pub mod atom;
mod backend;
Expand Down
20 changes: 17 additions & 3 deletions src/parser/ast_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ use crate::Backend;

use super::DocblockCtx;

/// The longest source whose parse reuses the thread-local arena; see
/// [`with_reusable_arena`].
const REUSABLE_ARENA_MAX_SOURCE_BYTES: usize = 512 * 1024;

/// Run `f` with a parsing arena, reusing a thread-local `LocalArena` across
/// calls instead of allocating a fresh one each time.
///
Expand All @@ -42,11 +46,21 @@ use super::DocblockCtx;
/// still using the arena). Such re-entrant calls fall back to a throwaway
/// `LocalArena` so the shared arena is never aliased — the borrow held for the
/// duration of `f` makes `try_borrow_mut` fail for the nested call.
fn with_reusable_arena<R>(f: impl FnOnce(&LocalArena) -> R) -> R {
///
/// A reset keeps the arena's last chunk, which grows to the size of
/// everything the arena held before it, so one parse of a multi-megabyte
/// file (a stub package that declares a whole framework in one file)
/// would leave its thread holding tens of megabytes for good. Sources
/// longer than [`REUSABLE_ARENA_MAX_SOURCE_BYTES`] therefore parse in a
/// throwaway arena, which costs nothing next to parsing them.
fn with_reusable_arena<R>(source_len: usize, f: impl FnOnce(&LocalArena) -> R) -> R {
thread_local! {
static ARENA: RefCell<LocalArena> = const { RefCell::new(LocalArena::new()) };
}

if source_len > REUSABLE_ARENA_MAX_SOURCE_BYTES {
return f(&LocalArena::new());
}
ARENA.with(|cell| match cell.try_borrow_mut() {
Ok(mut arena) => {
arena.reset();
Expand Down Expand Up @@ -219,7 +233,7 @@ impl Backend {
parse_errors = errors;
program
});
let update = with_reusable_arena(|arena| {
let update = with_reusable_arena(parsed.content().len(), |arena| {
self.ast_index_update_from(
arena,
uri,
Expand Down Expand Up @@ -447,7 +461,7 @@ impl Backend {
}

fn build_ast_index_update(&self, uri: &str, content: &str) -> AstIndexUpdate {
with_reusable_arena(|arena| {
with_reusable_arena(content.len(), |arena| {
let (parse_errors, program) = parse_with_errors(arena, content);
self.ast_index_update_from(arena, uri, content, parse_errors, program)
})
Expand Down
6 changes: 5 additions & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
match tokio::spawn(async move { tokio::task::spawn_blocking(f).await }).await {
let blocking = move || {
let _purge = crate::PurgeThreadHeapOnDrop;
f()
};
match tokio::spawn(async move { tokio::task::spawn_blocking(blocking).await }).await {
Ok(Ok(value)) => Some(value),
// The inner blocking task panicked, or the outer task carrying it did.
// Either way the request answers with its fallback; without this log
Expand Down
Loading