Notice (2018-05-24): bugzilla.xamarin.com is now in
Please join us on
Visual Studio Developer Community and in the
Mono organizations on
GitHub to continue tracking issues. Bugzilla will remain
available for reference in read-only mode. We will continue to work
on open Bugzilla bugs, copy them to the new locations
as needed for follow-up, and add the new items under Related
Our sincere thanks to everyone who has contributed on this bug
tracker over the years. Thanks also for your understanding as we
make these adjustments and improvements for the future.
Please create a new report on
GitHub or Developer Community with
your current version information, steps to reproduce, and relevant error
messages or log files if you are hitting an issue that looks similar to
this resolved bug and you do not yet see a matching new report.
System.Random.NextDouble() seems to do inexplicable memory allocations that result in a considerable slowdown when using the Boehm garbage collector, presumably due to the global allocation lock. Running the attached code is considerably faster with --gc=sgen.
This would not be surprising if there were a memory allocation involved. Yet the NextDouble()/Sample() code should not allocate any memory at all. When creating a custom version of mcs/class/corlib/System/Random.cs in a different namespace and calling that, the speed issue disappears as soon as the first two lines of the Sample() code (which increment inext and inextp with wrap-around semantics) are removed. I would suspect a boxing/unboxing issue, except that there shouldn't be any boxing/unboxing involved.
This was observed on OS X with Mono 3.0.10 and on Linux with Mono 3.0.12.
Created attachment 4297 [details]
Sample code to replicate the issue.
There is additional System.Threading.InternalThread when running with boehm but not sure why
On my machine
Even if there is no allocation involved
This is due to cache effects, the Random () instances get allocated to the same cache line, so the state changes done by Random.Sample() have to be synchronized between the cores.