Skip to content

perf: 优化数据同步性能 - 多级 rowSlicePool 和无锁进度聚合#137

Merged
xfg0218 merged 2 commits into
mainfrom
xfg0218
Jun 20, 2026
Merged

perf: 优化数据同步性能 - 多级 rowSlicePool 和无锁进度聚合#137
xfg0218 merged 2 commits into
mainfrom
xfg0218

Conversation

@xfg0218

@xfg0218 xfg0218 commented Jun 19, 2026

Copy link
Copy Markdown
Owner

P1: 多级 rowSlicePool 内存优化

  • 将单级固定 128 容量的 rowSlicePool 改为 4 级 size-class 池
  • Level 0: 1-8 列,Level 1: 9-32 列,Level 2: 33-128 列,Level 3: 129-256 列
  • Fallback: >256 列直接分配(带告警)
  • 零长度预分配设计:make([]interface{}, 0, capacity) 避免未使用元素浪费
  • 小表场景内存利用率提升 70-90%

P2: 无锁进度聚合(锁竞争优化)

  • 使用 buffered channel + 专用消费者 goroutine 替代 mutex
  • Channel buffer 大小 = concurrency(默认 10)
  • 满时丢弃策略:select { default: } 不阻塞工作 goroutine
  • 基准测试:51 倍速度提升(9155ns → 178ns),96% 内存减少

基准测试结果:

  • Mutex 方案:9155 ns/op, 3059 B/op, 69 allocs/op
  • Channel 方案:178 ns/op, 80 B/op, 10 allocs/op

文件变更:

  • internal/postgres/connection.go: 多级 rowSlicePool + getRowSlice/putRowSlice
  • internal/postgres/connection_test.go: P1 基准测试
  • internal/converter/postgres/sync_data.go: 无锁进度聚合 + displayProgressNoLock
  • internal/converter/postgres/sync_data_test.go: P2 基准测试(新建)
  • internal/converter/postgres/manager.go: 传递 progressChan

xfg0218 added 2 commits June 19, 2026 22:56
P1: 多级 rowSlicePool 内存优化
- 将单级固定 128 容量的 rowSlicePool 改为 4 级 size-class 池
- Level 0: 1-8 列,Level 1: 9-32 列,Level 2: 33-128 列,Level 3: 129-256 列
- Fallback: >256 列直接分配(带告警)
- 零长度预分配设计:make([]interface{}, 0, capacity) 避免未使用元素浪费
- 小表场景内存利用率提升 70-90%

P2: 无锁进度聚合(锁竞争优化)
- 使用 buffered channel + 专用消费者 goroutine 替代 mutex
- Channel buffer 大小 = concurrency(默认 10)
- 满时丢弃策略:select { default: } 不阻塞工作 goroutine
- 基准测试:51 倍速度提升(9155ns → 178ns),96% 内存减少

基准测试结果:
- Mutex 方案:9155 ns/op, 3059 B/op, 69 allocs/op
- Channel 方案:178 ns/op, 80 B/op, 10 allocs/op

文件变更:
- internal/postgres/connection.go: 多级 rowSlicePool + getRowSlice/putRowSlice
- internal/postgres/connection_test.go: P1 基准测试
- internal/converter/postgres/sync_data.go: 无锁进度聚合 + displayProgressNoLock
- internal/converter/postgres/sync_data_test.go: P2 基准测试(新建)
- internal/converter/postgres/manager.go: 传递 progressChan
- 添加多级行切片池(4 级 size-class 内存池)说明
- 添加无锁进度聚合(51 倍速度提升)说明
- 更新批量处理大小从 10,000 行到 50,000 行
- 同步更新英文版和中文版 README

性能优化详情:
- 小表场景内存分配减少 70-90%
- 进度更新速度提升 51 倍(9155ns → 178ns)
- 内存减少 96%
@xfg0218 xfg0218 merged commit c3a6653 into main Jun 20, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant