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

move for to foreach #6103

Closed

Conversation

SimonCropp
Copy link
Contributor

Changes

given the majority of loops in the repo are written as foreach. i figure converting the remaining for loops to foreach makes sense.

also IMO foreach loops improve code readability compared to for loops

Merge requirement checklist

  • CONTRIBUTING guidelines followed (license requirements, nullable enabled, static analysis, etc.)
  • Unit tests added/updated
  • Appropriate CHANGELOG.md files updated for non-trivial changes
  • Changes in public API reviewed (if applicable)

@SimonCropp SimonCropp requested a review from a team as a code owner January 26, 2025 23:48
@github-actions github-actions bot added pkg:OpenTelemetry.Api Issues related to OpenTelemetry.Api NuGet package pkg:OpenTelemetry.Exporter.Console Issues related to OpenTelemetry.Exporter.Console NuGet package pkg:OpenTelemetry.Exporter.OpenTelemetryProtocol Issues related to OpenTelemetry.Exporter.OpenTelemetryProtocol NuGet package pkg:OpenTelemetry.Exporter.Prometheus.HttpListener Issues related to OpenTelemetry.Exporter.Prometheus.HttpListener NuGet package pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package labels Jan 26, 2025
@@ -68,9 +68,9 @@ public CompositeTextMapPropagator(IEnumerable<TextMapPropagator> propagators)
/// <inheritdoc/>
public override PropagationContext Extract<T>(PropagationContext context, T carrier, Func<T, string, IEnumerable<string>?> getter)
{
for (int i = 0; i < this.propagators.Count; i++)
foreach (var propagator in this.propagators)
Copy link
Member

Choose a reason for hiding this comment

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

The problem here is when you foreach it is a call to _thing_.GetEnumerator(). If thing is an interface like IEnumerable or IReadOnlyList then GetEnumerator() usually returns an IEnumerator. The implementation for the enumerator on the concrete type is usually a struct so converting it to IEnumerator causes a boxing allocation. Agree the foreach is more readable, but the fors are written specifically to cheat and avoid these allocations. Avoiding allocations is a great good than readability in this case IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fair enough. will close

@SimonCropp SimonCropp closed this Jan 28, 2025
Copy link

codecov bot commented Jan 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.28%. Comparing base (b508b84) to head (6551e44).
Report is 4 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #6103      +/-   ##
==========================================
- Coverage   86.40%   86.28%   -0.12%     
==========================================
  Files         257      257              
  Lines       11649    11506     -143     
==========================================
- Hits        10065     9928     -137     
+ Misses       1584     1578       -6     
Files with missing lines Coverage Δ
.../Context/Propagation/CompositeTextMapPropagator.cs 100.00% <ø> (ø)
...metry.Exporter.Console/ConsoleLogRecordExporter.cs 0.00% <ø> (ø)
...ementation/Serializer/ProtobufOtlpLogSerializer.cs 98.47% <ø> (-0.03%) ⬇️
...ntation/Serializer/ProtobufOtlpMetricSerializer.cs 97.84% <ø> (-0.94%) ⬇️
...ation/Serializer/ProtobufOtlpResourceSerializer.cs 90.90% <ø> (-0.76%) ⬇️
...entation/Serializer/ProtobufOtlpTraceSerializer.cs 94.94% <ø> (-0.08%) ⬇️
...heus.HttpListener/Internal/PrometheusSerializer.cs 86.90% <ø> (-0.74%) ⬇️
src/OpenTelemetry/Metrics/MetricState.cs 96.55% <ø> (+5.12%) ⬆️
src/OpenTelemetry/Metrics/MetricStreamIdentity.cs 88.59% <ø> (-0.20%) ⬇️
...enTelemetry/Metrics/StringArrayEqualityComparer.cs 47.05% <ø> (-5.58%) ⬇️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg:OpenTelemetry.Api Issues related to OpenTelemetry.Api NuGet package pkg:OpenTelemetry.Exporter.Console Issues related to OpenTelemetry.Exporter.Console NuGet package pkg:OpenTelemetry.Exporter.OpenTelemetryProtocol Issues related to OpenTelemetry.Exporter.OpenTelemetryProtocol NuGet package pkg:OpenTelemetry.Exporter.Prometheus.HttpListener Issues related to OpenTelemetry.Exporter.Prometheus.HttpListener NuGet package pkg:OpenTelemetry Issues related to OpenTelemetry NuGet package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants