Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2919 from n8sh/issue-20550
Browse files Browse the repository at this point in the history
Fix Issue 20550 - Use fixed seeds for treaps in GC
merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
  • Loading branch information
dlang-bot authored Feb 6, 2020
2 parents 51841bc + bc5832a commit 0f9cd78
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 63 deletions.
4 changes: 2 additions & 2 deletions src/gc/impl/conservative/gc.d
Original file line number Diff line number Diff line change
Expand Up @@ -1256,8 +1256,8 @@ struct Gcx
{
(cast(byte*)&this)[0 .. Gcx.sizeof] = 0;
leakDetector.initialize(&this);
roots.initialize();
ranges.initialize();
roots.initialize(0x243F6A8885A308D3UL);
ranges.initialize(0x13198A2E03707344UL);
smallCollectThreshold = largeCollectThreshold = 0.0f;
usedSmallPages = usedLargePages = 0;
mappedPages = 0;
Expand Down
7 changes: 4 additions & 3 deletions src/rt/util/container/treap.d
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ nothrow:
removeAll();
}

void initialize()
void initialize(ulong randSeed)
{
rand48.defaultSeed();
Rand48 _rand48 = { randSeed };
rand48 = _rand48;
}

void insert(E element) @nogc
Expand Down Expand Up @@ -262,8 +263,8 @@ unittest
OP[] ops;
uint[] opdata;

treap.initialize();
srand(cast(uint)time(null));
treap.initialize(rand());

uint[] data;
initialLoop:
Expand Down
58 changes: 0 additions & 58 deletions src/rt/util/random.d
Original file line number Diff line number Diff line change
Expand Up @@ -11,66 +11,8 @@ struct Rand48
private ulong rng_state;

@safe @nogc nothrow:

void defaultSeed() @trusted
{
version (D_InlineAsm_X86_64)
{
// RDTSC takes around 22 clock cycles.
ulong result = void; // Workaround for LDC issue #950, cannot access struct members in DMD asm.
asm @nogc nothrow
{
rdtsc;
// RAX: low 32 bits are low bits of timestamp, high 32 bits are 0.
// RDX: low 32 bits are high bits of timestamp, high 32 bits are 0.
// We combine these into a 48 bit value instead of a full 64 bits
// because `front` and `popFront` only make use of the bottom 48
// bits of `rng_state`.
shl RDX, 16;
xor RDX, RAX;
mov result, RDX;
}
rng_state = result;
popFront();
}
//else version (D_InlineAsm_X86)
//{
// // We don't use `rdtsc` with version (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`.
// static assert(0);
//}
else version (Windows)
{
// QueryPerformanceCounter takes about 1/4 the time of ctime.time.
import core.sys.windows.winbase : QueryPerformanceCounter;
QueryPerformanceCounter(cast(long*) &rng_state);
popFront();
}
else version (OSX)
{
// mach_absolute_time is much faster than ctime.time.
import core.time : mach_absolute_time;
rng_state = mach_absolute_time();
popFront();
}
else
{
// Fallback to libc timestamp in seconds.
import ctime = core.stdc.time : time;
seed((cast(uint) ctime.time(null)));
}
}

pure:

void seed(uint seedval)
{
rng_state = cast(ulong)seedval << 16 | 0x330e;
popFront();
}

auto opCall()
{
auto result = front;
Expand Down

0 comments on commit 0f9cd78

Please sign in to comment.