Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Shrink System.Diagnostics.Activity by 40 bytes - #37395

Merged
lmolkova merged 4 commits into
dotnet:masterfrom
benaadams:Activity
Jun 19, 2019
Merged

lmolkova merged 4 commits into
dotnet:masterfrom
benaadams:Activity

Conversation

@benaadams

@benaadams benaadams commented May 3, 2019 •

Copy link
Copy Markdown
Member

Shrinks Activity on x64 by 40 bytes from 152 bytes to 112 bytes

ActivitySpanId and ActivityTraceId are shrunk to pointer size and only used for exchange types; so the in and readonly ref are removed on the apis (needs API review?)

ActivitySpanId and ActivityTraceId are managed structs (contain both string and ulongs) so can't be Unsafe.Cast to extract the ulong data and the normal way to extract the data is via .ToString or .ToHexString (for headers) so we can just store them as strings in the first place and not bother the the ulongs.

The other way is via .CopyTo, however this only saves on allocation if the input is not a string (direct binary); and the fields are never used as a string, or output as string. That also comes with extra complexity in size of the struct; and requiring continuous passing via in or readonly ref.

If you read the values from Http Headers or add them to headers, then you go via strings both ways and save on allocations as the values are immediately what they are directly, as well as the smaller Activity.

Before

Type layout for 'Activity'
Size: 152 bytes. Paddings: 2 bytes (%1 of empty space)
|=============================================================|
| Object Header (8 bytes)                                     |
|-------------------------------------------------------------|
| Method Table Ptr (8 bytes)                                  |
|=============================================================|
|   0-7: String <OperationName>k__BackingField (8 bytes)      |
|-------------------------------------------------------------|
|  8-15: Activity <Parent>k__BackingField (8 bytes)           |
|-------------------------------------------------------------|
| 16-23: String _rootId (8 bytes)                             |
|-------------------------------------------------------------|
| 24-31: KeyValueListNode _tags (8 bytes)                     |
|-------------------------------------------------------------|
| 32-39: KeyValueListNode _baggage (8 bytes)                  |
|-------------------------------------------------------------|
| 40-47: String _traceState (8 bytes)                         |
|-------------------------------------------------------------|
| 48-55: String _id (8 bytes)                                 |
|-------------------------------------------------------------|
| 56-63: String _parentId (8 bytes)                           |
|-------------------------------------------------------------|
| 64-67: ActivityIdFormat <IdFormat>k__BackingField (4 bytes) |
|-------------------------------------------------------------|
| 68-71: Int32 _currentChildId (4 bytes)                      |
|-------------------------------------------------------------|
|    72: Byte _w3CIdFlags (1 byte)                            |
|-------------------------------------------------------------|
|    73: Boolean _w3CIdFlagsSet (1 byte)                      |
|-------------------------------------------------------------|
|    74: Boolean _traceIdSet (1 byte)                         |
|-------------------------------------------------------------|
|    75: Boolean _spanIdSet (1 byte)                          |
|-------------------------------------------------------------|
|    76: Boolean _parentSpanIdSet (1 byte)                    |
|-------------------------------------------------------------|
|    77: Boolean isFinished (1 byte)                          |
|-------------------------------------------------------------|
| 78-79: padding (2 bytes)                                    |
|-------------------------------------------------------------|
| 80-87: DateTime <StartTimeUtc>k__BackingField (8 bytes)     |
|-------------------------------------------------------------|
| 88-95: TimeSpan <Duration>k__BackingField (8 bytes)         |
|-------------------------------------------------------------|
| 96-119: ActivityTraceId _traceId (24 bytes)                 |
| |======================================|                    |
| |   0-7: String _asHexString (8 bytes) |                    |
| |--------------------------------------|                    |
| |  8-15: UInt64 _id1 (8 bytes)         |                    |
| |--------------------------------------|                    |
| | 16-23: UInt64 _id2 (8 bytes)         |                    |
| |======================================|                    |
|-------------------------------------------------------------|
| 120-135: ActivitySpanId _spanId (16 bytes)                  |
| |======================================|                    |
| |   0-7: String _asHexString (8 bytes) |                    |
| |--------------------------------------|                    |
| |  8-15: UInt64 _id1 (8 bytes)         |                    |
| |======================================|                    |
|-------------------------------------------------------------|
| 136-151: ActivitySpanId _parentSpanId (16 bytes)            |
| |======================================|                    |
| |   0-7: String _asHexString (8 bytes) |                    |
| |--------------------------------------|                    |
| |  8-15: UInt64 _id1 (8 bytes)         |                    |
| |======================================|                    |
|=============================================================|

After

