mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 13:51:16 +00:00
use owned ast and tokens in bench (#11598)
This commit is contained in:
parent
e14096f0a8
commit
921bc15542
3 changed files with 39 additions and 63 deletions
|
@ -60,7 +60,9 @@ fn benchmark_linter(mut group: BenchmarkGroup, settings: &LinterSettings) {
|
||||||
// Parse the source.
|
// Parse the source.
|
||||||
let ast = parse_program_tokens(tokens.clone(), case.code(), false).unwrap();
|
let ast = parse_program_tokens(tokens.clone(), case.code(), false).unwrap();
|
||||||
|
|
||||||
b.iter(|| {
|
b.iter_batched(
|
||||||
|
|| (ast.clone(), tokens.clone()),
|
||||||
|
|(ast, tokens)| {
|
||||||
let path = case.path();
|
let path = case.path();
|
||||||
let result = lint_only(
|
let result = lint_only(
|
||||||
&path,
|
&path,
|
||||||
|
@ -69,15 +71,14 @@ fn benchmark_linter(mut group: BenchmarkGroup, settings: &LinterSettings) {
|
||||||
flags::Noqa::Enabled,
|
flags::Noqa::Enabled,
|
||||||
&SourceKind::Python(case.code().to_string()),
|
&SourceKind::Python(case.code().to_string()),
|
||||||
PySourceType::from(path.as_path()),
|
PySourceType::from(path.as_path()),
|
||||||
ParseSource::Precomputed {
|
ParseSource::Precomputed { tokens, ast },
|
||||||
tokens: &tokens,
|
|
||||||
ast: &ast,
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Assert that file contains no parse errors
|
// Assert that file contains no parse errors
|
||||||
assert_eq!(result.error, None);
|
assert_eq!(result.error, None);
|
||||||
});
|
},
|
||||||
|
criterion::BatchSize::SmallInput,
|
||||||
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -146,7 +146,7 @@ pub fn check_path(
|
||||||
.any(|rule_code| rule_code.lint_source().is_imports());
|
.any(|rule_code| rule_code.lint_source().is_imports());
|
||||||
if use_ast || use_imports || use_doc_lines {
|
if use_ast || use_imports || use_doc_lines {
|
||||||
// Parse, if the AST wasn't pre-provided provided.
|
// Parse, if the AST wasn't pre-provided provided.
|
||||||
match tokens.into_ast_source(source_kind, source_type) {
|
match tokens.into_ast(source_kind, source_type) {
|
||||||
Ok(python_ast) => {
|
Ok(python_ast) => {
|
||||||
let cell_offsets = source_kind.as_ipy_notebook().map(Notebook::cell_offsets);
|
let cell_offsets = source_kind.as_ipy_notebook().map(Notebook::cell_offsets);
|
||||||
let notebook_index = source_kind.as_ipy_notebook().map(Notebook::index);
|
let notebook_index = source_kind.as_ipy_notebook().map(Notebook::index);
|
||||||
|
@ -684,23 +684,16 @@ This indicates a bug in Ruff. If you could open an issue at:
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum ParseSource<'a> {
|
pub enum ParseSource {
|
||||||
/// Extract the tokens and AST from the given source code.
|
/// Extract the tokens and AST from the given source code.
|
||||||
None,
|
None,
|
||||||
/// Use the precomputed tokens and AST.
|
/// Use the precomputed tokens and AST.
|
||||||
Precomputed {
|
Precomputed { tokens: Tokens, ast: Suite },
|
||||||
tokens: &'a [LexResult],
|
|
||||||
ast: &'a Suite,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ParseSource<'a> {
|
impl ParseSource {
|
||||||
/// Convert to a [`TokenSource`], tokenizing if necessary.
|
/// Convert to a [`TokenSource`], tokenizing if necessary.
|
||||||
fn into_token_source(
|
fn into_token_source(self, source_kind: &SourceKind, source_type: PySourceType) -> TokenSource {
|
||||||
self,
|
|
||||||
source_kind: &SourceKind,
|
|
||||||
source_type: PySourceType,
|
|
||||||
) -> TokenSource<'a> {
|
|
||||||
match self {
|
match self {
|
||||||
Self::None => TokenSource::Tokens(ruff_python_parser::tokenize(
|
Self::None => TokenSource::Tokens(ruff_python_parser::tokenize(
|
||||||
source_kind.source_code(),
|
source_kind.source_code(),
|
||||||
|
@ -712,17 +705,14 @@ impl<'a> ParseSource<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum TokenSource<'a> {
|
pub enum TokenSource {
|
||||||
/// Use the precomputed tokens to generate the AST.
|
/// Use the precomputed tokens to generate the AST.
|
||||||
Tokens(Tokens),
|
Tokens(Tokens),
|
||||||
/// Use the precomputed tokens and AST.
|
/// Use the precomputed tokens and AST.
|
||||||
Precomputed {
|
Precomputed { tokens: Tokens, ast: Suite },
|
||||||
tokens: &'a [LexResult],
|
|
||||||
ast: &'a Suite,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TokenSource<'_> {
|
impl TokenSource {
|
||||||
/// Returns an iterator over the [`TokenKind`] and the corresponding range.
|
/// Returns an iterator over the [`TokenKind`] and the corresponding range.
|
||||||
///
|
///
|
||||||
/// [`TokenKind`]: ruff_python_parser::TokenKind
|
/// [`TokenKind`]: ruff_python_parser::TokenKind
|
||||||
|
@ -734,7 +724,7 @@ impl TokenSource<'_> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Deref for TokenSource<'_> {
|
impl Deref for TokenSource {
|
||||||
type Target = [LexResult];
|
type Target = [LexResult];
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
|
@ -745,39 +735,20 @@ impl Deref for TokenSource<'_> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> TokenSource<'a> {
|
impl TokenSource {
|
||||||
/// Convert to an [`AstSource`], parsing if necessary.
|
/// Convert to an [`AstSource`], parsing if necessary.
|
||||||
fn into_ast_source(
|
fn into_ast(
|
||||||
self,
|
self,
|
||||||
source_kind: &SourceKind,
|
source_kind: &SourceKind,
|
||||||
source_type: PySourceType,
|
source_type: PySourceType,
|
||||||
) -> Result<AstSource<'a>, ParseError> {
|
) -> Result<Suite, ParseError> {
|
||||||
match self {
|
match self {
|
||||||
Self::Tokens(tokens) => Ok(AstSource::Ast(ruff_python_parser::parse_program_tokens(
|
Self::Tokens(tokens) => Ok(ruff_python_parser::parse_program_tokens(
|
||||||
tokens,
|
tokens,
|
||||||
source_kind.source_code(),
|
source_kind.source_code(),
|
||||||
source_type.is_ipynb(),
|
source_type.is_ipynb(),
|
||||||
)?)),
|
)?),
|
||||||
Self::Precomputed { ast, .. } => Ok(AstSource::Precomputed(ast)),
|
Self::Precomputed { ast, .. } => Ok(ast),
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub enum AstSource<'a> {
|
|
||||||
/// Extract the AST from the given source code.
|
|
||||||
Ast(Suite),
|
|
||||||
/// Use the precomputed AST.
|
|
||||||
Precomputed(&'a Suite),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Deref for AstSource<'_> {
|
|
||||||
type Target = Suite;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
match self {
|
|
||||||
Self::Ast(ast) => ast,
|
|
||||||
Self::Precomputed(ast) => ast,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -353,10 +353,12 @@ mod tests {
|
||||||
use insta::assert_debug_snapshot;
|
use insta::assert_debug_snapshot;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
|
|
||||||
|
#[cfg(not(windows))]
|
||||||
use ruff_linter::registry::Linter;
|
use ruff_linter::registry::Linter;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
#[cfg(not(windows))]
|
||||||
const VS_CODE_INIT_OPTIONS_FIXTURE: &str =
|
const VS_CODE_INIT_OPTIONS_FIXTURE: &str =
|
||||||
include_str!("../../resources/test/fixtures/settings/vs_code_initialization_options.json");
|
include_str!("../../resources/test/fixtures/settings/vs_code_initialization_options.json");
|
||||||
const GLOBAL_ONLY_INIT_OPTIONS_FIXTURE: &str =
|
const GLOBAL_ONLY_INIT_OPTIONS_FIXTURE: &str =
|
||||||
|
@ -368,7 +370,8 @@ mod tests {
|
||||||
serde_json::from_str(content).expect("test fixture JSON should deserialize")
|
serde_json::from_str(content).expect("test fixture JSON should deserialize")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(not(windows), test)]
|
#[cfg(not(windows))]
|
||||||
|
#[test]
|
||||||
fn test_vs_code_init_options_deserialize() {
|
fn test_vs_code_init_options_deserialize() {
|
||||||
let options: InitializationOptions = deserialize_fixture(VS_CODE_INIT_OPTIONS_FIXTURE);
|
let options: InitializationOptions = deserialize_fixture(VS_CODE_INIT_OPTIONS_FIXTURE);
|
||||||
|
|
||||||
|
@ -553,7 +556,8 @@ mod tests {
|
||||||
"###);
|
"###);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(not(windows), test)]
|
#[cfg(not(windows))]
|
||||||
|
#[test]
|
||||||
fn test_vs_code_workspace_settings_resolve() {
|
fn test_vs_code_workspace_settings_resolve() {
|
||||||
let options = deserialize_fixture(VS_CODE_INIT_OPTIONS_FIXTURE);
|
let options = deserialize_fixture(VS_CODE_INIT_OPTIONS_FIXTURE);
|
||||||
let AllSettings {
|
let AllSettings {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue