
Mạch ổn dịnh tốc độ động cơ
zlyadvocate/kicadfileparse-
C++ high performance independent kicad file parser

C++ high performance independent kicad file parser
DC Motor Speed Controller 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 62.5 × 27.7 mm with 21 parts on 11 nets, routed with 64 tracks and 0 vias.
It is built around the NE555P and TIP122. Its parts include 4 diodes, 3 capacitors, 3 resistors, 2 connectors and 1 IC. It belongs with the robots and motor control boards of this library.
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 | 1N4007 | Diode_THT:D_DO-41_SOD81_P10.16mm_Horizontal | D1, D2, D3 |
| 2 | 100nF | Capacitor_THT:C_Disc_D4.7mm_W2.5mm_P5.00mm | C1, C2 |
| 2 | 1K | Resistor_THT:R_Axial_DIN0204_L3.6mm_D1.6mm_P7.62mm_Horizontal | R1, R2 |
| 2 | Screw_Terminal_01x02 | TerminalBlock_Phoenix:TerminalBlock_Phoenix_MKDS-1,5-2_1x02_P5.00mm_Horizontal | J1, J2 |
| 1 | |||
| 1 | 1000uF | Capacitor_THT:CP_Radial_D10.0mm_P5.00mm | C3 |
| 1 | 100K | Potentiometer_THT:Potentiometer_Alps_RK163_Single_Horizontal | RV1 |
| 1 | 2.2K | Resistor_THT:R_Axial_DIN0204_L3.6mm_D1.6mm_P7.62mm_Horizontal | R3 |
| 1 | LED | LED_THT:LED_D5.0mm | D4 |
| 1 | LOGO | LOGO | G*** |
| 1 | NE555P | Package_DIP:DIP-8_W7.62mm | U1 |
| 1 | TIP122 | Package_TO_SOT_THT:TO-220-3_Vertical | Q1 |
The 17 most common of 21 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

Aniket95-code/Fan-Speed-Controller-NE555-PCB-design
A simple, low-cost DC fan speed controller built around the classic NE555 timer IC operating in astable mode. Fan speed is adjusted by varying the PWM duty cycle via a potentiometer, while a TIP122 Darlington transistor…

arijit-arijit/pcb_design
my journey in learning to design PCBs from scratch through tutorials and projects

adityajoshi2008/555-Timer-PWM-DC-Motor-Speed-Controller
**555 timer-based PWM DC motor speed controller designed and developed in KiCAD for practical motor speed control.

apekg-blip/PWM-based-dc-motor-speed-controller
Built an analog DC motor speed controller using an NE555 timer IC and LM3914 LED bar graph for duty-cycle visualization. Designed the circuit schematic in KiCad, gaining experience in PWM and analog motor control

Hirusha785/DC-Motor-Speed-Controller-PCB
⚡ NE555 PWM DC motor speed controller PCB designed in KiCad, featuring adjustable speed control, TIP122 driver stage, flyback protection, screw terminals, and 3D PCB renders.

Mahender0408/Embedded-Systems-Projects
Embedded Systems, IoT and PCB Design Projects

MuhammadAbuzarCUI/DC-Motor-Speed-Controller-Using-555-Timer-IC-PCB-Designed-in-KiCad
PCB design of a DC motor speed controller using the 555 timer IC to generate a PWM signal for speed control. Includes circuit schematic and PCB layout for motor speed regulation by adjusting the PWM duty cycle.

Noraiz963/DC-Motor-Controller
A 555-timer-based DC motor speed controller designed in KiCad — uses a variable-duty-cycle PWM signal driven through a power transistor to control motor speed with a single potentiometer.
It measures 62.5 × 27.7 mm and has 2 copper layers, 1.6 mm thick.
21 parts, built around the NE555P and TIP122. 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/DC Motor Speed Controller.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).