Type layout for 'Activity'
Size: 112 bytes. Paddings: 2 bytes (%1 of empty space)
|===========================================================|
| Object Header (8 bytes)                                   |
|-----------------------------------------------------------|
| Method Table Ptr (8 bytes)                                |
|===========================================================|
|   0-7: String _traceState (8 bytes)                       |
|-----------------------------------------------------------|
|  8-15: String _id (8 bytes)                               |
|-----------------------------------------------------------|
| 16-23: String _rootId (8 bytes)                           |
|-----------------------------------------------------------|
| 24-31: String _parentId (8 bytes)                         |
|-----------------------------------------------------------|
| 32-39: String _parentSpanId (8 bytes)                     |
|-----------------------------------------------------------|
| 40-47: String _traceId (8 bytes)                          |
|-----------------------------------------------------------|
| 48-55: String _spanId (8 bytes)                           |
|-----------------------------------------------------------|
| 56-63: String <OperationName>k__BackingField (8 bytes)    |
|-----------------------------------------------------------|
| 64-71: Activity <Parent>k__BackingField (8 bytes)         |
|-----------------------------------------------------------|
| 72-79: KeyValueListNode _tags (8 bytes)                   |
|-----------------------------------------------------------|
| 80-87: KeyValueListNode _baggage (8 bytes)                |
|-----------------------------------------------------------|
| 88-91: Int32 _currentChildId (4 bytes)                    |
|-----------------------------------------------------------|
|    92: State _state (1 byte)                              |
|-----------------------------------------------------------|
|    93: Byte _w3CIdFlags (1 byte)                          |
|-----------------------------------------------------------|
| 94-95: padding (2 bytes)                                  |
|-----------------------------------------------------------|
| 96-103: TimeSpan <Duration>k__BackingField (8 bytes)      |
|-----------------------------------------------------------|
| 104-111: DateTime <StartTimeUtc>k__BackingField (8 bytes) |
|===========================================================|

cc: @SergeyKanzhelev @lmolkova @davidfowl @stephentoub

Resolves https://github.com/dotnet/corefx/issues/37396
Resolves https://github.com/dotnet/corefx/issues/38486

@benaadams

Copy link
Copy Markdown
Member Author

@ahsonkhan

ahsonkhan commented May 4, 2019 •

Copy link
Copy Markdown

Type layout for 'Activity'
Size: 152 bytes. Paddings: 2 bytes (%1 of empty space)

What did you do to generate the type layout like this (it's great)?

@benaadams

benaadams commented May 4, 2019 •

Copy link
Copy Markdown
Member Author

What did you do to generate the type layout like this (it's great)?

Using @SergeyTeplyakov's ObjectLayoutInspector and local copies of the changed objects to make it easier to inspect. on NuGet

ObjectLayoutInspector.TypeLayout.PrintLayout<Activity>(recursively: true);

Doesn't work for ref structs though

@ahsonkhan

Copy link
Copy Markdown

Doesn't work for ref structs though

Aww. I was going to try it on Utf8JsonReader. Still cool though :)

@benaadams

Copy link
Copy Markdown
Member Author

Aww. I was going to try it on Utf8JsonReader. Still cool though :)

Can do a local version and switch in a non ref Span (object + int struct); mostly just need all the fields and any auto-properties; can drop the methods to avoid compile errors; and you should end up with same layout?

@lmolkova lmolkova left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you read the values from Http Headers or add them to headers, then you go via strings both ways and save on allocations as the values are immediately what they are directly, as well as the smaller Activity.

There are potential cases when strings are not needed (only binary protocols are used in the service) and tracing system/logger work with ActivityTraceId/SpanId. This is not a real-world scenario (or pretty rare) for now, so I'm ok with making it just a bit less efficient in favor of a more common scenario.

/cc @noahfalk

@noahfalk noahfalk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nit things, but LGTM 👍

@benaadams

Copy link
Copy Markdown
Member Author

@karelz karelz added the blocked Issue/PR is blocked on something - see comments label May 22, 2019
@karelz

karelz commented May 22, 2019

Copy link
Copy Markdown
Member

Marking as blocked by API review ...

@stephentoub

Copy link
Copy Markdown
Member

API review completed.

@karelz karelz removed the blocked Issue/PR is blocked on something - see comments label Jun 5, 2019
@karelz

karelz commented Jun 5, 2019

Copy link
Copy Markdown
Member

Removing blocked as API shape is now approved.

@benaadams

Copy link
Copy Markdown
Member Author

Feedback addressed

@noahfalk noahfalk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks Ben!

@davidfowl

Copy link
Copy Markdown
Member

/azp run corefx-ci

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@benaadams

Copy link
Copy Markdown
Member Author

Anyone fancy merging? Since its an api change would be better to have it flow through earlier.

@benaadams

benaadams commented Jun 13, 2019 •

Copy link
Copy Markdown
Member Author

Looks like I got the .sln file caught up in rebase :sad:

@benaadams
benaadams force-pushed the Activity branch 3 times, most recently from ef7b25f to 725f867 Compare June 13, 2019 13:25
@lmolkova

Copy link
Copy Markdown

@benaadams is it ready to merge?

@benaadams

Copy link
Copy Markdown
Member Author

is it ready to merge?

@lmolkova yes

@lmolkova
lmolkova merged commit edc88b0 into dotnet:master Jun 19, 2019
@karelz karelz added this to the 3.0 milestone Jul 16, 2019
macrogreg pushed a commit to open-telemetry/opentelemetry-dotnet-instrumentation that referenced this pull request Sep 24, 2020
* Shrink Activity

* fix threading issues

* Handle parsing issues

* Fix


Commit migrated from dotnet/corefx@edc88b0
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
* Shrink Activity

* fix threading issues

* Handle parsing issues

* Fix


Commit migrated from dotnet/corefx@edc88b0
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Activity should generate new random ids when receives invalid W3C traceId and spanId Remove in and readonly ref from Activity apis

8 participants