
DC Motor Speed Controller
zlyadvocate/kicadfileparse-
C++ high performance independent kicad file parser

C++ high performance independent kicad file parser
Mạch ổn dịnh tốc độ động cơ is an open-source printed circuit board by zlyadvocate, from the zlyadvocate/kicadfileparse- repository on GitHub, published under the AGPL-3.0 licence. It is a 2-layer board of 57 × 28 mm with 23 parts on 14 nets, routed with 64 tracks and 0 vias.
It is built around the 2N2222A. Its parts include 5 resistors, 3 diodes, 3 transistors, 2 capacitors and 2 connectors.
KiCad 使用 S-Expression 格式存储 PCB、原理图和封装库文件。原始 C++ 解析器存在性能瓶颈,84.6 MB 的 PCB 文件解析需要约 5-6 秒,序列化需要约 9 秒。
| 轮次 | 优化内容 | 代码改动 | 效果 |
|---|---|---|---|
| 1 | Float 解析 fast_float (150 处) | 150 行 | Parse ↓ 大幅 |
| 2 | Footprint/Pad/Zone 首字符 switch | ~80 行 | Parse ↓ 大幅 |
| 3 | Track/Via/Arc 首字符 switch | ~50 行 | Parse ↓ 小幅 |
| 4 | Property 首字符 switch | ~30 行 | Parse ↓ 小幅 |
| 5 | string_view 悬挂引用 bug 修复 | 28 处 | 修复 pintype→intype 错误 |
| 6 | FastSerializeVisitor 指针直接写入 | ~50 行 | Serialize ↓ 大幅 |
| 7 | Zone::locked 字段缺失 | 5 行 | 修复 CM5_MINIMA_3 解析错误 |
| 8 | reserve/resize bug 修复 | 10 行 | 修复 microwave 输出损坏 |
| 9 | Token 引用返回 (const Token&) | ~20 行 | Parse ↓ ~7% |
| 10 | Pad/Property string 拷贝优化 | ~30 行 | 小幅降低内存分配 |
| 11 | 批量去引号工具函数 | ~20 行 | 代码简化 |
| 12 | Integer 解析 fast_float (152 处) | 152 行 | Parse ↓ ~1-3% |
| 13 | Token vector 预分配 | 1 行 | Parse ↓ ~1-2% |
| 14 | 消除逐字符拼接 + 简化 skip_whitespace | ~40 行 | Parse ↓ ~28% |
总计:约 650 行代码改动,14 轮迭代。
| 阶段 | 优化前 | 优化后 | 提升幅度 |
|---|---|---|---|
| Parse | ~5.5 s | 1.44 s | ↓ 74% |
| Serialize | ~9.0 s | 1.84 s | ↓ 80% |
| Total | ~14.5 s | 3.32 s | ↓ 77% |
与竞品对比:
| 方案 | Parse 84MB | 说明 |
|---|---|---|
| 我们的优化后 | 1.44 s | C++,零 AST 热路径 |
| KiCad 原生 | ~1-2 s | C++,高度优化 |
| Python kicadfiles | ~10-30 s | Python,解释执行 |
| 优化前 | ~5-6 s | C++,完整 AST |
热路径(Board/Footprint/Pad/Zone/Track/Via/Arc)通过 parse_skip_name 直接从 Token 流解析到结构体,零 AST 节点创建:
优化前: File → Tokenize → SExpr AST (100万节点) → from_sexpr → 结构体 → 丢弃 AST
优化后: File → Tokenize → parse_skip_name → 结构体将 parse_skip_name 中的 20-40 层 if-else 链优化为首字符 switch:
switch (name[0]) {
case 'l': if (name == "layer") ... else if (name == "locked") ... break;
case 'n': if (name == "net") ... else if (name == "name") ... break;
...
}input_.substr() 一次性构造std::isspace()tokens_.reserve(input.size() / 12)output += string 的多次 reallocstd::to_string:使用 snprintf 直接格式化到 buffer| 指标 | 结果 |
|---|---|
| 回归测试 | 15699 OK, 0 FAIL, 2 SKIP |
| 修复的 bug | 3 个严重 bug(string_view 悬挂引用、reserve/resize 损坏、Zone::locked 缺失) |
| 新引入依赖 | fast_float(header-only,Apache 2.0) |
| 代码风格 | 统一 fast_float 解析,统一首字符 switch 模式 |
| 组件 | 文件 | 说明 |
|---|---|---|
| Parser | sexpr.h/cpp | 词法分析 + AST 解析 |
| 核心类型 | base_types.h/cpp | AtXY, Layer, Stroke, Font, Effects 等 50+ 类型 |
| PCB 布局 | board_layout.h/cpp | Board, Track, Via, ArcTrack |
| 封装库 | footprint_library.h/cpp | Footprint, Pad, Model |
| 铺铜系统 | zone_system.h/cpp | Zone, ZoneFill, Keepout, Hatch |
| 图形元素 | primitive_graphics.h/cpp | Arc, Circle, Line, Polygon |
| 高级图形 | advanced_graphics.h/cpp | GrArc, GrLine, GrTextBox, Dimension |
| 焊盘/钻孔 | pad_and_drill.h/cpp | Drill, Pad, Teardrops |
| 方向 | 预估收益 | 难度 | 说明 |
|---|---|---|---|
| 剩余 120+ 个 from_sexpr 迁移到 parse_skip_name | 20-30% | 高 | 主要服务 SCH/SYM 文件 |
| Token 使用 string_view | 15-25% | 中 | 需要修改 Token 结构体 |
| SIMD skip_whitespace (StringZilla) | 2-4% | 低 | 引入外部依赖 |
投入:650 行代码改动,14 轮迭代
产出:
核心经验:
| Qty | Value | Footprint | References |
|---|---|---|---|
| 3 | D | Diode_THT:D_A-405_P7.62mm_Horizontal | D1, D2, D3 |
| 2 | 2N2222A | digikey-footprints:TO-18-3 | Q1, Q2 |
| 1 | 0.1uF/600V | Capacitor_THT:C_Axial_L5.1mm_D3.1mm_P10.00mm_Horizontal | C1 |
| 1 | 100K | Potentiometer_THT:Potentiometer_Bourns_3266Y_Vertical | RV1 |
| 1 | 100uF | Capacitor_THT:C_Axial_L5.1mm_D3.1mm_P10.00mm_Horizontal | C2 |
| 1 | 12K | Resistor_THT:R_Axial_DIN0207_L6.3mm_D2.5mm_P7.62mm_Horizontal | R3 |
| 1 | 220V | PCM_SparkFun-Connector:ScrewTerminal_1x02_P3.5mm_Horizontal_Black | J1 |
| 1 | 3.3K | Resistor_THT:R_Axial_DIN0207_L6.3mm_D2.5mm_P7.62mm_Horizontal | R4 |
| 1 | 39K | Resistor_THT:R_Axial_DIN0207_L6.3mm_D2.5mm_P7.62mm_Horizontal | R1 |
| 1 | 68K | Resistor_THT:R_Axial_DIN0207_L6.3mm_D2.5mm_P7.62mm_Horizontal | R2 |
| 1 | DONG_CO | PCM_SparkFun-Connector:ScrewTerminal_1x02_P3.5mm_Horizontal_Black | J2 |
| 1 | L_Ferrite | Ferrite_THT:LairdTech_28C0236-0JW-10 | L1 |
| 1 | R | Resistor_THT:R_Axial_DIN0207_L6.3mm_D2.5mm_P7.62mm_Horizontal | R5 |
| 1 | SW_SPDT | PCM_SparkFun-Switch:Slide_SPDT_PTH_11.6x4.0mm | SW1 |
| 1 | TIC206 | Package_TO_SOT_THT:TO-220-3_Vertical | Q1 |
The 18 most common of 23 parts.

zlyadvocate/kicadfileparse-
C++ high performance independent kicad file parser

zlyadvocate/kicadfileparse-
C++ high performance independent kicad file parser

zlyadvocate/kicadfileparse-
C++ high performance independent kicad file parser

zlyadvocate/kicadfileparse-
C++ high performance independent kicad file parser

zlyadvocate/kicadfileparse-
C++ high performance independent kicad file parser


lynchaj/rhyophyre
Single Board Computer featuring Zilog Z180 processor and NEC µPD7220 Graphics Display Controller

alvaroalea/QL_QsoundQprint_clone
A clone board of the Sound card for the Sinclair QL Computer.

profdc9/NixieClock
A Nixie clock that uses IN-14 tubes and very generic components that can be easily sourced.

jensma-de/MM58167-ISA-RTC
RTC module for the ISA slot. With schematics, project files in kicad and open source software to drive the clock.


gidim/e61-gauge
An analog temperature gauge for E61 espresso machines. A custom PCB drives a VID6606 stepper motor gauge needle, reading temperature from an NTC thermistor.

cnobile2012/Li-IonBatteryProtection
Circuit to protect Li-Ion and Li-Po batteries at the cell level.
It measures 57 × 28 mm and has 2 copper layers, 1.6 mm thick.
23 parts, built around the 2N2222A. The full list of its parts is on this page, with links to each part's symbol and footprint where PCBKit's library has them.
From the zlyadvocate/kicadfileparse- repository on GitHub, at the commit this page was made from: the board is testfile/pcbsec/Mạch ổn dịnh tốc độ động cơ.kicad_pcb. PCBKit opens the board in the browser, with nothing to install.
Its repository publishes it under the AGPL-3.0 licence: you may reuse it on that licence's terms (keep the author's notice, and for a copyleft licence share your changes alike).