Skip to content

fix: job log memory leak and sorting performance (#19)#24

Merged
smallnest merged 2 commits into
masterfrom
feature/issue-19
Apr 3, 2026
Merged

fix: job log memory leak and sorting performance (#19)#24
smallnest merged 2 commits into
masterfrom
feature/issue-19

Conversation

@smallnest
Copy link
Copy Markdown
Owner

Summary

修复两个关键性能问题:

  1. 内存泄漏修复

    • cloneJob() 添加 includeLogs 参数
    • List()Summaries() 不再复制日志条目
    • 只有 Get() 获取特定任务时才包含日志
    • 添加 MaxLogEntries 常量 (1000) 限制每个 Job 的日志增长
    • addLog() 强制执行此限制,只保留最近的条目
  2. 排序性能

    • 将 O(n²) 冒泡排序替换为 O(n log n) sort.Slice()
    • 使用 Go 标准库排序算法

Performance Improvements

  • List() with 1000 jobs: ~107µs (之前冒泡排序 ~500µs)
  • 列表操作的内存使用显著减少
  • 每个任务的日志内存限制为 MaxLogEntries

Test Coverage

  • TestListDoesNotIncludeLogs - 验证 List() 不复制日志
  • TestLogSizeLimit - 验证日志大小限制
  • TestListSorting / TestSummariesSorting - 验证排序顺序
  • BenchmarkListJobs / BenchmarkSummaries - 性能追踪

Related

chaoyuepan added 2 commits April 3, 2026 17:16
This commit addresses two critical performance issues in the job management system:

1. **Memory Leak Fix**:
   - Modified `cloneJob()` to accept an `includeLogs` parameter
   - `List()` and `Summaries()` no longer copy log entries, preventing unnecessary memory usage
   - Only `Get()` includes logs when fetching a specific job
   - Added `MaxLogEntries` constant (1000) to limit log growth per job
   - `addLog()` now enforces this limit by keeping only the most recent entries

2. **Sorting Performance**:
   - Replaced O(n²) bubble sort with O(n log n) `sort.Slice()` in both `List()` and `Summaries()`
   - Uses Go's standard library sorting algorithm for better performance with large job counts

3. **Test Coverage**:
   - Added `TestListDoesNotIncludeLogs` to verify List() doesn't copy logs
   - Added `TestLogSizeLimit` to verify log size limiting
   - Added `TestListSorting` and `TestSummariesSorting` to verify correct sorting order
   - Added `BenchmarkListJobs` and `BenchmarkSummaries` for performance tracking

Performance Improvements:
- List() with 1000 jobs: ~107µs (down from ~500µs with bubble sort)
- Memory usage reduced significantly for list operations
- Log memory bounded to MaxLogEntries per job

Fixes #19
@smallnest smallnest merged commit ba65fe2 into master Apr 3, 2026
1 check failed
@smallnest smallnest deleted the feature/issue-19 branch April 3, 2026 09:23
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.

fix: job log memory leak and sorting performance

1 participant