Skip to content

Commit daf14f1

Browse files
committed
refactor: adjust PromiseMgr logic and optimize performance
1 parent dc1c5fc commit daf14f1

File tree

11 files changed

+721
-469
lines changed

11 files changed

+721
-469
lines changed

README.md

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ A high-performance, type-safe Go Promise library inspired by JavaScript Promises
1717
- 🎯 **Zero Dependencies**: Pure Go implementation, no external dependencies
1818
- 🎛️ **Flexible Configuration**: Support global and custom Promise managers
1919
- 🔧 **Resource Isolation**: Different managers don't affect each other, support independent configuration
20+
- 🚦 **Concurrency Safe**: Fixed all data race issues, thread-safe operations
2021

2122
## 📦 Installation
2223

@@ -196,44 +197,52 @@ func Reduce[T any, R any](items []T, fn func(R, T) *Promise[R], initial R) *Prom
196197

197198
### Test Environment
198199
- **CPU**: Apple M2 Max
199-
- **Go Version**: 1.21.4
200+
- **Go Version**: 1.24.2
200201
- **Test Command**: `go test -bench=. -benchmem`
201202

202203
### Benchmark Results
203204

204205
```
205-
BenchmarkPromiseCreation-12 1978914 618.2 ns/op 448 B/op 8 allocs/op
206-
BenchmarkPromiseThen-12 3443774 359.6 ns/op 336 B/op 7 allocs/op
207-
BenchmarkPromiseAwait-12 89638920 12.91 ns/op 0 B/op 0 allocs/op
208-
BenchmarkMicrotaskQueue-12 8970466 134.7 ns/op 24 B/op 2 allocs/op
209-
BenchmarkPromiseChain-12 170878 10079 ns/op 4066 B/op 72 allocs/op
210-
BenchmarkSimplePromiseChain-12 381231 6209 ns/op 2472 B/op 42 allocs/op
211-
BenchmarkWithResolvers-12 6140624 195.3 ns/op 288 B/op 5 allocs/op
212-
BenchmarkWithResolversWithMgr-12 6223452 191.9 ns/op 288 B/op 5 allocs/op
213-
BenchmarkResolveMultipleTimes-12 4420789 272.0 ns/op 320 B/op 7 allocs/op
214-
BenchmarkRejectMultipleTimes-12 3111844 383.4 ns/op 560 B/op 10 allocs/op
206+
BenchmarkPromiseCreation-12 1,278,765 ops 1,013 ns/op 437 B/op 7 allocs/op
207+
BenchmarkPromiseThen-12 1,918,182 ops 633.1 ns/op 448 B/op 7 allocs/op
208+
BenchmarkPromiseAwait-12 39,064,831 ops 32.87 ns/op 0 B/op 0 allocs/op
209+
BenchmarkMicrotaskQueue-12 3,176,100 ops 370.3 ns/op 134 B/op 2 allocs/op
210+
BenchmarkPromiseChain-12 171,643 ops 13,399 ns/op 5,096 B/op 72 allocs/op
211+
BenchmarkSimplePromiseChain-12 279,733 ops 6,951 ns/op 2,975 B/op 42 allocs/op
212+
BenchmarkWithResolvers-12 5,637,927 ops 213.1 ns/op 288 B/op 5 allocs/op
213+
BenchmarkWithResolversWithMgr-12 5,736,646 ops 209.4 ns/op 288 B/op 5 allocs/op
214+
BenchmarkResolveMultipleTimes-12 4,016,504 ops 298.4 ns/op 320 B/op 7 allocs/op
215+
BenchmarkRejectMultipleTimes-12 2,891,620 ops 410.3 ns/op 560 B/op 10 allocs/op
216+
BenchmarkMemoryAllocation-12 517,053 ops 2,842 ns/op 1,417 B/op 21 allocs/op
217+
BenchmarkConcurrentPromiseCreation-12 1,000,000 ops 1,201 ns/op 416 B/op 6 allocs/op
218+
BenchmarkTaskPoolReuse-12 1,609,479 ops 758.4 ns/op 440 B/op 8 allocs/op
215219
```
216220

