Skip to content

Commit

Permalink
perf: reduce memory usage (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Feb 23, 2024
1 parent 575828c commit ad04638
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 13 deletions.
63 changes: 60 additions & 3 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ tracing = ["dprint-core/tracing"]

[dependencies]
anyhow = "1.0.65"
dprint-core = { version = "0.65.0", features = ["formatting"] }
dprint-core = { version = "0.66.1", features = ["formatting"] }
dprint-core-macros = "0.1.0"
itertools = "0.10"
serde = { version = "1.0.145", features = ["derive"] }
serde_json = { version = "1.0", optional = true }
Expand Down
19 changes: 10 additions & 9 deletions src/generation/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use dprint_core::formatting::conditions::*;
use dprint_core::formatting::ir_helpers::SingleLineOptions;
use dprint_core::formatting::*;
use dprint_core_macros::sc;
use std::cell::Cell;
use std::rc::Rc;
use taplo::rowan::NodeOrToken;
Expand Down Expand Up @@ -64,7 +65,7 @@ fn gen_node_with_inner<'a>(node: SyntaxElement, context: &mut Context<'a>, inner
SyntaxKind::COMMENT => Ok(gen_comment(token, context)),
SyntaxKind::MULTI_LINE_STRING | SyntaxKind::MULTI_LINE_STRING_LITERAL => {
let mut items = PrintItems::new();
items.push_str("");
items.push_force_current_line_indentation();
items.extend(ir_helpers::gen_from_raw_string(token.text().trim()));
Ok(items)
}
Expand Down Expand Up @@ -178,14 +179,14 @@ fn gen_inline_table<'a>(node: SyntaxNode, context: &mut Context<'a>) -> PrintIte
ensure_all_kind(values.clone(), SyntaxKind::ENTRY)?;

let mut items = PrintItems::new();
items.push_str("{");
items.push_sc(sc!("{"));
let mut had_item = false;
for (i, value) in values.enumerate() {
items.push_str(if i > 0 { ", " } else { " " });
items.push_sc(if i > 0 { sc!(", ") } else { sc!(" ") });
items.extend(gen_node(value.into(), context));
had_item = true;
}
items.push_str(if had_item { " }" } else { "}" });
items.push_sc(if had_item { sc!(" }") } else { sc!("}") });

// the comment seems to be stored as the last child of an inline table, so check for it here
if let Some(NodeOrToken::Token(token)) = node.children_with_tokens().last() {
Expand All @@ -209,7 +210,7 @@ fn gen_entry<'a>(node: SyntaxNode, context: &mut Context<'a>) -> PrintItemsResul
let mut items = PrintItems::new();

items.extend(gen_node(key.into(), context));
items.push_str(" = ");
items.push_sc(sc!(" = "));
items.extend(gen_node(value.into(), context));

Ok(items)
Expand Down Expand Up @@ -237,19 +238,19 @@ fn gen_table_header<'a>(node: SyntaxNode, context: &mut Context<'a>) -> PrintIte
// Spec: Naming rules for tables are the same as for keys
let key = get_child_with_kind(node.clone(), SyntaxKind::KEY)?;
let mut items = PrintItems::new();
items.push_str("[");
items.push_sc(sc!("["));
items.extend(gen_node(key.into(), context));
items.push_str("]");
items.push_sc(sc!("]"));
Ok(items)
}

fn gen_table_array_header<'a>(node: SyntaxNode, context: &mut Context<'a>) -> PrintItemsResult {
// Spec: Naming rules for tables are the same as for keys
let key = get_child_with_kind(node.clone(), SyntaxKind::KEY)?;
let mut items = PrintItems::new();
items.push_str("[[");
items.push_sc(sc!("[["));
items.extend(gen_node(key.into(), context));
items.push_str("]]");
items.push_sc(sc!("]]"));
Ok(items)
}

Expand Down

0 comments on commit ad04638

Please sign in to comment.