@@ -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
255264go 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
275278import "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
303306defaultMgr := 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
0 commit comments