Skip to content

Commit

Permalink
perf: try shrinking the enum size for RuleEnum from 168 to 16 bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Dec 22, 2023
1 parent ed67928 commit dde4b70
Show file tree
Hide file tree
Showing 21 changed files with 331 additions and 121 deletions.
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 crates/oxc_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ oxc_macros = { workspace = true }
oxc_semantic = { workspace = true }
oxc_syntax = { workspace = true }
oxc_formatter = { workspace = true }
oxc_index = { workspace = true }
oxc_resolver = { version = "1.0.1" }

rayon = { workspace = true }
Expand Down
11 changes: 11 additions & 0 deletions crates/oxc_linter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ pub use crate::{
};
pub(crate) use rules::{RuleEnum, RULES};

#[cfg(target_pointer_width = "64")]
#[test]
fn size_asserts() {
use oxc_index::assert_eq_size;

// `RuleEnum` runs in a really tight loop, make sure it is small for CPU cache.
// A reduction from 168 bytes to 16 results 15% performance improvement.
// See codspeed in https://github.com/oxc-project/oxc/pull/1783
assert_eq_size!(RuleEnum, [u8; 16]);
}

#[derive(Debug, Clone)]
pub struct LintSettings {
jsx_a11y: JsxA11y,
Expand Down
68 changes: 34 additions & 34 deletions crates/oxc_linter/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ mod eslint {
pub mod getter_return;
pub mod no_array_constructor;
pub mod no_async_promise_executor;
// pub mod no_bitwise;
pub mod no_bitwise;
pub mod no_caller;
pub mod no_case_declarations;
pub mod no_class_assign;
pub mod no_compare_neg_zero;
pub mod no_cond_assign;
// pub mod no_console;
pub mod no_console;
pub mod no_const_assign;
pub mod no_constant_binary_expression;
pub mod no_constant_condition;
Expand All @@ -62,11 +62,11 @@ mod eslint {
pub mod no_extra_boolean_cast;
pub mod no_fallthrough;
pub mod no_func_assign;
// pub mod no_global_assign;
pub mod no_global_assign;
pub mod no_import_assign;
pub mod no_inner_declarations;
pub mod no_loss_of_precision;
// pub mod no_mixed_operators;
pub mod no_mixed_operators;
pub mod no_new_symbol;
pub mod no_obj_calls;
pub mod no_prototype_builtins;
Expand All @@ -92,7 +92,7 @@ mod eslint {

mod typescript {
pub mod adjacent_overload_signatures;
// pub mod ban_ts_comment;
pub mod ban_ts_comment;
pub mod ban_types;
pub mod consistent_type_exports;
pub mod no_duplicate_enum_values;
Expand All @@ -102,36 +102,36 @@ mod typescript {
pub mod no_misused_new;
pub mod no_namespace;
pub mod no_non_null_asserted_optional_chain;
// pub mod no_this_alias;
pub mod no_this_alias;
pub mod no_unnecessary_type_constraint;
pub mod no_unsafe_declaration_merging;
pub mod no_var_requires;
pub mod prefer_as_const;
}

mod jest {
// pub mod expect_expect;
pub mod expect_expect;
pub mod max_expects;
pub mod no_alias_methods;
pub mod no_commented_out_tests;
pub mod no_conditional_expect;
pub mod no_confusing_set_timeout;
// pub mod no_deprecated_functions;
pub mod no_deprecated_functions;
pub mod no_disabled_tests;
pub mod no_done_callback;
pub mod no_export;
pub mod no_focused_tests;
// pub mod no_hooks;
pub mod no_hooks;
pub mod no_identical_title;
pub mod no_interpolation_in_snapshots;
pub mod no_jasmine_globals;
pub mod no_mocks_import;
// pub mod no_standalone_expect;
pub mod no_standalone_expect;
pub mod no_test_prefixes;
pub mod prefer_todo;
pub mod valid_describe_callback;
// pub mod valid_expect;
// pub mod valid_title;
pub mod valid_expect;
pub mod valid_title;
}

mod react {
Expand All @@ -150,7 +150,7 @@ mod react {
}

mod unicorn {
// pub mod catch_error_name;
pub mod catch_error_name;
pub mod empty_brace_spaces;
pub mod error_message;
pub mod escape_case;
Expand Down Expand Up @@ -188,7 +188,7 @@ mod unicorn {
pub mod no_useless_switch_case;
pub mod no_zero_fractions;
pub mod number_literal_case;
// pub mod numeric_separators_style;
pub mod numeric_separators_style;
pub mod prefer_add_event_listener;
pub mod prefer_array_flat;
pub mod prefer_array_flat_map;
Expand Down Expand Up @@ -228,13 +228,13 @@ mod unicorn {
}

mod jsx_a11y {
// pub mod alt_text;
pub mod alt_text;
pub mod anchor_has_content;
pub mod anchor_is_valid;
// pub mod heading_has_content;
pub mod heading_has_content;
pub mod html_has_lang;
pub mod iframe_has_title;
// pub mod img_redundant_alt;
pub mod img_redundant_alt;
pub mod no_access_key;
pub mod no_autofocus;
pub mod no_distracting_elements;
Expand Down Expand Up @@ -266,13 +266,13 @@ oxc_macros::declare_all_lint_rules! {
eslint::getter_return,
eslint::no_array_constructor,
eslint::no_async_promise_executor,
// eslint::no_bitwise,
eslint::no_bitwise,
eslint::no_caller,
eslint::no_case_declarations,
eslint::no_class_assign,
eslint::no_compare_neg_zero,
eslint::no_cond_assign,
// eslint::no_console,
eslint::no_console,
eslint::no_const_assign,
eslint::no_constant_binary_expression,
eslint::no_constant_condition,
Expand All @@ -292,11 +292,11 @@ oxc_macros::declare_all_lint_rules! {
eslint::no_extra_boolean_cast,
eslint::no_fallthrough,
eslint::no_func_assign,
// eslint::no_global_assign,
eslint::no_global_assign,
eslint::no_import_assign,
eslint::no_inner_declarations,
eslint::no_loss_of_precision,
// eslint::no_mixed_operators,
eslint::no_mixed_operators,
eslint::no_new_symbol,
eslint::no_obj_calls,
eslint::no_prototype_builtins,
Expand All @@ -319,7 +319,7 @@ oxc_macros::declare_all_lint_rules! {
eslint::use_isnan,
eslint::valid_typeof,
typescript::adjacent_overload_signatures,
// typescript::ban_ts_comment,
typescript::ban_ts_comment,
typescript::ban_types,
typescript::consistent_type_exports,
typescript::no_duplicate_enum_values,
Expand All @@ -329,34 +329,34 @@ oxc_macros::declare_all_lint_rules! {
typescript::no_misused_new,
typescript::no_namespace,
typescript::no_non_null_asserted_optional_chain,
// typescript::no_this_alias,
typescript::no_this_alias,
typescript::no_unnecessary_type_constraint,
typescript::no_unsafe_declaration_merging,
typescript::no_var_requires,
typescript::prefer_as_const,
// jest::expect_expect,
jest::expect_expect,
jest::max_expects,
jest::no_alias_methods,
jest::no_commented_out_tests,
jest::no_conditional_expect,
jest::no_confusing_set_timeout,
// jest::no_deprecated_functions,
jest::no_deprecated_functions,
jest::no_disabled_tests,
jest::no_done_callback,
jest::no_export,
jest::no_focused_tests,
// jest::no_hooks,
jest::no_hooks,
jest::no_identical_title,
jest::no_interpolation_in_snapshots,
jest::no_jasmine_globals,
jest::no_mocks_import,
// jest::no_standalone_expect,
jest::no_standalone_expect,
jest::no_test_prefixes,
jest::prefer_todo,
jest::valid_describe_callback,
// jest::valid_expect,
// jest::valid_title,
// unicorn::catch_error_name,
jest::valid_expect,
jest::valid_title,
unicorn::catch_error_name,
unicorn::prefer_node_protocol,
unicorn::empty_brace_spaces,
unicorn::error_message,
Expand Down Expand Up @@ -394,7 +394,7 @@ oxc_macros::declare_all_lint_rules! {
unicorn::no_useless_switch_case,
unicorn::no_zero_fractions,
unicorn::number_literal_case,
// unicorn::numeric_separators_style,
unicorn::numeric_separators_style,
unicorn::prefer_add_event_listener,
unicorn::prefer_array_flat_map,
unicorn::prefer_array_flat,
Expand Down Expand Up @@ -449,13 +449,13 @@ oxc_macros::declare_all_lint_rules! {
import::no_self_import,
import::no_amd,
import::export,
// jsx_a11y::alt_text,
jsx_a11y::alt_text,
jsx_a11y::anchor_has_content,
jsx_a11y::anchor_is_valid,
// jsx_a11y::heading_has_content,
jsx_a11y::heading_has_content,
jsx_a11y::html_has_lang,
jsx_a11y::iframe_has_title,
// jsx_a11y::img_redundant_alt,
jsx_a11y::img_redundant_alt,
jsx_a11y::no_access_key,
jsx_a11y::no_autofocus,
jsx_a11y::scope,
Expand Down
17 changes: 14 additions & 3 deletions crates/oxc_linter/src/rules/eslint/no_bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,22 @@ use crate::{context::LintContext, rule::Rule, AstNode};
struct NoBitwiseDiagnostic(&'static str, #[label] pub Span);

#[derive(Debug, Default, Clone)]
pub struct NoBitwise {
pub struct NoBitwise(Box<NoBitwiseConfig>);

#[derive(Debug, Default, Clone)]
pub struct NoBitwiseConfig {
allow: Vec<String>,
int32_hint: bool,
}

impl std::ops::Deref for NoBitwise {
type Target = NoBitwiseConfig;

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

declare_oxc_lint!(
/// ### What it does
///
Expand All @@ -46,7 +57,7 @@ impl Rule for NoBitwise {
fn from_configuration(value: serde_json::Value) -> Self {
let obj = value.get(0);

Self {
Self(Box::new(NoBitwiseConfig {
allow: obj
.and_then(|v| v.get("allow"))
.and_then(serde_json::Value::as_array)
Expand All @@ -61,7 +72,7 @@ impl Rule for NoBitwise {
.and_then(|v| v.get("int32Hint"))
.and_then(serde_json::Value::as_bool)
.unwrap_or_default(),
}
}))
}

fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
Expand Down
17 changes: 14 additions & 3 deletions crates/oxc_linter/src/rules/eslint/no_console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ use crate::{context::LintContext, rule::Rule, AstNode};
struct NoConsoleDiagnostic(#[label] pub Span);

#[derive(Debug, Default, Clone)]
pub struct NoConsole {
pub struct NoConsole(Box<NoConsoleConfig>);

#[derive(Debug, Default, Clone)]
pub struct NoConsoleConfig {
/// A list of methods allowed to be used.
///
/// ```javascript
Expand All @@ -25,6 +28,14 @@ pub struct NoConsole {
pub allow: Vec<String>,
}

impl std::ops::Deref for NoConsole {
type Target = NoConsoleConfig;

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

declare_oxc_lint!(
/// ### What it does
/// Disallows using the global console object.
Expand All @@ -45,7 +56,7 @@ declare_oxc_lint!(

impl Rule for NoConsole {
fn from_configuration(value: serde_json::Value) -> Self {
Self {
Self(Box::new(NoConsoleConfig {
allow: value
.get(0)
.and_then(|v| v.get("allow"))
Expand All @@ -57,7 +68,7 @@ impl Rule for NoConsole {
.collect()
})
.unwrap_or_default(),
}
}))
}

fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
Expand Down
17 changes: 14 additions & 3 deletions crates/oxc_linter/src/rules/eslint/no_global_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,21 @@ struct NoGlobalAssignDiagnostic(
);

#[derive(Debug, Default, Clone)]
pub struct NoGlobalAssign {
pub struct NoGlobalAssign(Box<NoGlobalAssignConfig>);

#[derive(Debug, Default, Clone)]
pub struct NoGlobalAssignConfig {
excludes: Vec<Atom>,
}

impl std::ops::Deref for NoGlobalAssign {
type Target = NoGlobalAssignConfig;

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

declare_oxc_lint!(
/// ### What it does
/// Disallow modifications to read-only global variables.
Expand All @@ -39,7 +50,7 @@ impl Rule for NoGlobalAssign {
fn from_configuration(value: serde_json::Value) -> Self {
let obj = value.get(0);

Self {
Self(Box::new(NoGlobalAssignConfig {
excludes: obj
.and_then(|v| v.get("exceptions"))
.and_then(serde_json::Value::as_array)
Expand All @@ -49,7 +60,7 @@ impl Rule for NoGlobalAssign {
.filter(std::option::Option::is_some)
.map(|x| Atom::from(x.unwrap().to_string()))
.collect::<Vec<Atom>>(),
}
}))
}

fn run_once(&self, ctx: &LintContext) {
Expand Down
Loading

0 comments on commit dde4b70

Please sign in to comment.