Bug 19075 – rt.util.random.Rand48.defaultSeed should prefer RDTSC or mach_absolute_time or QueryPerformanceCounter to ctime.time
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
x86_64
OS
All
Creation time
2018-07-11T01:45:11Z
Last change time
2018-07-19T21:25:50Z
Assigned to
No Owner
Creator
Nathan S.
Comments
Comment #0 by n8sh.secondary — 2018-07-11T01:45:11Z
`rt.util.random.Rand48.defaultSeed` generates a seed from the clock using `ctime.time`. With `D_InlineAsm_X86_64` it would be better to use the `rdtsc` instruction because it is much faster as well as being finer precision. (Finer precision is a benefit not because we care about accurate time, but because the differences between seeds generated this way are potentially greater.) The same is true of `mach_absolute_time` on macOS. The Microsoft Windows equivalent would be `QueryPerformanceCounter` but I haven't benchmarked it to verify that it is faster than `ctime.time`.
Comment #1 by n8sh.secondary — 2018-07-19T15:48:29Z
Verified that QueryPerformanceCounter is faster.
Comment #2 by github-bugzilla — 2018-07-19T21:25:49Z
Commits pushed to master at https://github.com/dlang/druntimehttps://github.com/dlang/druntime/commit/449ef5265e46d57000bfb5a745b2be5856325e1e
Fix Issue 19075 - rt.util.random.Rand48.defaultSeed should prefer RDTSC or mach_absolute_time or QueryPerformanceCounter to ctime.time
`rt.util.random.Rand48.defaultSeed` generates a seed from the clock
using `ctime.time`. With `D_InlineAsm_X86_64` it would be better to
use the RDTSC instruction because it is much faster as well as being
finer precision. (Finer precision is a benefit not because we care
about accurate time, but because the differences between seeds
generated this way are potentially greater.) The same is true of
`mach_absolute_time` on macOS and `QueryPerformanceCounter` on
Microsoft Windows.
This PR doesn't use RDTSC with `D_InlineAsm_X86` because some x86
processors don't support RDTSC and because on x86 (but not x86-64)
Linux `prctl` can disable a process's ability to use RDTSC.
https://github.com/dlang/druntime/commit/b3edb173828b75c23d3e2bd8614389d4a5ccb845
Merge pull request #2248 from n8sh/rt-defaultSeed-rdtsc
Fix Issue 19075 - rt.util.random.Rand48.defaultSeed should prefer RDTSC or mach_absolute_time or QueryPerformanceCounter to ctime.time