Skip to content

perf: Avoid allocations in OpenTelemetryExtensions #4289

@jamescrosswell

Description

@jamescrosswell

perf: intermediate string allocations

I know this is just a re-format of existing code.
But I noticed an extra string allocations just used as intermediate data for converting from SpanId to ActivitySpanId, and from SentryId to ActivityTraceId:

  • allocating from SpanId/SentryId to string via ToString()
  • converted allocation-free to a ReadOnlySpan<char>
  • where ActivitySpanId.CreateFromString and ActivityTraceId.CreateFromString both internally create a new string that is passed to the ActivitySpanId/ActivityTraceId constructors

If we would - instead of converting to a heap-allocated string and calling CreateFromString,
would instead either

  • format to a stack-allocated Span<char> and call CreateFromString
  • format to a stack-allocated ReadOnlySpan<byte> and call CreateFromUtf8String
  • format to a stack-allocated ReadOnlySpan<byte> and call CreateFromBytes

we should be able to get rid of half of the heap allocations there.

This is a smaller gain, where we could get, in the SentryId case

  • from 176 B (GUID to String with Length 32 and 24 B Class overhead times two)
  • to 88 B (cutting above in half)

Do you think this is worth a future improvement?
If so, then I will create a new issue from this comment, labeled Good First Issue with a low priority.
Otherwise, we just resolve this comment.

Originally posted by @Flash0ver in #4283 (comment)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions