Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion PERFORMANCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ make qbench
# 或 cargo run --release --bin qbench -- --json --iterations 100
```

`qbench` 为每个内建负载输出一行 JSON,分别记录编译、验证和执行的纳秒总耗时,并在计时循环中校验预期最终值。其结果用于 CI 回归和绘图,不替代下文要求的三次 release 中位数报告。
要自动取得三次样本的中位数,可加 `--repeat 3`;输出中的 `repeat` 记录样本数,三个 `*_ns` 字段仍是每轮迭代总耗时的中位数:

```sh
cargo run --locked --release --bin qbench -- --json --iterations 100 --repeat 3
```

`qbench` 为每个内建负载输出一行 JSON,分别记录编译、验证和执行的纳秒总耗时,并在计时循环中校验预期最终值。默认 `--repeat 1` 适合快速 CI 回归;需要正式三次 release 中位数时使用 `--repeat 3`,其结果可直接作为下文报告数据。

测量环境:Apple arm64(Darwin 25.5.0,T6000)、`rustc 1.94.0 (4a4ef493e 2026-03-02)`、`cargo bench --bench core`。基准以 release profile 运行;报告日期为 2026-08-22。

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

QuickCoffee 是一台以 Rust 编写、受 CoffeeScript 启发的字节码脚本引擎。它保留紧凑、可读的表达式语法,却不兼容 JavaScript:没有原型链、`this`、`eval` 或嵌入 JavaScript。

当前实现遵循 [RFCs/0000-project-scope.md](RFCs/0000-project-scope.md) 至 [RFCs/0103-qbench-schema-version.md](RFCs/0103-qbench-schema-version.md)。
当前实现遵循 [RFCs/0000-project-scope.md](RFCs/0000-project-scope.md) 至 [RFCs/0105-qbench-repeat-median.md](RFCs/0105-qbench-repeat-median.md)。

```coffee
square = (x) -> x * x
Expand All @@ -26,6 +26,7 @@ cargo run -- --check example.qc
cargo run -- --dump-bytecode example.qc
cargo run -- --fingerprint example.qc
cargo run --release --bin qbench -- --json --iterations 100
cargo run --release --bin qbench -- --json --iterations 100 --repeat 3
cargo run --example embed
cargo run --bin qdocco -- example.qc -o example.html
cargo run --bin qdocco -- --markdown example.qc -o example.md
Expand Down
2 changes: 1 addition & 1 deletion RFCs/0000-project-scope.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ QuickCoffee 是一个 Rust 实现的、受 CoffeeScript 2016 启发的脚本引

本仓库中的测试即 0.1 的语义基线。对语法或运行时的新增特性必须先以 RFC 补充定义,并至少添加:成功测试、错误测试及字节码验证测试。

当前已实现的后续语义与工具 RFC 延伸至 RFC 0103;其中 RFC 0077 定义 JSON 输出、RFC 0079 定义 TAP 输出、RFC 0080 定义 CLI 字节码指纹、RFC 0081 定义可机器读取的基准输出、RFC 0082 规范化指纹编码、RFC 0083 定义 Markdown 文学编程产物、RFC 0084 定义嵌入上下文 fuel 控制、RFC 0085 定义可执行 Rust 嵌入示例、RFC 0086 定义 crate 发布元数据、RFC 0094 定义 qdocco 最终值门禁、RFC 0095 定义字符串步进迭代、RFC 0096 定义其性能基准、RFC 0097 定义 `do` 参数转发、RFC 0098 定义 RFC 索引门禁、RFC 0099 定义 `!` 否定别名、RFC 0100 定义有符号 `by` 步长、RFC 0101 定义 qdocco 原子输出、RFC 0102 定义 qtest 规范文件去重、RFC 0103 定义 qbench schema 版本,均不改变脚本语言值模型的原型无关约束。
当前已实现的后续语义与工具 RFC 延伸至 RFC 0105;其中 RFC 0077 定义 JSON 输出、RFC 0079 定义 TAP 输出、RFC 0080 定义 CLI 字节码指纹、RFC 0081 定义可机器读取的基准输出、RFC 0082 规范化指纹编码、RFC 0083 定义 Markdown 文学编程产物、RFC 0084 定义嵌入上下文 fuel 控制、RFC 0085 定义可执行 Rust 嵌入示例、RFC 0086 定义 crate 发布元数据、RFC 0094 定义 qdocco 最终值门禁、RFC 0095 定义字符串步进迭代、RFC 0096 定义其性能基准、RFC 0097 定义 `do` 参数转发、RFC 0098 定义 RFC 索引门禁、RFC 0099 定义 `!` 否定别名、RFC 0100 定义有符号 `by` 步长、RFC 0101 定义 qdocco 原子输出、RFC 0102 定义 qtest 规范文件去重、RFC 0103 定义 qbench schema 版本、RFC 0104 定义 qdocco 块注释代码保留、RFC 0105 定义 qbench 重复采样中位数,均不改变脚本语言值模型的原型无关约束。
8 changes: 8 additions & 0 deletions RFCs/0104-qdocco-block-comments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# RFC 0104:qdocco 保留块注释代码

