Skip to content

Commit

Permalink
将Var使用Rc, 可能可以缓解大量String克隆的性能问题
Browse files Browse the repository at this point in the history
  • Loading branch information
A4-Tacks committed Apr 28, 2024
1 parent 558616a commit a7331e6
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 69 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mindustry_logic_bang_lang"
version = "0.15.8"
version = "0.15.9"
edition = "2021"

authors = ["A4-Tacks <[email protected]>"]
Expand Down
11 changes: 6 additions & 5 deletions tools/parser/src/parser.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,16 @@ pub TopLevel: Expand = CtrlStart <mut lines:Expand> <ctrl:CtrlStop> => {
lines
};

pub String: Var = r#""(?:\\\r?\n\s*(?:\\ )?|\r?\n|\\[n\\\[]|[^"\r\n\\])*""# => string_escape(<>);
pub String: Var = r#""(?:\\\r?\n\s*(?:\\ )?|\r?\n|\\[n\\\[]|[^"\r\n\\])*""#
=> string_escape(<>).into();
pub Ident: Var = r"[_\p{XID_Start}]\p{XID_Continue}*" => <>.into();
pub OIdent: Var = r"@[_\p{XID_Start}][\p{XID_Continue}\-]*" => <>.into(); // `@abc-def`这种
pub Number: Var = r"(?:0(?:x-?[\da-fA-F][_\da-fA-F]*|b-?[01][_01]*)|-?\d[_\d]*(?:\.\d[\d_]*|e[+\-]?\d[\d_]*)?)"
=> <>.chars().filter(|&c| c != '_').collect();
// 原始字面量, 如`'@abc-def'`, 其中双引号会被替换为单引号.
// 在一对单引号内可以写任意非空字符, 可避开关键字等
pub OtherVar: Var = r"'[^'\s]+'" => {
<>[1..<>.len()-1].replace('"', "\'")
<>[1..<>.len()-1].replace('"', "\'").into()
};

// 逻辑里面一个单元
Expand Down Expand Up @@ -304,7 +305,7 @@ pub Op: Op = {
),
}

Label: String = ":" <Var>;
Label: Var = ":" <Var>;

#[inline]
Expand: Expand = LogicLine+? => Expand(<>.unwrap_or_default());
Expand Down Expand Up @@ -734,7 +735,7 @@ pub BuiltinCommand: LogicLine = {

Take::new(
args.unwrap_or_default(),
var.unwrap_or_else(|| String::from("__")),
var.unwrap_or_else(|| "__".into()),
do_leak_res,
value
)
Expand Down Expand Up @@ -979,7 +980,7 @@ ControlWithoutOptionalEnd: LogicLine = {
// 用于添加到头部的捕获块
let mut catch_lines = Vec::new();
let value_handle: Var = if catchs_is_empty {
Var::with_capacity(0)
Var::new()
} else { meta.get_tmp_var() };

// 这里开始遍历捕获
Expand Down
2 changes: 1 addition & 1 deletion tools/parser/tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parser-tests"
version = "0.1.18"
version = "0.1.19"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
2 changes: 1 addition & 1 deletion tools/parser/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4150,7 +4150,7 @@ fn string_escape_test() {
];
let quoted = |s| format!("\"{s}\"");
for (src, dst) in true_case {
assert_eq!(parse!(parser, &quoted(src)), Ok(quoted(dst)));
assert_eq!(parse!(parser, &quoted(src)), Ok(quoted(dst).into()));
}
for src in false_case {
let src = quoted(src);
Expand Down
2 changes: 1 addition & 1 deletion tools/syntax/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "syntax"
version = "0.2.16"
version = "0.2.17"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
34 changes: 17 additions & 17 deletions tools/syntax/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ pub fn build_builtins() -> Vec<BuiltinFunc> {
Ok(if Value::is_string(var) {
var.into()
} else {
format!("\"{var}\"")
format!("\"{var}\"").into()
})
})
}

fn status:Status(meta) [] {
Ok(meta.last_builtin_exit_code().to_string())
Ok(meta.last_builtin_exit_code().to_string().into())
}

fn concat:Concat(meta) [va:a vb:b] {
Expand All @@ -176,26 +176,26 @@ pub fn build_builtins() -> Vec<BuiltinFunc> {
if !Value::is_string(b) {
return Err((2, format!("{b} is not a string")));
}
Ok([&a[..a.len()-1], &b[1..]].concat())
Ok([&a[..a.len()-1], &b[1..]].concat().into())
})
})
}

fn info:Info(meta) [var:data] {
let value = data.value();
check_type!("var" Value::Var(var) = value => {
let msg = String::from(var);
meta.log_info(msg.clone());
Ok(msg)
let var = var.clone();
meta.log_info(var.clone());
Ok(var)
})
}

fn err:Err(meta) [var:data] {
let value = data.value();
check_type!("var" Value::Var(var) = value => {
let msg = String::from(var);
meta.log_err(msg.clone());
Ok(msg)
let var = var.clone();
meta.log_err(var.clone());
Ok(var)
})
}

Expand All @@ -214,20 +214,20 @@ pub fn build_builtins() -> Vec<BuiltinFunc> {
/// 建议直接使用quick_dexp_take
fn r#const:Const(meta) [n:name v:value] {
check_type!("var" Value::Var(name) = name.value() => {
let name: String = name.into();
Const(name.clone().into(), value.value().clone(), vec![]).compile(meta);
meta.add_const_value_leak(name);
let name = &name.clone();
Const(name.into(), value.value().clone(), vec![]).compile(meta);
meta.add_const_value_leak(name.clone());
Ok("__".into())
})
}

fn binder:Binder(meta) [n:name v:value] {
check_type!("var" Value::Var(name) = name.value() => {
check_type!("valuebind" Value::ValueBind(ValueBind(binder, _)) = value.value() => {
let name: String = name.into();
Const(name.clone().into(), binder.as_ref().clone(), vec![])
let name = &name.clone();
Const(name.into(), binder.as_ref().clone(), vec![])
.compile(meta);
meta.add_const_value_leak(name);
meta.add_const_value_leak(name.clone());
Ok("__".into())
})
})
Expand Down Expand Up @@ -272,7 +272,7 @@ pub fn build_builtins() -> Vec<BuiltinFunc> {
}

fn args_len:ArgsLen(meta) [] {
Ok(meta.get_env_second_args().len().to_string())
Ok(meta.get_env_second_args().len().to_string().into())
}

fn slice_args:SliceArgs(meta) [s:start e:end] {
Expand Down Expand Up @@ -309,7 +309,7 @@ pub fn build_builtins() -> Vec<BuiltinFunc> {
}

fn max_expand_depth:MaxExpandDepth(meta) [] {
Ok(meta.const_expand_max_depth().to_string())
Ok(meta.const_expand_max_depth().to_string().into())
}

fn set_max_expand_depth:SetMaxExpandDepth(meta) [d:depth] {
Expand Down
Loading

0 comments on commit a7331e6

Please sign in to comment.