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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apps/oxlint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test = false
doctest = false

[dependencies]
oxc_allocator = { workspace = true }
oxc_diagnostics = { workspace = true }
oxc_linter = { workspace = true }
oxc_span = { workspace = true }
Expand Down
5 changes: 4 additions & 1 deletion apps/oxlint/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::{

use cow_utils::CowUtils;
use ignore::{gitignore::Gitignore, overrides::OverrideBuilder};
use oxc_allocator::AllocatorPool;
use oxc_diagnostics::{DiagnosticService, GraphicalReportHandler, OxcDiagnostic};
use oxc_linter::{
AllowWarnDeny, Config, ConfigStore, ConfigStoreBuilder, InvalidFilterKind, LintFilter,
Expand Down Expand Up @@ -290,9 +291,11 @@ impl Runner for LintRunner {

let number_of_rules = linter.number_of_rules();

let allocator_pool = AllocatorPool::new(rayon::current_num_threads());

// Spawn linting in another thread so diagnostics can be printed immediately from diagnostic_service.run.
rayon::spawn(move || {
let mut lint_service = LintService::new(&linter, options);
let mut lint_service = LintService::new(&linter, allocator_pool, options);
lint_service.run(&tx_error);
});

Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_allocator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ mod convert;
#[cfg(feature = "from_raw_parts")]
mod from_raw_parts;
pub mod hash_map;
mod pool;
mod string_builder;
mod take_in;
mod vec;
Expand All @@ -44,6 +45,7 @@ pub use boxed::Box;
pub use clone_in::CloneIn;
pub use convert::{FromIn, IntoIn};
pub use hash_map::HashMap;
pub use pool::{AllocatorGuard, AllocatorPool};
pub use string_builder::StringBuilder;
pub use take_in::{Dummy, TakeIn};
pub use vec::Vec;
71 changes: 71 additions & 0 deletions crates/oxc_allocator/src/pool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use std::mem::ManuallyDrop;
use std::sync::{Arc, Mutex};

use crate::Allocator;

/// A thread-safe pool for reusing `Allocator` instances to reduce allocation overhead.
///
/// Internally uses a `Vec` protected by a `Mutex` to store available allocators.
#[derive(Default)]
pub struct AllocatorPool {
allocators: Arc<Mutex<Vec<Allocator>>>,
}

/// A guard object representing exclusive access to an `Allocator` from the pool.
///
/// On drop, the `Allocator` is reset and returned to the pool.
pub struct AllocatorGuard {
allocator: ManuallyDrop<Allocator>,
pool: Arc<Mutex<Vec<Allocator>>>,
}

impl AllocatorPool {
/// Creates a new `AllocatorPool` pre-filled with the given number of default `Allocator` instances.
pub fn new(size: usize) -> AllocatorPool {
let allocators = std::iter::repeat_with(Allocator::default).take(size).collect();
AllocatorPool { allocators: Arc::new(Mutex::new(allocators)) }
}

/// Retrieves an `Allocator` from the pool, or creates a new one if the pool is empty.
///
/// Returns an `AllocatorPoolGuard` that gives mutable access to the allocator.
///
/// # Panics
///
/// Panics if the underlying mutex is poisoned.
pub fn get(&self) -> AllocatorGuard {
let allocator = {
let mut allocators = self.allocators.lock().unwrap();
allocators.pop().unwrap_or_default()
};

AllocatorGuard {
allocator: ManuallyDrop::new(allocator),
pool: Arc::clone(&self.allocators),
}
}
}

impl std::ops::Deref for AllocatorGuard {
type Target = Allocator;

fn deref(&self) -> &Self::Target {
&self.allocator
}
}

impl std::ops::DerefMut for AllocatorGuard {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.allocator
}
}

impl Drop for AllocatorGuard {
fn drop(&mut self) {
// SAFETY: we're taking ownership and promise not to drop `allocator` again.
let mut allocator = unsafe { ManuallyDrop::take(&mut self.allocator) };
allocator.reset();
let mut allocators = self.pool.lock().unwrap();
allocators.push(allocator);
}
}
10 changes: 6 additions & 4 deletions crates/oxc_language_server/src/linter/isolated_lint_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tower_lsp_server::{
lsp_types::{self, DiagnosticRelatedInformation, DiagnosticSeverity, Uri},
};

use oxc_allocator::Allocator;
use oxc_allocator::{Allocator, AllocatorPool};
use oxc_linter::RuntimeFileSystem;
use oxc_linter::{
LINTABLE_EXTENSIONS, LintService, LintServiceOptions, Linter, MessageWithPosition,
Expand Down Expand Up @@ -140,9 +140,11 @@ impl IsolatedLintHandler {
.with_cross_module(self.options.use_cross_module);

let mut lint_service =
LintService::new(&self.linter, lint_service_options).with_file_system(Box::new(
IsolatedLintHandlerFileSystem::new(path.to_path_buf(), source_text),
));
LintService::new(&self.linter, AllocatorPool::default(), lint_service_options)
.with_file_system(Box::new(IsolatedLintHandlerFileSystem::new(
path.to_path_buf(),
source_text,
)));
let result = lint_service.run_source(allocator);

Some(result)
Expand Down
8 changes: 6 additions & 2 deletions crates/oxc_linter/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ pub struct LintService<'l> {
}

impl<'l> LintService<'l> {
pub fn new(linter: &'l Linter, options: LintServiceOptions) -> Self {
let runtime = Runtime::new(linter, options);
pub fn new(
linter: &'l Linter,
allocator_pool: oxc_allocator::AllocatorPool,
options: LintServiceOptions,
) -> Self {
let runtime = Runtime::new(linter, allocator_pool, options);
Self { runtime }
}

Expand Down
17 changes: 13 additions & 4 deletions crates/oxc_linter/src/service/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};
use self_cell::self_cell;
use smallvec::SmallVec;

use oxc_allocator::Allocator;
use oxc_allocator::{Allocator, AllocatorGuard, AllocatorPool};
use oxc_diagnostics::{DiagnosticSender, DiagnosticService, Error, OxcDiagnostic};
use oxc_parser::{ParseOptions, Parser};
use oxc_resolver::Resolver;
Expand All @@ -42,6 +42,8 @@ pub struct Runtime<'l> {
resolver: Option<Resolver>,

pub(super) file_system: Box<dyn RuntimeFileSystem + Sync + Send>,

allocator_pool: AllocatorPool,
}

/// Output of `Runtime::process_path`
Expand Down Expand Up @@ -95,7 +97,7 @@ unsafe impl Send for ModuleContent {}

struct ModuleContentOwner {
source_text: String,
allocator: Allocator,
allocator: AllocatorGuard,
}

/// source text and semantic for each source section. They are in the same order as `ProcessedModule.section_module_records`
Expand Down Expand Up @@ -160,11 +162,16 @@ impl RuntimeFileSystem for OsFileSystem {
}

impl<'l> Runtime<'l> {
pub(super) fn new(linter: &'l Linter, options: LintServiceOptions) -> Self {
pub(super) fn new(
linter: &'l Linter,
allocator_pool: AllocatorPool,
options: LintServiceOptions,
) -> Self {
let resolver = options.cross_module.then(|| {
Self::get_resolver(options.tsconfig.or_else(|| Some(options.cwd.join("tsconfig.json"))))
});
Self {
allocator_pool,
cwd: options.cwd,
paths: options.paths.iter().cloned().collect(),
linter,
Expand Down Expand Up @@ -771,7 +778,9 @@ impl<'l> Runtime<'l> {
};
let mut records = SmallVec::<[Result<ResolvedModuleRecord, Vec<OxcDiagnostic>>; 1]>::new();
let mut module_content: Option<ModuleContent> = None;
let allocator = Allocator::default();

let allocator = self.allocator_pool.get();

if self.paths.contains(path) {
module_content =
Some(ModuleContent::new(ModuleContentOwner { source_text, allocator }, |owner| {
Expand Down
9 changes: 5 additions & 4 deletions crates/oxc_linter/src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
};

use cow_utils::CowUtils;
use oxc_allocator::Allocator;
use oxc_allocator::{Allocator, AllocatorPool};
use oxc_diagnostics::{GraphicalReportHandler, GraphicalTheme, NamedSource};
use rustc_hash::FxHashMap;
use serde::Deserialize;
Expand Down Expand Up @@ -530,9 +530,10 @@ impl Tester {
let paths = vec![Arc::<OsStr>::from(path_to_lint.as_os_str())];
let options =
LintServiceOptions::new(cwd, paths).with_cross_module(self.plugins.has_import());
let mut lint_service = LintService::new(&linter, options).with_file_system(Box::new(
TesterFileSystem::new(path_to_lint, source_text.to_string()),
));
let mut lint_service =
LintService::new(&linter, AllocatorPool::default(), options).with_file_system(
Box::new(TesterFileSystem::new(path_to_lint, source_text.to_string())),
);

let (sender, _receiver) = mpsc::channel();
let result = lint_service.run_test_source(&allocator, false, &sender);
Expand Down
Loading