- 状态:已采纳
- 依赖:RFC 0032、RFC 0083

qdocco 文学 prose 行必须以恰好两个 `#` 开始(可有前导空格);以三个或更多 `#` 开始的行属于 QuickCoffee 块注释或其内容,必须保留在生成文档的代码区。这样 `### ... ###` 不会被误渲染成说明文字,也不会从可复制代码中消失。

HTML 与 Markdown 输出采用同一分类规则。块注释仍由 QuickCoffee 编译器执行并从运行语义中移除,RFC 0104 只规定文学文档的代码/prose 分区。
12 changes: 12 additions & 0 deletions RFCs/0105-qbench-repeat-median.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# RFC 0105:qbench 重复采样与中位数

- 状态:已采纳
- 依赖:RFC 0081、RFC 0096、RFC 0103

`qbench` 增加 `--repeat N`(默认 `1`),对每个内建负载完整执行 N 次编译、验证和执行计时。每一轮都运行语义护栏;输出的 `compile_ns`、`verify_ns`、`execute_ns` 是各阶段样本的中位数,仍表示单轮内 `--iterations` 次迭代的纳秒总耗时。偶数样本取排序后的上侧中位数(索引 `N / 2`)。

JSON 与文本输出新增 `repeat` 字段/键;`--repeat 0` 或非整数返回退出码 2。默认 `--repeat 1` 保持既有单次采样行为,schema 仍为 `quickcoffee.qbench.v1`。

## 验收

集成测试必须验证默认和显式 repeat 值、JSON 的中位数记录字段、非法 repeat 参数及每轮语义护栏;性能报告可直接使用 `--repeat 3` 取得三次中位数。
4 changes: 2 additions & 2 deletions docs/manual.classical-zh.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@
<p>数组之环可书 by step;步惟求一遍,须非零有限整数,负者自末起,映射环弗用之。</p>
<p>数组之环亦可系从一始之下标:for value, index in items then value + index。</p>
<p>后置之推导亦循严收集:value * 2 for value in items,或括以 [value * 2 for value in items]。</p>
<p># 此段有无效 ` 文,机不求之</p>
<p>#</p>
</main><main><h1>Code</h1><pre><code>甲 = 6
倍 = (x) -&gt; x * 2
shorthand = 'yes'
Expand Down Expand Up @@ -126,5 +124,7 @@
nil? == false and false? == true and 0? == true
默认数 ?= 42
默认数 == 42
### 此段有无效 ` 文,机不求之
###
42 == 42
</code></pre></main><footer>Final value: <code>true</code></footer>
4 changes: 2 additions & 2 deletions docs/manual.devanagari-sa.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@
<p>array-for by step उपयुज्यते; non-zero finite integer step एकवारं मूल्यते, negative क्रमः अन्तिम-पदात् आरभते, map तु न।</p>
<p>array-for शून्यात् गणितं index अपि बध्नाति: for value, index in items then value + index।</p>
<p>postfix-comprehension समानं strict-collection वहति: value * 2 for value in items, अथवा [value * 2 for value in items]।</p>
<p># invalid ` source अत्र उपेक्षितः</p>
<p>#</p>
</main><main><h1>Code</h1><pre><code>base = 40
add = (x) -&gt; x + base
shorthand = 'yes'
Expand Down Expand Up @@ -129,5 +127,7 @@
nil? == false and false? == true and 0? == true
default_value ?= 42
default_value == 42
### invalid ` source अत्र उपेक्षितः
###
42 == 42
</code></pre></main><footer>Final value: <code>true</code></footer>
4 changes: 2 additions & 2 deletions docs/manual.en.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@
<p>Arithmetic also has floor division // and dividend-dependent modulo %%: -7 // 5 is -2, while -7 %% 5 is 3.</p>
<p>return expression exits only its current function; bare return yields nil, cleans loops, and runs enclosing finally blocks.</p>
<p>Parameters may use strict nested array/map patterns; defaults and rest stay name-only.</p>
<p># invalid ` source is safely ignored here</p>
<p>#</p>
</main><main><h1>Code</h1><pre><code>base = 20
add = (x) -&gt;
result = x + base
Expand Down Expand Up @@ -136,5 +134,7 @@
heredoc = &quot;&quot;&quot;answer #{add(22)}
next&quot;&quot;&quot;
heredoc == 'answer 42\nnext'
### invalid ` source is safely ignored here
###
42 == 42
</code></pre></main><footer>Final value: <code>true</code></footer>
4 changes: 2 additions & 2 deletions docs/manual.latin.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@
<p>Ordo seriei by step uti potest; gradus integer finitus positivus semel aestimatur, maps eum excludunt.</p>
<p>Iteratio seriei etiam indicem a zero numeratum ligare potest: for value, index in items then value + index.</p>
<p>Comprehensio postfix eandem collectionem strictam servat: value * 2 for value in items, vel [value * 2 for value in items].</p>
<p># fons invalidus ` hic ignoratur</p>
<p>#</p>
</main><main><h1>Code</h1><pre><code>numerus = 7
quadratum = (x) -&gt; x * x
shorthand = 'yes'
Expand Down Expand Up @@ -127,5 +125,7 @@
nil? == false and false? == true and 0? == true
valor_defectus ?= 42
valor_defectus == 42
### fons invalidus ` hic ignoratur
###
42 == 42
</code></pre></main><footer>Final value: <code>true</code></footer>
4 changes: 2 additions & 2 deletions docs/manual.zh-CN.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@
<p>数组循环可写 by step;步长只求一次,须为非零有限整数,负步从末项起,映射循环不用 by。</p>
<p>数组循环亦可绑定从零开始的下标:for value, index in items then value + index。</p>
<p>后置推导沿用严格收集:value * 2 for value in items,亦可写作 [value * 2 for value in items]。</p>
<p># 这段含无效 ` 源文,却不会参与执行</p>
<p>#</p>
</main><main><h1>Code</h1><pre><code>base = 21
double = (x) -&gt; x * 2
shorthand = 'yes'
Expand Down Expand Up @@ -133,5 +131,7 @@
多行 = &quot;&quot;&quot;答案 #{double(base)}
次行&quot;&quot;&quot;
多行 == '答案 42\n次行'
### 这段含无效 ` 源文,却不会参与执行
###
42 == 42
</code></pre></main><footer>Final value: <code>true</code></footer>
73 changes: 48 additions & 25 deletions src/bin/qbench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,21 @@ const WORKLOADS: &[Workload] = &[
];

fn usage() {
eprintln!("Usage: qbench [--iterations N] [--json]\n qbench --version");
eprintln!("Usage: qbench [--iterations N] [--repeat N] [--json]\n qbench --version");
}

fn json_escape(value: &str) -> String {
value.replace('\\', "\\\\").replace('"', "\\\"")
}

fn median(samples: &mut [u128]) -> u128 {
samples.sort_unstable();
samples[samples.len() / 2]
}

fn main() -> ExitCode {
let mut iterations = 100;
let mut repeat = 1;
let mut json = false;
let mut args = env::args().skip(1);
while let Some(arg) = args.next() {
Expand All @@ -74,6 +80,13 @@ fn main() -> ExitCode {
return ExitCode::from(2);
}
},
"--repeat" => match args.next().and_then(|value| value.parse().ok()) {
Some(value) if value > 0 => repeat = value,
_ => {
eprintln!("--repeat requires a positive integer");
return ExitCode::from(2);
}
},
_ => {
usage();
return ExitCode::from(2);
Expand All @@ -82,52 +95,62 @@ fn main() -> ExitCode {
}
let engine = Engine::new();
for workload in WORKLOADS {
let start = Instant::now();
for _ in 0..iterations {
engine
.compile(workload.source)
let mut compile_samples = Vec::with_capacity(repeat);
let mut verify_samples = Vec::with_capacity(repeat);
let mut execute_samples = Vec::with_capacity(repeat);
for _ in 0..repeat {
let start = Instant::now();
for _ in 0..iterations {
engine
.compile(workload.source)
.expect("qbench workload must compile");
}
compile_samples.push(start.elapsed().as_nanos());
let program = engine
.compile_program(workload.source)
.expect("qbench workload must compile");
}
let compile_ns = start.elapsed().as_nanos();
let program = engine
.compile_program(workload.source)
.expect("qbench workload must compile");

let start = Instant::now();
for _ in 0..iterations {
program.verify().expect("qbench workload must verify");
}
let verify_ns = start.elapsed().as_nanos();
let start = Instant::now();
for _ in 0..iterations {
program.verify().expect("qbench workload must verify");
}
verify_samples.push(start.elapsed().as_nanos());

let start = Instant::now();
for _ in 0..iterations {
let mut context = Context::new().with_fuel(100_000);
let value = context
.run_program(&program)
.expect("qbench workload must execute");
assert_eq!(value.to_string(), workload.expected, "{}", workload.name);
let start = Instant::now();
for _ in 0..iterations {
let mut context = Context::new().with_fuel(100_000);
let value = context
.run_program(&program)
.expect("qbench workload must execute");
assert_eq!(value.to_string(), workload.expected, "{}", workload.name);
}
execute_samples.push(start.elapsed().as_nanos());
}
let execute_ns = start.elapsed().as_nanos();
let compile_ns = median(&mut compile_samples);
let verify_ns = median(&mut verify_samples);
let execute_ns = median(&mut execute_samples);

if json {
println!(
"{{\"schema\":\"{}\",\"version\":\"{}\",\"name\":\"{}\",\"iterations\":{},\"expected\":\"{}\",\"compile_ns\":{},\"verify_ns\":{},\"execute_ns\":{}}}",
"{{\"schema\":\"{}\",\"version\":\"{}\",\"name\":\"{}\",\"iterations\":{},\"repeat\":{},\"expected\":\"{}\",\"compile_ns\":{},\"verify_ns\":{},\"execute_ns\":{}}}",
OUTPUT_SCHEMA,
env!("CARGO_PKG_VERSION"),
json_escape(workload.name),
iterations,
repeat,
json_escape(workload.expected),
compile_ns,
verify_ns,
execute_ns
);
} else {
println!(
"schema={} version={} {} iterations={} compile_ns={} verify_ns={} execute_ns={} expected={}",
"schema={} version={} {} iterations={} repeat={} compile_ns={} verify_ns={} execute_ns={} expected={}",
OUTPUT_SCHEMA,
env!("CARGO_PKG_VERSION"),
workload.name,
iterations,
repeat,
compile_ns,
verify_ns,
execute_ns,
Expand Down
47 changes: 30 additions & 17 deletions src/bin/qdocco.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,38 +50,51 @@ fn escape(input: &str) -> String {
.replace('>', "&gt;")
.replace('"', "&quot;")
}
fn render(source: &str, result: &str) -> String {
fn prose_text(line: &str) -> Option<&str> {
let trimmed = line.trim_start();
let text = trimmed.strip_prefix("##")?;
(!text.starts_with('#')).then_some(text)
Comment thread
tiye marked this conversation as resolved.
}
fn split_source(source: &str) -> (String, String) {
let mut prose = String::new();
let mut code = String::new();
let mut in_block_comment = false;
for line in source.lines() {
if let Some(text) = line.trim_start().strip_prefix("##") {
prose.push_str(&format!("<p>{}</p>\n", escape(text.trim())))
let trimmed = line.trim_start();
if in_block_comment {
code.push_str(line);
code.push('\n');
if trimmed.contains("###") {
in_block_comment = false;
}
} else if let Some(after_marker) = trimmed.strip_prefix("###") {
code.push_str(line);
code.push('\n');
in_block_comment = !after_marker.contains("###");
} else if let Some(text) = prose_text(line) {
prose.push_str(text.trim());
prose.push('\n');
} else {
code.push_str(line);
code.push('\n');
}
}
(prose, code)
}
fn render(source: &str, result: &str) -> String {
let (prose_text, code) = split_source(source);
let prose = prose_text
.lines()
.map(|line| format!("<p>{}</p>\n", escape(line)))
.collect::<String>();
format!(
"<!doctype html><meta charset=\"utf-8\"><title>QuickCoffee document</title><style>body{{font:16px system-ui;display:grid;grid-template-columns:1fr 1fr;gap:2rem;margin:2rem}}pre{{background:#f5f5f5;padding:1rem;white-space:pre-wrap}}footer{{grid-column:1/-1}}</style><main><h1>Notes</h1>{prose}</main><main><h1>Code</h1><pre><code>{}</code></pre></main><footer>Final value: <code>{}</code></footer>",
escape(&code),
escape(result)
)
}
fn render_markdown(source: &str, result: &str) -> String {
let mut prose = String::new();
let mut code = String::new();
for line in source.lines() {
if let Some(text) = line.trim_start().strip_prefix("##") {
let text = text.trim();
if !text.is_empty() {
prose.push_str(text);
prose.push('\n');
}
} else {
code.push_str(line);
code.push('\n');
}
}
let (prose, code) = split_source(source);
let fence = markdown_fence(source);
format!(
"# QuickCoffee document\n\n## Notes\n\n{prose}\n## Code\n\n{fence}quickcoffee\n{code}{fence}\n\n## Final value\n\n`{result}`\n"
Expand Down
Loading