-
Notifications
You must be signed in to change notification settings - Fork 787
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
move for to foreach #6103
Conversation
@@ -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) |
There was a problem hiding this comment.
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 for
s are written specifically to cheat and avoid these allocations. Avoiding allocations is a great good than readability in this case IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fair enough. will close
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ 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
|
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
CHANGELOG.md
files updated for non-trivial changes