-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
C# [EC86] GC.Collect should not be called & [EC87] Use collection ind…
…exer (#295)
- Loading branch information
Showing
7 changed files
with
116 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
ecocode-rules-specifications/src/main/rules/EC86/EC86.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"title": "GC.Collect should not be called", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "10min" | ||
}, | ||
"tags": [ | ||
"eco-design", | ||
"ecocode", | ||
"bad-practice" | ||
], | ||
"defaultSeverity": "Major" | ||
} |
37 changes: 37 additions & 0 deletions
37
ecocode-rules-specifications/src/main/rules/EC86/csharp/EC86.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
:!sectids: | ||
|
||
`GC.Collect` should not be called. | ||
|
||
## Why is this an issue ? | ||
|
||
Calling `GC.Collect` forces a garbage collection to occur. This can be a very expensive operation, as it is fully blocking and can take a significant amount of time to complete. | ||
In most cases, the garbage collector is able to determine when it is appropriate to run a collection, and calling `GC.Collect` is not only unnecessary but also not advisable. | ||
|
||
Calling `GC.Collect` on generation 0 (ephemeral objects) is excluded from this rule, as it is very lightweight in comparison to the other generations. | ||
|
||
### When can it be ignored ? | ||
|
||
In general, it is not recommended to call GC.Collect as the cost far outweighs the benefits. | ||
In some cases however, typically if you know you have a lot of long-lived objects whose memory you need reclaimed immediately, calling `GC.Collect` can make sense. | ||
|
||
## Non-compliant examples | ||
|
||
[source, cs] | ||
---- | ||
public void Method(); | ||
{ | ||
GC.Collect(); // Non-compliant, same as GC.Collect(generation: GC.MaxGeneration) | ||
GC.Collect(generation: 2); // Non-compliant | ||
} | ||
---- | ||
|
||
## Compliant examples | ||
|
||
[source, cs] | ||
---- | ||
public void Method(); | ||
{ | ||
GC.Collect(generation: 0); // Compliant | ||
GC.Collect(generation: 0, mode: GCCollectionMode.Optimized); // Compliant | ||
} | ||
---- |
16 changes: 16 additions & 0 deletions
16
ecocode-rules-specifications/src/main/rules/EC87/EC87.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"title": "Use collection indexer", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "5min" | ||
}, | ||
"tags": [ | ||
"eco-design", | ||
"ecocode", | ||
"performance", | ||
"bad-practice" | ||
], | ||
"defaultSeverity": "Major" | ||
} |
36 changes: 36 additions & 0 deletions
36
ecocode-rules-specifications/src/main/rules/EC87/csharp/EC87.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
:!sectids: | ||
|
||
Use collection indexer. | ||
|
||
## Why is this an issue ? | ||
|
||
Linq methods like `First()`, `Last()` and `ElementAt()` can be necessary on enumerable types that don't have an indexer. | ||
But for those that implement `IReadOnlyList<T>`, `IList<T>` or `IList`, direct index access is cheaper at runtime and should be used instead. | ||
|
||
### When can it be ignored ? | ||
|
||
This rule shouldn't be ignored. | ||
|
||
## Non-compliant examples | ||
|
||
[source, cs] | ||
---- | ||
public static void Test(int[] arr) | ||
{ | ||
int first = arr.First(); // Non-compliant, use arr[0] | ||
int last = arr.Last(); // Non-compliant, use arr[^1], or arr[arr.Length - 1] if C# < 8 | ||
int third = arr.ElementAt(2); // Non-compliant, use arr[2] | ||
} | ||
---- | ||
|
||
## Compliant examples | ||
|
||
[source, cs] | ||
---- | ||
public static void Test(List<int> list) | ||
{ | ||
int first = list[0]; | ||
int last = list[^1]; // Or list[list.Count - 1] if C# < 8 | ||
int third = list[2]; | ||
} | ||
---- |