Hello! I'm trying to deal with serious performance drops in the application.

Drawdowns about which there is a speech appear periodically. Mostly, the iterations are worked out rather quickly, but they are diluted with iterations, which take much longer. Since the application has a graphical display of information, it all looks very twitchy and requires adjustment.

Please look at the following code:

while (true) { var rng = new Random(1); var concurrenBag = new ConcurrentBag<ICollection<(int X, int Y)>>(); Parallel.For(0, 20000, i => { var entry = new List<(int X, int Y)>(); // здСсь Π΄Π΅ΠΌΠΎΠ½ΡΡ‚Ρ€ΠΈΡ€ΡƒΡŽΡ‚ΡΡ основныС аспСкты Ρ€Π΅Π°Π»ΡŒΠ½ΠΎΠ³ΠΎ Π°Π»Π³ΠΎΡ€ΠΈΡ‚ΠΌΠ° var r = rng.Next(0, 3); // ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π½ΠΎ 20ΠΊ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠΎΠ² Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°ΡŽΡ‚ ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΡŽ ΠΏΠ°Ρ€Ρ‹ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚ for (var j = 0; j < r; j++) // ΠΈΠ½ΠΎΠ³Π΄Π° Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°ΡŽΡ‚ΡΡ null'Ρ‹, ΠΈΠ½ΠΎΠ³Π΄Π° коллСкция с 1 записью, Ρ‡Π°Ρ‰Π΅ - с 2-мя { // всС Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠ°ΠΌΠΈ значСния ΠΈΠ΄ΡƒΡ‚ Π² ConcurrentBag entry.Add((j, j * j)); } if (entry.Count == 0) entry = null; concurrenBag.Add(entry); }); var sw = Stopwatch.StartNew(); var results = concurrenBag.ToList().AsParallel().Where(x => x != null).SelectMany(x => x).Distinct().ToList(); // ΠΊΠ°ΠΊ Π²Ρ‹ΡΡΠ½ΠΈΠ»ΠΎΡΡŒ, просадки Π² ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ΄ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΠΈ Π²Π΅Π΄ΡƒΡ‚ Π½Π° эту строчку var time = sw.ElapsedMilliseconds; Console.WriteLine($"CB count: {concurrenBag.Count:00000}, result count: {results.Count:00}, time: {time:000}"); Thread.Sleep(1000); } 

The above code leads to the following result in the console:

 CB count: 20000, result count: 02, time: 032 <- 32мс, для ΠΏΠ΅Ρ€Π²ΠΎΠΉ ΠΈΡ‚Π΅Ρ€Π°Ρ†ΠΈΠΈ с ΡƒΡ‡Π΅Ρ‚ΠΎΠΌ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ ΠΈ всСго Ρ‚Π°ΠΊΠΎΠ³ΠΎ это Π½ΠΎΡ€ΠΌΠ° CB count: 20000, result count: 02, time: 004 CB count: 20000, result count: 02, time: 014 <- 14мс, ΡƒΠΆΠ΅ Π½Π΅ Π½ΠΎΡ€ΠΌΠ° CB count: 20000, result count: 02, time: 003 CB count: 20000, result count: 02, time: 004 CB count: 20000, result count: 02, time: 004 CB count: 20000, result count: 02, time: 003 CB count: 20000, result count: 02, time: 015 <- ΠΊΠ°ΠΊ Π²ΠΈΠ΄Π½ΠΎ, повторяСтся ΠΊΠ°ΠΆΠ΄Ρ‹Π΅ нСсколько ΠΈΡ‚Π΅Ρ€Π°Ρ†ΠΈΠΉ CB count: 20000, result count: 02, time: 003 CB count: 20000, result count: 02, time: 019 CB count: 20000, result count: 02, time: 004 CB count: 20000, result count: 02, time: 004 CB count: 20000, result count: 02, time: 003 CB count: 20000, result count: 02, time: 014 CB count: 20000, result count: 02, time: 003 CB count: 20000, result count: 02, time: 004 CB count: 20000, result count: 02, time: 003 CB count: 20000, result count: 02, time: 008 CB count: 20000, result count: 02, time: 003 CB count: 20000, result count: 02, time: 004 CB count: 20000, result count: 02, time: 011 CB count: 20000, result count: 02, time: 003 CB count: 20000, result count: 02, time: 003 CB count: 20000, result count: 02, time: 004 

I think the idea is clear. In a real application, each "normal" iteration takes 10-15ms, and "subsiding" occur every 6-8 iterations and take up to 150ms

Initially, I assumed that the problem could be with business logic, but you can run the example above and get about the same results. Now I assume that something is wrong with the way I use Parallel.For , AsParallel() or ConcurrentBag , but I have no idea what exactly is wrong.

  • try removing this part .ToList().AsParallel() - tym32167
  • @ tym32167 Thanks for the reply! Unfortunately, this did not help. Periodic drawdowns still occur. Just launched an example - the usual iteration is ~ 2ms, and every 6-8 one iteration is ~ 13-14ms. - Dmitry Volkov
  • one
    Why do you need ConcurrentBag at all? What did not suit Enumerable.Range(...).AsParallel().SelectMany(...)? - Pavel Mayorov
  • 2
    @DmitryVolkov so much more! Do preprocessed.AsParallel() and jump! Why do you need Parallel.For ? - Pavel Mayorov
  • one
    You have a bill here for milliseconds, so it can be everything. whatever, for example, GC. Try to put GC.Collect(); before your measurements (although it does not guarantee anything), well, plus Paul - tym32167

0