rspec/rules/S3055/csharp/rule.adoc

44 lines
1.3 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
Synchronization can be expensive in terms of time when multiple threads need to pass through the same bottleneck (method with ``++[MethodImpl(MethodImplOptions.Synchronized)]++``).
2020-06-30 12:48:39 +02:00
2021-02-02 15:02:10 +01:00
If you have a piece of code calling a method with ``++[MethodImpl(MethodImplOptions.Synchronized)]++`` attribute once, then it only has to wait its turn to pass through the bottleneck once. But call it in a loop, and your code has to get back in line for the bottleneck over and over.
2020-06-30 12:48:39 +02:00
2021-02-02 15:02:10 +01:00
2020-06-30 12:48:39 +02:00
Instead, it would be better to get into the bottleneck, and then do the looping. I.e. consider refactoring the code to perform the loop inside the method.
2021-02-02 15:02:10 +01:00
This rule raises an issue when a method with ``++[MethodImpl(MethodImplOptions.Synchronized)]++`` is called in a loop.
2020-06-30 12:48:39 +02:00
=== Noncompliant code example
2020-06-30 12:48:39 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2020-06-30 12:48:39 +02:00
----
public void doSomething(int max) {
for (int i = 0; i < max; i++) {
doSynchronized(i); // Noncompliant
}
}
[MethodImpl(MethodImplOptions.Synchronized)]
public void doSynchronized(int val) {
// ...
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]