Skip to content
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

[repo] Small cleanup/simplification #6048

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

pentp
Copy link

@pentp pentp commented Jan 6, 2025

Some basic cleanup and simplification.

@pentp pentp requested a review from a team as a code owner January 6, 2025 09:58
Copy link

linux-foundation-easycla bot commented Jan 6, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@github-actions github-actions bot added pkg:OpenTelemetry.Api.ProviderBuilderExtensions Issues related to OpenTelemetry.Api.ProviderBuilderExtensions NuGet package pkg:OpenTelemetry.Api Issues related to OpenTelemetry.Api NuGet package pkg:OpenTelemetry.Exporter.OpenTelemetryProtocol Issues related to OpenTelemetry.Exporter.OpenTelemetryProtocol NuGet package pkg:OpenTelemetry.Extensions.Hosting Issues related to OpenTelemetry.Extensions.Hosting NuGet package pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package labels Jan 6, 2025
{
var sw = default(SpinWait);
while (true)
{
sw.SpinOnce();

double currentValue = Volatile.Read(ref location);
var returnedValue = Interlocked.CompareExchange(ref location, currentValue + value, currentValue);
Copy link

@rhuijben rhuijben Jan 7, 2025

Choose a reason for hiding this comment

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

Doesn't just refetching the new value break the compare exchange pattern?

If that is not needed, you can just overwrite what is there, without looking at the current value.

Copy link
Author

@pentp pentp Jan 7, 2025

Choose a reason for hiding this comment

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

Not sure if I understood the question correctly, but the new value is calculated based on the current value. Previously it used currentValue from the previous failed compare-exchange, but after spin waiting, which doesn't make much sense since the value is quite old by that point (and thus likely to fail again).

Copy link
Author

Choose a reason for hiding this comment

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

I haven't seen spin waits used with compare exchange in simple flows like this where it's guaranteed that forward progress is made even under heavy contention. So maybe should remove the spin wait and then it would make sense to use the compare exchange result for current value.

Copy link
Author

Choose a reason for hiding this comment

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

I removed the spin wait since just doing compare exchange in a loop is more efficient and produces smaller machine code.

@@ -36,14 +36,14 @@ public DirectorySizeTracker(long maxSizeInBytes, string path)
/// <returns>True if space is available else false.</returns>
public bool IsSpaceAvailable(out long currentSizeInBytes)
{
currentSizeInBytes = Interlocked.Read(ref this.directoryCurrentSizeInBytes);
currentSizeInBytes = Volatile.Read(ref this.directoryCurrentSizeInBytes);
Copy link

Choose a reason for hiding this comment

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

Why?

Is this still safe on 32 bit machines?

Copy link
Author

Choose a reason for hiding this comment

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

From the docs: "Volatile reads and writes on such 64-bit memory are atomic even on 32-bit processors, unlike regular reads and writes."

It's significantly faster to use volatile reads (that are basically as fast as regular reads with just some additional guarantees about memory ordering) than interlocked operations that take exclusive ownership of the cache line and take at least 40 CPU cycles.

@CodeBlanch
Copy link
Member

@pentp There's some stuff on here I would be happy to merge. Like adding readonly or simplifying lambdas, etc. There's other stuff I think we need to look at more closely like switching from Interlocked to Volatile. Would you be willing to split up this PR?

@CodeBlanch CodeBlanch changed the title Small metrics handling optimizations/cleanup [repo] Small cleanup/simplification Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg:OpenTelemetry.Api.ProviderBuilderExtensions Issues related to OpenTelemetry.Api.ProviderBuilderExtensions NuGet package pkg:OpenTelemetry.Api Issues related to OpenTelemetry.Api NuGet package pkg:OpenTelemetry.Exporter.OpenTelemetryProtocol Issues related to OpenTelemetry.Exporter.OpenTelemetryProtocol NuGet package pkg:OpenTelemetry.Extensions.Hosting Issues related to OpenTelemetry.Extensions.Hosting NuGet package pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants