Bug 13136 - System.Random exhibits odd performance issue with threads
Summary: System.Random exhibits odd performance issue with threads
Status: RESOLVED NOT_ON_ROADMAP
Alias: None
Product: Runtime
Classification: Mono
Component: GC ()
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-07-09 16:41 UTC by Reimer Behrends
Modified: 2013-07-17 23:39 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Sample code to replicate the issue. (475 bytes, application/octet-stream)
2013-07-09 16:42 UTC, Reimer Behrends
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and 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 Links.

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.

Related Links:
Status:
RESOLVED NOT_ON_ROADMAP

Description Reimer Behrends 2013-07-09 16:41:03 UTC
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.
Comment 1 Reimer Behrends 2013-07-09 16:42:22 UTC
Created attachment 4297 [details]
Sample code to replicate the issue.
Comment 2 Marek Safar 2013-07-10 03:33:03 UTC
There is additional System.Threading.InternalThread when running with boehm but not sure why

On my machine
Boehm: ~10secs
Sgen: ~4secs

Even if there is no allocation involved
Comment 3 Zoltan Varga 2013-07-16 19:11:34 UTC
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.
Comment 4 Zoltan Varga 2013-07-17 23:39:17 UTC
-> NOTABUG.