1
Fork 0
mirror of https://github.com/RGBCube/alejandra synced 2025-07-30 12:07:46 +00:00

perf: lazy position

This commit is contained in:
Kevin Amado 2022-02-26 16:42:45 -05:00
parent d9f5fe5022
commit ea3418cbe7
2 changed files with 38 additions and 16 deletions

View file

@ -2,23 +2,29 @@
rnix = "*"
rowan = "0.12.6" # follows rnix
[target.aarch64-unknown-linux-musl.dependencies]
mimalloc = { version = "*", default-features = false }
[target.aarch64-unknown-linux-musl.dependencies.mimalloc]
default-features = false
version = "*"
[target.armv6l-unknown-linux-musleabihf.dependencies]
mimalloc = { version = "*", default-features = false }
[target.armv6l-unknown-linux-musleabihf.dependencies.mimalloc]
default-features = false
version = "*"
[target.armv7l-unknown-linux-musleabihf.dependencies]
mimalloc = { version = "*", default-features = false }
[target.armv7l-unknown-linux-musleabihf.dependencies.mimalloc]
default-features = false
version = "*"
[target.i686-unknown-linux-musl.dependencies]
mimalloc = { version = "*", default-features = false }
[target.i686-unknown-linux-musl.dependencies.mimalloc]
default-features = false
version = "*"
[target.x86_64-unknown-linux-gnu.dependencies]
mimalloc = { version = "*", default-features = false }
[target.x86_64-unknown-linux-gnu.dependencies.mimalloc]
default-features = false
version = "*"
[target.x86_64-unknown-linux-musl.dependencies]
mimalloc = { version = "*", default-features = false }
[target.x86_64-unknown-linux-musl.dependencies.mimalloc]
default-features = false
version = "*"
[package]
authors = ["Kevin Amado <kamadorueda@gmail.com>"]

View file

@ -15,13 +15,24 @@ impl Children {
) -> Children {
let mut children: Vec<rnix::SyntaxElement> = Vec::new();
let mut pos = build_ctx.pos_old.clone();
// Updating the position is costly,
// so let's just do it when really needed
let mut pos = {
let has_comments = node.children_with_tokens().any(|child| {
matches!(child.kind(), rnix::SyntaxKind::TOKEN_COMMENT)
});
if has_comments { Some(build_ctx.pos_old.clone()) } else { None }
};
for child in node.children_with_tokens() {
match child {
rnix::SyntaxElement::Node(node) => {
children.push(node.clone().into());
pos.update(&node.text().to_string());
if pos.is_some() {
pos.as_mut().unwrap().update(&node.text().to_string());
}
}
rnix::SyntaxElement::Token(token) => {
match token.kind() {
@ -29,7 +40,10 @@ impl Children {
children.push(
crate::builder::make_isolated_token(
rnix::SyntaxKind::TOKEN_COMMENT,
&dedent_comment(&pos, token.text()),
&dedent_comment(
&pos.as_ref().unwrap(),
token.text(),
),
)
.into(),
);
@ -44,7 +58,9 @@ impl Children {
}
}
pos.update(token.text());
if pos.is_some() {
pos.as_mut().unwrap().update(token.text());
}
}
}
}