-
Notifications
You must be signed in to change notification settings - Fork 117
Fix High CPU (#189) #194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix High CPU (#189) #194
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| The code in `event` was originally a part of https://github.com/kelindar/event (v1.5.2) | ||
|
|
||
| The original code uses a `time.Ticker` to process the event queue which caused a large increase in CPU usage ([#189](https://github.com/mostlygeek/llama-swap/issues/189)). This code was ported to remove the ticker and instead be more event driven. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // Copyright (c) Roman Atachiants and contributore. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for detaile. | ||
|
|
||
| package event | ||
|
|
||
| import ( | ||
| "context" | ||
| ) | ||
|
|
||
| // Default initializes a default in-process dispatcher | ||
| var Default = NewDispatcherConfig(25000) | ||
|
|
||
| // On subscribes to an event, the type of the event will be automatically | ||
| // inferred from the provided type. Must be constant for this to work. This | ||
| // functions same way as Subscribe() but uses the default dispatcher instead. | ||
| func On[T Event](handler func(T)) context.CancelFunc { | ||
| return Subscribe(Default, handler) | ||
| } | ||
|
|
||
| // OnType subscribes to an event with the specified event type. This functions | ||
| // same way as SubscribeTo() but uses the default dispatcher instead. | ||
| func OnType[T Event](eventType uint32, handler func(T)) context.CancelFunc { | ||
| return SubscribeTo(Default, eventType, handler) | ||
| } | ||
|
|
||
| // Emit writes an event into the dispatcher. This functions same way as | ||
| // Publish() but uses the default dispatcher instead. | ||
| func Emit[T Event](ev T) { | ||
| Publish(Default, ev) | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,54 @@ | ||||||||||
| // Copyright (c) Roman Atachiants and contributore. All rights reserved. | ||||||||||
| // Licensed under the MIT license. See LICENSE file in the project root for detaile. | ||||||||||
|
Comment on lines
+1
to
+2
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix typos in copyright header. The copyright header contains typos that should be corrected. -// Copyright (c) Roman Atachiants and contributore. All rights reserved.
-// Licensed under the MIT license. See LICENSE file in the project root for detaile.
+// Copyright (c) Roman Atachiants and contributors. All rights reserved.
+// Licensed under the MIT license. See LICENSE file in the project root for details.📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||
|
|
||||||||||
| package event | ||||||||||
|
|
||||||||||
| import ( | ||||||||||
| "sync" | ||||||||||
| "sync/atomic" | ||||||||||
| "testing" | ||||||||||
|
|
||||||||||
| "github.com/stretchr/testify/assert" | ||||||||||
| ) | ||||||||||
|
|
||||||||||
| /* | ||||||||||
| cpu: 13th Gen Intel(R) Core(TM) i7-13700K | ||||||||||
| BenchmarkSubcribeConcurrent-24 1826686 606.3 ns/op 1648 B/op 5 allocs/op | ||||||||||
| */ | ||||||||||
| func BenchmarkSubscribeConcurrent(b *testing.B) { | ||||||||||
| d := NewDispatcher() | ||||||||||
| b.ReportAllocs() | ||||||||||
| b.ResetTimer() | ||||||||||
|
|
||||||||||
| b.RunParallel(func(pb *testing.PB) { | ||||||||||
| for pb.Next() { | ||||||||||
| unsub := Subscribe(d, func(ev MyEvent1) {}) | ||||||||||
| unsub() | ||||||||||
| } | ||||||||||
| }) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func TestDefaultPublish(t *testing.T) { | ||||||||||
| var wg sync.WaitGroup | ||||||||||
|
|
||||||||||
| // Subscribe | ||||||||||
| var count int64 | ||||||||||
| defer On(func(ev MyEvent1) { | ||||||||||
| atomic.AddInt64(&count, 1) | ||||||||||
| wg.Done() | ||||||||||
| })() | ||||||||||
|
|
||||||||||
| defer OnType(TypeEvent1, func(ev MyEvent1) { | ||||||||||
| atomic.AddInt64(&count, 1) | ||||||||||
| wg.Done() | ||||||||||
| })() | ||||||||||
|
|
||||||||||
| // Publish | ||||||||||
| wg.Add(4) | ||||||||||
| Emit(MyEvent1{}) | ||||||||||
| Emit(MyEvent1{}) | ||||||||||
|
|
||||||||||
| // Wait and check | ||||||||||
| wg.Wait() | ||||||||||
| assert.Equal(t, int64(4), count) | ||||||||||
| } | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix typos in copyright header.
Same typos as in the test file.
📝 Committable suggestion
🤖 Prompt for AI Agents