Skip to the content
2 layers57 × 28 mmMạch ổn dịnh tốc độ động cơ, the printed circuit board in 3D

Mạch ổn dịnh tốc độ động cơ

zlyadvocate/kicadfileparse-

C++ high performance independent kicad file parser

Size
57 × 28 mm
Copper layers
2
Thickness
1.6 mm
Parts
23
Nets
14
Pads
45
Tracks, vias
64, 0
Pours
1
Routed
Yes
KiCad
KiCad 9.0 (file 20241229)
Created
21 May 2026
Updated
21 May 2026

Overview

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.

Main components

1

README

readme.md

KiCad S-Expression 解析器项目总结

一、项目背景

KiCad 使用 S-Expression 格式存储 PCB、原理图和封装库文件。原始 C++ 解析器存在性能瓶颈,84.6 MB 的 PCB 文件解析需要约 5-6 秒,序列化需要约 9 秒。

二、优化历程(14 轮迭代)

轮次优化内容代码改动效果
1Float 解析 fast_float (150 处)150 行Parse ↓ 大幅
2Footprint/Pad/Zone 首字符 switch~80 行Parse ↓ 大幅
3Track/Via/Arc 首字符 switch~50 行Parse ↓ 小幅
4Property 首字符 switch~30 行Parse ↓ 小幅
5string_view 悬挂引用 bug 修复28 处修复 pintype→intype 错误
6FastSerializeVisitor 指针直接写入~50 行Serialize ↓ 大幅
7Zone::locked 字段缺失5 行修复 CM5_MINIMA_3 解析错误
8reserve/resize bug 修复10 行修复 microwave 输出损坏
9Token 引用返回 (const Token&)~20 行Parse ↓ ~7%
10Pad/Property string 拷贝优化~30 行小幅降低内存分配
11批量去引号工具函数~20 行代码简化
12Integer 解析 fast_float (152 处)152 行Parse ↓ ~1-3%
13Token vector 预分配1 行Parse ↓ ~1-2%
14消除逐字符拼接 + 简化 skip_whitespace~40 行Parse ↓ ~28%

总计:约 650 行代码改动,14 轮迭代。


三、性能提升(jetson-agx-thor 84.6 MB,10 次平均)

阶段优化前优化后提升幅度
Parse~5.5 s1.44 s↓ 74%
Serialize~9.0 s1.84 s↓ 80%
Total~14.5 s3.32 s↓ 77%

与竞品对比:

方案Parse 84MB说明
我们的优化后1.44 sC++,零 AST 热路径
KiCad 原生~1-2 sC++,高度优化
Python kicadfiles~10-30 sPython,解释执行
优化前~5-6 sC++,完整 AST

四、关键优化技术

4.1 消除 AST 构造(最大收益)

热路径(Board/Footprint/Pad/Zone/Track/Via/Arc)通过 parse_skip_name 直接从 Token 流解析到结构体,零 AST 节点创建:

text
优化前: File → Tokenize → SExpr AST (100万节点) → from_sexpr → 结构体 → 丢弃 AST
优化后: File → Tokenize → parse_skip_name → 结构体

4.2 首字符 switch 分流

将 parse_skip_name 中的 20-40 层 if-else 链优化为首字符 switch:

cpp
switch (name[0]) {
    case 'l': if (name == "layer") ... else if (name == "locked") ... break;
    case 'n': if (name == "net") ... else if (name == "name") ... break;
    ...
}

4.3 Tokenize 阶段优化

  • 消除逐字符拼接:Symbol/Integer/Float 使用 input_.substr() 一次性构造
  • 简化 skip_whitespace:直接字符比较替代 std::isspace()
  • Token vector 预分配:tokens_.reserve(input.size() / 12)

4.4 序列化优化

  • FastSerializeVisitor 指针直接写入:避免 output += string 的多次 realloc
  • 消除 std::to_string:使用 snprintf 直接格式化到 buffer

五、质量保障

指标结果
回归测试15699 OK, 0 FAIL, 2 SKIP
修复的 bug3 个严重 bug(string_view 悬挂引用、reserve/resize 损坏、Zone::locked 缺失)
新引入依赖fast_float(header-only,Apache 2.0)
代码风格统一 fast_float 解析,统一首字符 switch 模式

六、代码结构概览

组件文件说明
Parsersexpr.h/cpp词法分析 + AST 解析
核心类型base_types.h/cppAtXY, Layer, Stroke, Font, Effects 等 50+ 类型
PCB 布局board_layout.h/cppBoard, Track, Via, ArcTrack
封装库footprint_library.h/cppFootprint, Pad, Model
铺铜系统zone_system.h/cppZone, ZoneFill, Keepout, Hatch
图形元素primitive_graphics.h/cppArc, Circle, Line, Polygon
高级图形advanced_graphics.h/cppGrArc, GrLine, GrTextBox, Dimension
焊盘/钻孔pad_and_drill.h/cppDrill, Pad, Teardrops

七、未完成的优化方向

方向预估收益难度说明
剩余 120+ 个 from_sexpr 迁移到 parse_skip_name20-30%高主要服务 SCH/SYM 文件
Token 使用 string_view15-25%中需要修改 Token 结构体
SIMD skip_whitespace (StringZilla)2-4%低引入外部依赖

八、总结

投入:650 行代码改动,14 轮迭代

产出:

  • Parse 性能提升 4 倍(5.5s → 1.44s)
  • Serialize 性能提升 5 倍(9s → 1.84s)
  • 总耗时降低 77%(14.5s → 3.32s)
  • 修复 3 个严重 bug
  • 接近 KiCad 原生加载速度

核心经验:

  1. 消除不必要的中间数据结构(AST)是最大收益来源
  2. 首字符 switch 分流是将 O(n) 字符串比较优化为 O(1) 的有效手段
  3. 消除逐字符操作(str += char)改为批量操作(substr)可带来 20%+ 提升
  4. 预分配内存(vector reserve)是低成本高收益的优化

Parts

23
QtyValueFootprintReferences
3DDiode_THT:D_A-405_P7.62mm_HorizontalD1, D2, D3
22N2222Adigikey-footprints:TO-18-3Q1, Q2
10.1uF/600VCapacitor_THT:C_Axial_L5.1mm_D3.1mm_P10.00mm_HorizontalC1
1100KPotentiometer_THT:Potentiometer_Bourns_3266Y_VerticalRV1
1100uFCapacitor_THT:C_Axial_L5.1mm_D3.1mm_P10.00mm_HorizontalC2
112KResistor_THT:R_Axial_DIN0207_L6.3mm_D2.5mm_P7.62mm_HorizontalR3
1220VPCM_SparkFun-Connector:ScrewTerminal_1x02_P3.5mm_Horizontal_BlackJ1
13.3KResistor_THT:R_Axial_DIN0207_L6.3mm_D2.5mm_P7.62mm_HorizontalR4
139KResistor_THT:R_Axial_DIN0207_L6.3mm_D2.5mm_P7.62mm_HorizontalR1
168KResistor_THT:R_Axial_DIN0207_L6.3mm_D2.5mm_P7.62mm_HorizontalR2
1DONG_COPCM_SparkFun-Connector:ScrewTerminal_1x02_P3.5mm_Horizontal_BlackJ2
1L_FerriteFerrite_THT:LairdTech_28C0236-0JW-10L1
1RResistor_THT:R_Axial_DIN0207_L6.3mm_D2.5mm_P7.62mm_HorizontalR5
1SW_SPDTPCM_SparkFun-Switch:Slide_SPDT_PTH_11.6x4.0mmSW1
1TIC206Package_TO_SOT_THT:TO-220-3_VerticalQ1

The 18 most common of 23 parts.

More from zlyadvocate/kicadfileparse-

5
  • 2 layers62.5 × 27.7 mmDC Motor Speed Controller, the printed circuit board in 3D

    DC Motor Speed Controller

    zlyadvocate/kicadfileparse-

    C++ high performance independent kicad file parser

    21 partsAGPL-3.0
  • 2 layers45.2 × 36.2 mmTransformerless Power Supply, the printed circuit board in 3D

    Transformerless Power Supply

    zlyadvocate/kicadfileparse-

    C++ high performance independent kicad file parser

    20 partsAGPL-3.0
  • 2 layers38.5 × 25.5 mmMạch đuổi muỗi, the printed circuit board in 3D

    Mạch đuổi muỗi

    zlyadvocate/kicadfileparse-

    C++ high performance independent kicad file parser

    16 partsAGPL-3.0
  • 2 layers34.8 × 30 mmADC Converter, the printed circuit board in 3D

    ADC Converter

    zlyadvocate/kicadfileparse-

    C++ high performance independent kicad file parser

    12 partsAGPL-3.0
  • 2 layers32.1 × 31.9 mmWater Alarm, the printed circuit board in 3D

    Water Alarm

    zlyadvocate/kicadfileparse-

    C++ high performance independent kicad file parser

    11 partsAGPL-3.0

Similar boards

  • 2 layers101.5 × 83 mmSd2snes, the printed circuit board in 3D

    Sd2snes

    mrehkopf/sd2snes

    SD card based multi-purpose cartridge for the SNES

    154 parts722GPL-2.0
  • 2 layers279.4 × 203.2 mmZ180GDC, the printed circuit board in 3D

    Z180GDC

    lynchaj/rhyophyre

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

    249 parts12
  • 2 layers99.1 × 99.1 mmQL Qsound10x10, the printed circuit board in 3D

    QL Qsound10x10

    alvaroalea/QL_QsoundQprint_clone

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

    23 parts11
  • 2 layers156.5 × 67.5 mmNixieScanDisplay, the printed circuit board in 3D

    NixieScanDisplay

    profdc9/NixieClock

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

    106 parts10
  • 2 layers89.3 × 50 mmMM58167-ISA RTC, the printed circuit board in 3D

    MM58167-ISA RTC

    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.

    29 parts9
  • 2 layers97.6 × 76.8 mm12f675-mppt, the printed circuit board in 3D

    12f675-mppt

    rasberryrabbit/PIC12F675MPPT

    Solar MPPT circuit.

    45 parts9GPL-3.0
  • 2 layers14.6 × 13.6 mmChip and transistor, the printed circuit board in 3D

    Chip and transistor

    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.

    15 parts8
  • 2 layers147.2 × 58.3 mmLi IonBatteryProtection, the printed circuit board in 3D

    Li IonBatteryProtection

    cnobile2012/Li-IonBatteryProtection

    Circuit to protect Li-Ion and Li-Po batteries at the cell level.

    81 parts6MIT

Questions

How big is the Mạch ổn dịnh tốc độ động cơ PCB?

It measures 57 × 28 mm and has 2 copper layers, 1.6 mm thick.

What parts does the Mạch ổn dịnh tốc độ động cơ use?

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.

Where can I download the Mạch ổn dịnh tốc độ động cơ KiCad files?

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.

Can I use the Mạch ổn dịnh tốc độ động cơ design in my own project?

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).