[Feature] Improve performance
- 主要语言
- Rust
- 星标
- 5.2k
- 派生
- 145
- 平均合并
- 5 天 3 小时
- 30 天内合并 PR
- 7
描述
**Is your feature request related to a problem? Please describe.**
I'm experimenting with amber and I'm very surprised by the performance compared to native bash
Compare these snippets:
```bash
#!/usr/bin/env bash
# Readable rewrite of the obfuscated Amber-generated script
# Behavior: for each line in the input file (e.g. "L23" or "R7"),
# start with pos = 50, then for each line:
# - if line starts with 'L', do pos -= number
# - else, do pos += number
# - normalize pos to 0..99
# - count how many times pos becomes 0
# Input file path kept the same as the original script:
# /home/finkel/work_self/aoc-2025/src/day1.txt
set -euo pipefail
INPUT="/home/finkel/work_self/aoc-2025/src/day1.txt"
# Initialize
pos=50
count=0
# Read lines; handle last line even if there is no trailing newline
while IFS= read -r line || [ -n "${line-}" ]; do
# Skip empty lines
[[ -z "$line" ]] && continue
dir="${line:0:1}"
rest="${line:1}"
# Remove all whitespace from the numeric part
rest="${rest//[[:space:]]/}"
# Validate numeric part
if [[ ! "$rest" =~ ^-?[0-9]+$ ]]; then
continue
fi
num="$rest"
if [[ "$dir" == "L" ]]; then
pos=$(( pos - num ))
else
pos=$(( pos + num ))
fi
# Normalize to 0..99
rem=$(( pos % 100 ))
if (( rem < 0 )); then rem=$(( rem + 100 )); fi
pos="$rem"
if (( pos == 0 )); then
count=$(( count + 1 ))
fi
done < "$INPUT"
echo "$count"
```
```amber
import { slice, char_at, split_lines, parse_int } from "std/text"
import { file_read } from "std/fs"
const input = "/home/finkel/work_self/aoc-2025/src/day1.txt"
let pos = 50
let count = 0
for line in split_lines(trust file_read(input)) {
const dir = char_at(line, 0)
const num = trust parse_int(slice(line, 1))
if(dir == "L"){
pos = pos - num
} else {
pos = pos + num
}
let rem = pos % 100
if(rem < 0) {
rem = rem + 100
}
pos = rem
if(pos == 0){
count = count + 1
}
}
echo "Count {count}"
```
The code is almost identical in everything but syntax. But somehow the native bash script runs orders of magnitude faster:
```
❯ time ./xx.sh
Count 1168
./xx.sh 5.79s user 9.51s system 106% cpu 14.352 total
❯ time ./y.sh
1168
./y.sh 0.11s user 0.00s system 98% cpu 0.112 total
```
First call is the compiled amber, second call is the bash above
**Describe the solution you'd like**
I would take as many optimizations as possible from the bash script above. How we iterate over lines, how we do char_at, and how we do slice. Maybe not create functions when they're not strictly necessary.
- [ ] Implement an App interface to be able to detect usage of certain command
- [ ] Implement coproc cross platform alternative for "detected commands" in loops
贡献指南
这个仓库没有索引到贡献指南
评估
这个 Issue 还没有评估数据。