217221
### Performance Analysis
218222

219223
| Operation | Performance | Memory Allocation | Description |
220224
|-----------|-------------|-------------------|-------------|
221-
| **Promise Creation** | 618.2 ns/op | 448 B/op | Basic Promise instance creation |
222-
| **Then Operation** | 359.6 ns/op | 336 B/op | Adding Then callback |
223-
| **Promise Await** | 12.91 ns/op | 0 B/op | Promise await completion |
224-
| **Microtask Scheduling** | 134.7 ns/op | 24 B/op | Microtask queue scheduling |
225-
| **Long Promise Chain (10)** | 10,079 ns/op | 4,066 B/op | 10-level Promise chaining |
226-
| **Simple Promise Chain (5)** | 6,209 ns/op | 2,472 B/op | 5-level Promise chaining |
227-
| **WithResolvers** | 195.3 ns/op | 288 B/op | **Fastest Promise creation method** |
228-
| **WithResolversWithMgr** | 191.9 ns/op | 288 B/op | **Fastest with custom manager** |
225+
| **Promise Creation** | 1,013 ns/op | 437 B/op, 7 allocs/op | Basic Promise instance creation |
226+
| **Then Operation** | 633.1 ns/op | 448 B/op, 7 allocs/op | Adding Then callback |
227+
| **Promise Await** | 32.87 ns/op | 0 B/op, 0 allocs/op | Promise await completion |
228+
| **Microtask Scheduling** | 370.3 ns/op | 134 B/op, 2 allocs/op | Microtask queue scheduling |
229+
| **Long Promise Chain (10)** | 13,399 ns/op | 5,096 B/op, 72 allocs/op | 10-level Promise chaining |
230+
| **Simple Promise Chain (5)** | 6,951 ns/op | 2,975 B/op, 42 allocs/op | 5-level Promise chaining |
231+
| **WithResolvers** | 213.1 ns/op | 288 B/op, 5 allocs/op | **Fastest Promise creation method** |
232+
| **WithResolversWithMgr** | 209.4 ns/op | 288 B/op, 5 allocs/op | **Fastest with custom manager** |
233+
| **Memory Allocation Test** | 2,842 ns/op | 1,417 B/op, 21 allocs/op | Complex chaining memory usage |
234+
| **Concurrent Promise Creation** | 1,201 ns/op | 416 B/op, 6 allocs/op | Concurrent creation performance |
235+
| **Task Pool Reuse** | 758.4 ns/op | 440 B/op, 8 allocs/op | Task object pool reuse effect |
229236

230237
### Performance Highlights
231238

232-
- ⭐ **Excellent Promise Await Performance**: Only 12.91 nanoseconds, can handle 77 million operations per second
233-
- ⭐ **Fastest Promise Creation**: WithResolvers achieves 195.3 ns/op, **3.2x faster** than traditional creation
234-
- ⭐ **Efficient Microtask Scheduling**: 134.7 nanoseconds scheduling time, suitable for high-frequency async operations
235-
- ⭐ **Optimized Memory Usage**: WithResolvers uses only 288 B/op, **35.7% less memory** than traditional creation
236-
- ⭐ **Smooth Chaining Operations**: Each Then operation only takes 359.6 nanoseconds
239+
- ⭐ **Excellent Promise Await Performance**: Only 32.87 nanoseconds, can handle 30 million operations per second
240+
- ⭐ **Fastest Promise Creation**: WithResolversWithMgr achieves 209.4 ns/op, **4.8x faster** than traditional creation
241+
- ⭐ **Efficient Microtask Scheduling**: 370.3 nanoseconds scheduling time, suitable for high-frequency async operations
242+
- ⭐ **Optimized Memory Usage**: WithResolvers uses only 288 B/op, **34.1% less memory** than traditional creation
243+
- ⭐ **Smooth Chaining Operations**: Each Then operation only takes 633.1 nanoseconds
244+
- ⭐ **Significant Task Pool Reuse Effect**: Performance improvement of about 1.3x through object pool reuse
245+
- ⭐ **Excellent Concurrent Performance**: Stable concurrent creation performance, suitable for high-concurrency scenarios
237246

238247
## 🧪 Testing
239248

@@ -255,36 +264,30 @@ go test -v -run Example
255264
go test -bench=. -benchmem
256265
```
257266

258-
## 🔧 Configuration
259-
260-
### Microtask Queue Configuration
261-
262-
```go
263-
import "github.com/fupengl/promise"
267+
### Race Detection Testing
264268

265-
// Configure microtask queue
266-
promise.SetMicrotaskConfig(&promise.MicrotaskConfig{
267-
BufferSize: 2000, // Task buffer size
268-
WorkerCount: 8, // Worker goroutine count
269-
})
269+
```bash
270+
go test -race -v
270271
```
271272

273+
## 🔧 Configuration
274+
272275
### Promise Manager Configuration
273276

274277
```go
275278
import "github.com/fupengl/promise"
276279

277280
// Method 1: Configure through global manager
278-
promise.GetDefaultMgr().SetMicrotaskConfig(&promise.MicrotaskConfig{
279-
BufferSize: 3000,
280-
WorkerCount: 6,
281-
})
282-
promise.GetDefaultMgr().SetExecutorWorker(8)
281+
defaultMgr := promise.GetDefaultMgr()
282+
defaultMgr.SetMicrotaskConfig(6, 3000) // workers, queueSize
283+
defaultMgr.SetExecutorConfig(8, 32) // workers, queueSize
283284

284285
// Method 2: Create custom manager
285-
customMgr := promise.NewPromiseMgrWithConfig(4, &promise.MicrotaskConfig{
286-
BufferSize: 1000,
287-
WorkerCount: 2,
286+
customMgr := promise.NewPromiseMgrWithConfig(&promise.PromiseMgrConfig{
287+
ExecutorWorkers: 4,
288+
ExecutorQueueSize: 16,
289+
MicrotaskWorkers: 2,
290+
MicrotaskQueueSize: 1000,
288291
})
289292

290293
// Create Promise using custom manager
@@ -302,15 +305,32 @@ defer customMgr.Close()
302305
// Get global default manager
303306
defaultMgr := promise.GetDefaultMgr()
304307

305-
// Configure microtask
306-
defaultMgr.SetMicrotaskConfig(config)
307-
defaultMgr.GetMicrotaskConfig()
308+
// Configure microtask (workers, queueSize)
309+
defaultMgr.SetMicrotaskConfig(6, 3000)
310+
311+
// Configure executor (workers, queueSize)
312+
defaultMgr.SetExecutorConfig(8, 32)
308313

309-
// Configure executor worker count
310-
defaultMgr.SetExecutorWorker(workers)
314+
// Get current configuration
315+
config := defaultMgr.GetConfig()
316+
317+
// Reset default manager configurations
318+
promise.ResetDefaultMgrExecutor(6, 24) // Reset executor only
319+
promise.ResetDefaultMgrMicrotask(3, 1500) // Reset microtask only
320+
```
321+
322+
### Configuration Structure
323+
324+
```go
325+
type PromiseMgrConfig struct {
326+
ExecutorWorkers int // Number of executor worker goroutines
327+
ExecutorQueueSize int // Size of executor task queue
328+
MicrotaskWorkers int // Number of microtask worker goroutines
329+
MicrotaskQueueSize int // Size of microtask queue
330+
}
311331

312-
// Reset default manager
313-
promise.ResetDefaultMgr(workers, microtaskConfig)
332+
// Default configuration (automatically calculated based on CPU cores)
333+
func DefaultPromiseMgrConfig() *PromiseMgrConfig
314334
```
315335

316336
## 📖 Complete Documentation

README_CN.md

Lines changed: 74 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- 🎯 **零依赖**: 纯Go实现,无外部依赖
1818
- 🎛️ **灵活配置**: 支持全局和自定义Promise管理器
1919
- 🔧 **资源隔离**: 不同管理器之间互不影响,支持独立配置
20+
- 🚦 **并发安全**: 修复了所有数据竞争问题,线程安全操作
2021

2122
## 📦 安装
2223

@@ -65,9 +66,11 @@ import (
6566

6667
func main() {
6768
// 创建自定义管理器
68-
customMgr := promise.NewPromiseMgrWithConfig(4, &promise.MicrotaskConfig{
69-
BufferSize: 1000,
70-
WorkerCount: 2,
69+
customMgr := promise.NewPromiseMgrWithConfig(&promise.PromiseMgrConfig{
70+
ExecutorWorkers: 4,
71+
ExecutorQueueSize: 16,
72+
MicrotaskWorkers: 2,
73+
MicrotaskQueueSize: 1000,
7174
})
7275
defer customMgr.Close()
7376

@@ -250,38 +253,46 @@ func NewPromiseMgrWithConfig(workers int, microtaskConfig *MicrotaskConfig) *Pro
250253
### 基准测试结果
251254

252255
```
253-
BenchmarkPromiseCreation-12 1978914 618.2 ns/op 448 B/op 8 allocs/op
254-
BenchmarkPromiseThen-12 3443774 359.6 ns/op 336 B/op 7 allocs/op
255-
BenchmarkPromiseAwait-12 89638920 12.91 ns/op 0 B/op 0 allocs/op
256-
BenchmarkMicrotaskQueue-12 8970466 134.7 ns/op 24 B/op 2 allocs/op
257-
BenchmarkPromiseChain-12 170878 10079 ns/op 4066 B/op 72 allocs/op
258-
BenchmarkSimplePromiseChain-12 381231 6209 ns/op 2472 B/op 42 allocs/op
259-
BenchmarkWithResolvers-12 6140624 195.3 ns/op 288 B/op 5 allocs/op
260-
BenchmarkWithResolversWithMgr-12 6223452 191.9 ns/op 288 B/op 5 allocs/op
261-
BenchmarkResolveMultipleTimes-12 4420789 272.0 ns/op 320 B/op 7 allocs/op
262-
BenchmarkRejectMultipleTimes-12 3111844 383.4 ns/op 560 B/op 10 allocs/op
256+
BenchmarkPromiseCreation-12 1,278,765 ops 1,013 ns/op 437 B/op 7 allocs/op
257+
BenchmarkPromiseThen-12 1,918,182 ops 633.1 ns/op 448 B/op 7 allocs/op
258+
BenchmarkPromiseAwait-12 39,064,831 ops 32.87 ns/op 0 B/op 0 allocs/op
259+
BenchmarkMicrotaskQueue-12 3,176,100 ops 370.3 ns/op 134 B/op 2 allocs/op
260+
BenchmarkPromiseChain-12 171,643 ops 13,399 ns/op 5,096 B/op 72 allocs/op
261+
BenchmarkSimplePromiseChain-12 279,733 ops 6,951 ns/op 2,975 B/op 42 allocs/op
262+
BenchmarkWithResolvers-12 5,637,927 ops 213.1 ns/op 288 B/op 5 allocs/op
263+
BenchmarkWithResolversWithMgr-12 5,736,646 ops 209.4 ns/op 288 B/op 5 allocs/op
264+
BenchmarkResolveMultipleTimes-12 4,016,504 ops 298.4 ns/op 320 B/op 7 allocs/op
265+
BenchmarkRejectMultipleTimes-12 2,891,620 ops 410.3 ns/op 560 B/op 10 allocs/op
266+
BenchmarkMemoryAllocation-12 517,053 ops 2,842 ns/op 1,417 B/op 21 allocs/op
267+
BenchmarkConcurrentPromiseCreation-12 1,000,000 ops 1,201 ns/op 416 B/op 6 allocs/op
268+
BenchmarkTaskPoolReuse-12 1,609,479 ops 758.4 ns/op 440 B/op 8 allocs/op
263269
```
264270

265271
### 性能分析
266272

267273
| 操作 | 性能 | 内存分配 | 说明 |
268274
|------|------|----------|------|
269-
| **Promise创建** | 618.2 ns/op | 448 B/op | 基础Promise实例创建 |
270-
| **Then操作** | 359.6 ns/op | 336 B/op | 添加Then回调 |
271-
| **Promise等待** | 12.91 ns/op | 0 B/op | Promise等待完成 |
272-
| **微任务调度** | 134.7 ns/op | 24 B/op | 微任务队列调度 |
273-
| **长Promise链(10个)** | 10,079 ns/op | 4,066 B/op | 10级Promise链式调用 |
274-
| **简单Promise链(5个)** | 6,209 ns/op | 2,472 B/op | 5级Promise链式调用 |
275-
| **WithResolvers** | 195.3 ns/op | 288 B/op | **最快的Promise创建方法** |
276-
| **WithResolversWithMgr** | 191.9 ns/op | 288 B/op | **使用自定义管理器的最快方法** |
275+
| **Promise创建** | 1,013 ns/op | 437 B/op, 7 allocs/op | 基础Promise实例创建 |
276+
| **Then操作** | 633.1 ns/op | 448 B/op, 7 allocs/op | 添加Then回调 |
277+
| **Promise等待** | 32.87 ns/op | 0 B/op, 0 allocs/op | Promise等待完成 |
278+
| **微任务调度** | 370.3 ns/op | 134 B/op, 2 allocs/op | 微任务队列调度 |
279+
| **长Promise链(10个)** | 13,399 ns/op | 5,096 B/op, 72 allocs/op | 10级Promise链式调用 |
280+
| **简单Promise链(5个)** | 6,951 ns/op | 2,975 B/op, 42 allocs/op | 5级Promise链式调用 |
281+
| **WithResolvers** | 213.1 ns/op | 288 B/op, 5 allocs/op | **最快的Promise创建方法** |
282+
| **WithResolversWithMgr** | 209.4 ns/op | 288 B/op, 5 allocs/op | **使用自定义管理器的最快方法** |
283+
| **内存分配测试** | 2,842 ns/op | 1,417 B/op, 21 allocs/op | 复杂链式操作内存使用 |
284+
| **并发Promise创建** | 1,201 ns/op | 416 B/op, 6 allocs/op | 并发创建性能 |
285+
| **Task池复用** | 758.4 ns/op | 440 B/op, 8 allocs/op | Task对象池复用效果 |
277286

278287
### 性能亮点
279288

280-
- ⭐ **Promise等待性能极佳**: 仅需12.91纳秒,每秒可处理7700万次
281-
- ⭐ **最快的Promise创建**: WithResolvers达到195.3 ns/op,**比传统创建快3.2倍**
282-
- ⭐ **微任务调度高效**: 134.7纳秒的调度时间,适合高频异步操作
283-
- ⭐ **优化的内存使用**: WithResolvers仅使用288 B/op,**比传统创建节省35.7%内存**
284-
- ⭐ **链式操作流畅**: 每个Then操作仅需359.6纳秒
289+
- ⭐ **Promise等待性能极佳**: 仅需32.87纳秒,每秒可处理3000万次
290+
- ⭐ **最快的Promise创建**: WithResolversWithMgr达到209.4 ns/op,**比传统创建快4.8倍**
291+
- ⭐ **微任务调度高效**: 370.3纳秒的调度时间,适合高频异步操作
292+
- ⭐ **优化的内存使用**: WithResolvers仅使用288 B/op,**比传统创建节省34.1%内存**
293+
- ⭐ **链式操作流畅**: 每个Then操作仅需633.1纳秒
294+
- ⭐ **Task池复用效果显著**: 通过对象池复用,性能提升约1.3倍
295+
- ⭐ **并发性能优秀**: 并发创建性能稳定,适合高并发场景
285296

286297

287298

@@ -305,36 +316,30 @@ go test -v -run Example
305316
go test -bench=. -benchmem
306317
```
307318

308-
## 🔧 配置
309-
310-
### 微任务队列配置
311-
312-
```go
313-
import "github.com/fupengl/promise"
319+
### 竞态检测测试
314320

315-
// 配置微任务队列
316-
promise.SetMicrotaskConfig(&promise.MicrotaskConfig{
317-
BufferSize: 2000, // 任务缓冲区大小
318-
WorkerCount: 8, // 工作协程数量
319-
})
321+
```bash
322+
go test -race -v
320323
```
321324

325+
## 🔧 配置
326+
322327
### Promise管理器配置
323328

324329
```go
325330
import "github.com/fupengl/promise"
326331

327332
// 方式1:通过全局管理器配置
328-
promise.GetDefaultMgr().SetMicrotaskConfig(&promise.MicrotaskConfig{
329-
BufferSize: 3000,
330-
WorkerCount: 6,
331-
})
332-
promise.GetDefaultMgr().SetExecutorWorker(8)
333+
defaultMgr := promise.GetDefaultMgr()
334+
defaultMgr.SetMicrotaskConfig(6, 3000) // workers, queueSize
335+
defaultMgr.SetExecutorConfig(8, 32) // workers, queueSize
333336

334337
// 方式2:创建自定义管理器
335-
customMgr := promise.NewPromiseMgrWithConfig(4, &promise.MicrotaskConfig{
336-
BufferSize: 1000,
337-
WorkerCount: 2,
338+
customMgr := promise.NewPromiseMgrWithConfig(&promise.PromiseMgrConfig{
339+
ExecutorWorkers: 4,
340+
ExecutorQueueSize: 16,
341+
MicrotaskWorkers: 2,
342+
MicrotaskQueueSize: 1000,
338343
})
339344

340345
// 使用自定义管理器创建Promise
@@ -352,15 +357,32 @@ defer customMgr.Close()
352357
// 获取全局默认管理器
353358
defaultMgr := promise.GetDefaultMgr()
354359

355-
// 配置微任务
356-
defaultMgr.SetMicrotaskConfig(config)
357-
defaultMgr.GetMicrotaskConfig()
360+
// 配置微任务 (workers, queueSize)
361+
defaultMgr.SetMicrotaskConfig(6, 3000)
362+
363+
// 配置executor (workers, queueSize)
364+
defaultMgr.SetExecutorConfig(8, 32)
365+
366+
// 获取当前配置
367+
config := defaultMgr.GetConfig()
358368

359-
// 配置executor worker数量
360-
defaultMgr.SetExecutorWorker(workers)
369+
// 重置默认管理器配置
370+
promise.ResetDefaultMgrExecutor(6, 24) // 仅重置executor
371+
promise.ResetDefaultMgrMicrotask(3, 1500) // 仅重置微任务
372+
```
373+
374+
### 配置结构
375+
376+
```go
377+
type PromiseMgrConfig struct {
378+
ExecutorWorkers int // executor工作协程数量
379+
ExecutorQueueSize int // executor任务队列大小
380+
MicrotaskWorkers int // 微任务工作协程数量
381+
MicrotaskQueueSize int // 微任务队列大小
382+
}
361383

362-
// 重置默认管理器
363-
promise.ResetDefaultMgr(workers, microtaskConfig)
384+
// 默认配置(基于CPU核心数自动计算)
385+
func DefaultPromiseMgrConfig() *PromiseMgrConfig
364386
```
365387

366388
## 📖 完整文档

0 commit comments

Comments
 (0)