Skip to content

Commit 6c5b8be

Browse files
committed
refactor(linter): create AllocatorPool in Runtime::new (#13106)
Move creation of `AllocatorPool` into just one place. Previously it was created in various places and passed in to `LintService::new` which passed it to `Runtime::new`. Just create it in `Runtime::new` instead. This does have one substantive effect. Language server was creating `AllocatorPool` with `AllocatorPool::default` (0 slots for `Allocator`s) whereas now it's created with `rayon::current_num_threads()` slots. This doesn't make much difference at present, because the pool creates allocators on demand. But it's going to become important when running JS plugins multi-threaded. At that point it'll be essential that the `AllocatorPool` is created with an accurate thread count, and undefined behavior may result if it isn't.
1 parent 81b0162 commit 6c5b8be

File tree

5 files changed

+12
-19
lines changed

5 files changed

+12
-19
lines changed

apps/oxlint/src/lint.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use ignore::{gitignore::Gitignore, overrides::OverrideBuilder};
1313
use rustc_hash::{FxHashMap, FxHashSet};
1414
use serde_json::Value;
1515

16-
use oxc_allocator::AllocatorPool;
1716
use oxc_diagnostics::{DiagnosticSender, DiagnosticService, GraphicalReportHandler, OxcDiagnostic};
1817
use oxc_linter::{
1918
AllowWarnDeny, Config, ConfigStore, ConfigStoreBuilder, ExternalLinter, ExternalPluginStore,
@@ -338,11 +337,9 @@ impl LintRunner {
338337

339338
let number_of_rules = linter.number_of_rules(self.options.type_aware);
340339

341-
let allocator_pool = AllocatorPool::new(rayon::current_num_threads());
342-
343340
// Spawn linting in another thread so diagnostics can be printed immediately from diagnostic_service.run.
344341
rayon::spawn(move || {
345-
let mut lint_service = LintService::new(linter, allocator_pool, options);
342+
let mut lint_service = LintService::new(linter, options);
346343
lint_service.with_paths(paths);
347344

348345
// Use `RawTransferFileSystem` if `oxlint2` feature is enabled.

crates/oxc_language_server/src/linter/isolated_lint_handler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use tower_lsp_server::{
1010
lsp_types::{self, DiagnosticRelatedInformation, DiagnosticSeverity, Uri},
1111
};
1212

13-
use oxc_allocator::{Allocator, AllocatorPool};
13+
use oxc_allocator::Allocator;
1414
use oxc_linter::{
1515
ConfigStore, LINTABLE_EXTENSIONS, LintOptions, LintService, LintServiceOptions, Linter,
1616
MessageWithPosition, loader::Loader, read_to_arena_str,
@@ -76,7 +76,7 @@ impl IsolatedLintHandler {
7676
lint_service_options = lint_service_options.with_tsconfig(tsconfig_path);
7777
}
7878

79-
let service = LintService::new(linter, AllocatorPool::default(), lint_service_options);
79+
let service = LintService::new(linter, lint_service_options);
8080

8181
Self { service }
8282
}

crates/oxc_linter/src/service/mod.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,8 @@ pub struct LintService {
6666
}
6767

6868
impl LintService {
69-
pub fn new(
70-
linter: Linter,
71-
allocator_pool: oxc_allocator::AllocatorPool,
72-
options: LintServiceOptions,
73-
) -> Self {
74-
let runtime = Runtime::new(linter, allocator_pool, options);
69+
pub fn new(linter: Linter, options: LintServiceOptions) -> Self {
70+
let runtime = Runtime::new(linter, options);
7571
Self { runtime }
7672
}
7773

crates/oxc_linter/src/service/runtime.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,14 @@ mod message_cloner {
240240
use message_cloner::MessageCloner;
241241

242242
impl Runtime {
243-
pub(super) fn new(
244-
linter: Linter,
245-
allocator_pool: AllocatorPool,
246-
options: LintServiceOptions,
247-
) -> Self {
243+
pub(super) fn new(linter: Linter, options: LintServiceOptions) -> Self {
244+
let thread_count = rayon::current_num_threads();
245+
let allocator_pool = AllocatorPool::new(thread_count);
246+
248247
let resolver = options.cross_module.then(|| {
249248
Self::get_resolver(options.tsconfig.or_else(|| Some(options.cwd.join("tsconfig.json"))))
250249
});
250+
251251
Self {
252252
allocator_pool,
253253
cwd: options.cwd,

crates/oxc_linter/src/tester.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_hash::FxHashMap;
1111
use serde::Deserialize;
1212
use serde_json::{Value, json};
1313

14-
use oxc_allocator::{Allocator, AllocatorPool};
14+
use oxc_allocator::Allocator;
1515
use oxc_diagnostics::{GraphicalReportHandler, GraphicalTheme, NamedSource};
1616

1717
use crate::{
@@ -545,7 +545,7 @@ impl Tester {
545545
let cwd = self.current_working_directory.clone();
546546
let paths = vec![Arc::<OsStr>::from(path_to_lint.as_os_str())];
547547
let options = LintServiceOptions::new(cwd).with_cross_module(self.plugins.has_import());
548-
let mut lint_service = LintService::new(linter, AllocatorPool::default(), options);
548+
let mut lint_service = LintService::new(linter, options);
549549
lint_service
550550
.with_file_system(Box::new(TesterFileSystem::new(
551551
path_to_lint,

0 commit comments

Comments
 (0)