1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-05 22:54:18 +02:00

Optimize evictedQueue implementation and use (#2556)

* Optimize evictedQueue impl and use

Avoid unnecessary allocations in the recordingSpan by using an
evictedQueue type instead of a pointer to one.

Lazy allocate the evictedQueue queue to prevent unnecessary operations
for spans without any use of the queue.

Document the evictedQueue

* Fix grammar
This commit is contained in:
Tyler Yahn 2022-01-27 13:55:21 -08:00 committed by GitHub
parent 310c7be3b4
commit d3bb03883b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 10 deletions

View File

@ -14,24 +14,25 @@
package trace // import "go.opentelemetry.io/otel/sdk/trace" package trace // import "go.opentelemetry.io/otel/sdk/trace"
// evictedQueue is a FIFO queue with a configurable capacity.
type evictedQueue struct { type evictedQueue struct {
queue []interface{} queue []interface{}
capacity int capacity int
droppedCount int droppedCount int
} }
func newEvictedQueue(capacity int) *evictedQueue { func newEvictedQueue(capacity int) evictedQueue {
eq := &evictedQueue{ // Do not pre-allocate queue, do this lazily.
capacity: capacity, return evictedQueue{capacity: capacity}
queue: make([]interface{}, 0),
}
return eq
} }
// add adds value to the evictedQueue eq. If eq is at capacity, the oldest
// queued value will be discarded and the drop count incremented.
func (eq *evictedQueue) add(value interface{}) { func (eq *evictedQueue) add(value interface{}) {
if len(eq.queue) == eq.capacity { if len(eq.queue) == eq.capacity {
eq.queue = eq.queue[1:] // Drop first-in while avoiding allocating more capacity to eq.queue.
copy(eq.queue[:eq.capacity-1], eq.queue[1:])
eq.queue = eq.queue[:eq.capacity-1]
eq.droppedCount++ eq.droppedCount++
} }
eq.queue = append(eq.queue, value) eq.queue = append(eq.queue, value)

View File

@ -142,10 +142,10 @@ type recordingSpan struct {
attributes *attributesMap attributes *attributesMap
// events are stored in FIFO queue capped by configured limit. // events are stored in FIFO queue capped by configured limit.
events *evictedQueue events evictedQueue
// links are stored in FIFO queue capped by configured limit. // links are stored in FIFO queue capped by configured limit.
links *evictedQueue links evictedQueue
// executionTracerTaskEnd ends the execution tracer span. // executionTracerTaskEnd ends the execution tracer span.
executionTracerTaskEnd func() executionTracerTaskEnd func()