dave-hillier / disruptor-unity3d

Basic implementation of Disruptor for Unity3d
Apache License 2.0
200 stars 35 forks source link

32 Threads with Ringed Buffers blows up unity #7

Closed NVentimiglia closed 6 years ago

NVentimiglia commented 6 years ago

In my quest for benchmarks, I have blown up unity. In this instance, I passed in 32. Maybe not applicable to a client, but for a server. Similar tests going against the que and lock passed. this test pushed my CPU to 100, and locked up my machine (hard reset).


 public void BenchmarkRing(int count)
        {
            for (int t = 0; t < TESTS; t++) // 15
            {
                COUNTER = 0;
                var list = new List<Thread>();
                var que = new RingBuffer<int>(count);
                var watch = new Stopwatch();
                for (int i = 0; i < count; i++)
                {
                    list.Add(new Thread(() =>
                    {
                        while (COUNT >= COUNTER) // 100,000
                        {
                            que.Enqueue(COUNTER++);
                        }
                        watch.Stop();
                    }));
                    list.Add(new Thread(() =>
                    {
                        while (COUNT >= COUNTER)
                        {
                            int c = 0;
                            que.TryDequeue(out c);
                        }
                        watch.Stop();
                    }));
                }

                watch.Start();
                for (int i = 0; i < list.Count; i++)
                {
                    list[i].Start();
                }

                while (COUNT >= COUNTER)
                {
                    Thread.Sleep(100);
                }

                UnityEngine.Debug.LogWarning("BenchmarkRing - Threads : " + list.Count + " | ms :" + watch.ElapsedMilliseconds);
                GC.Collect();

            }
        }
dave-hillier commented 6 years ago

Not sure what I can do about this

NVentimiglia commented 6 years ago

Ill do another pass this weekend see what I can find

dave-hillier commented 6 years ago

The ring buffer is supposed to be single consumer-single producer; you've got 32 on the one buffer From the first paragraph of Readme.md

Only supports a single producer/single consumer.

I'd suggest you use https://github.com/disruptor-net/Disruptor-net if you want something more complex.

nxrighthere commented 5 years ago

@NVentimiglia for a high-parallelism degree where you need multi-producer multi-consumer bounded queue you can use ConcurrentBuffer from NetStack. But be aware of CMPXCHG instruction scalability and measure stuff.