aboutsummaryrefslogtreecommitdiff
path: root/lib/std/priority_dequeue.zig
diff options
context:
space:
mode:
authorOminitay <37453713+Ominitay@users.noreply.github.com>2021-10-27 15:53:29 +0100
committerAndrew Kelley <andrew@ziglang.org>2021-10-27 16:07:48 -0400
commitc1a5ff34f3f68a2a0bc32828ab483328cd436fea (patch)
treebad85387a89da38890f72696ccfc0ae3c416ab0f /lib/std/priority_dequeue.zig
parent9024f27d8f5cb651e2260348ce0ee6fd67fc2c32 (diff)
downloadzig-c1a5ff34f3f68a2a0bc32828ab483328cd436fea.tar.gz
zig-c1a5ff34f3f68a2a0bc32828ab483328cd436fea.zip
std.rand: Refactor `Random` interface
These changes have been made to resolve issue #10037. The `Random` interface was implemented in such a way that causes significant slowdown when calling the `fill` function of the rng used. The `Random` interface is no longer stored in a field of the rng, and is instead returned by the child function `random()` of the rng. This avoids the performance issues caused by the interface.
Diffstat (limited to 'lib/std/priority_dequeue.zig')
-rw-r--r--lib/std/priority_dequeue.zig17
1 files changed, 10 insertions, 7 deletions
diff --git a/lib/std/priority_dequeue.zig b/lib/std/priority_dequeue.zig
index 59f3e15ff6..e8862ae631 100644
--- a/lib/std/priority_dequeue.zig
+++ b/lib/std/priority_dequeue.zig
@@ -850,17 +850,18 @@ test "std.PriorityDequeue: shrinkAndFree" {
test "std.PriorityDequeue: fuzz testing min" {
var prng = std.rand.DefaultPrng.init(0x12345678);
+ const random = prng.random();
const test_case_count = 100;
const queue_size = 1_000;
var i: usize = 0;
while (i < test_case_count) : (i += 1) {
- try fuzzTestMin(&prng.random, queue_size);
+ try fuzzTestMin(random, queue_size);
}
}
-fn fuzzTestMin(rng: *std.rand.Random, comptime queue_size: usize) !void {
+fn fuzzTestMin(rng: std.rand.Random, comptime queue_size: usize) !void {
const allocator = testing.allocator;
const items = try generateRandomSlice(allocator, rng, queue_size);
@@ -878,17 +879,18 @@ fn fuzzTestMin(rng: *std.rand.Random, comptime queue_size: usize) !void {
test "std.PriorityDequeue: fuzz testing max" {
var prng = std.rand.DefaultPrng.init(0x87654321);
+ const random = prng.random();
const test_case_count = 100;
const queue_size = 1_000;
var i: usize = 0;
while (i < test_case_count) : (i += 1) {
- try fuzzTestMax(&prng.random, queue_size);
+ try fuzzTestMax(random, queue_size);
}
}
-fn fuzzTestMax(rng: *std.rand.Random, queue_size: usize) !void {
+fn fuzzTestMax(rng: std.rand.Random, queue_size: usize) !void {
const allocator = testing.allocator;
const items = try generateRandomSlice(allocator, rng, queue_size);
@@ -906,17 +908,18 @@ fn fuzzTestMax(rng: *std.rand.Random, queue_size: usize) !void {
test "std.PriorityDequeue: fuzz testing min and max" {
var prng = std.rand.DefaultPrng.init(0x87654321);
+ const random = prng.random();
const test_case_count = 100;
const queue_size = 1_000;
var i: usize = 0;
while (i < test_case_count) : (i += 1) {
- try fuzzTestMinMax(&prng.random, queue_size);
+ try fuzzTestMinMax(random, queue_size);
}
}
-fn fuzzTestMinMax(rng: *std.rand.Random, queue_size: usize) !void {
+fn fuzzTestMinMax(rng: std.rand.Random, queue_size: usize) !void {
const allocator = testing.allocator;
const items = try generateRandomSlice(allocator, rng, queue_size);
@@ -943,7 +946,7 @@ fn fuzzTestMinMax(rng: *std.rand.Random, queue_size: usize) !void {
}
}
-fn generateRandomSlice(allocator: *std.mem.Allocator, rng: *std.rand.Random, size: usize) ![]u32 {
+fn generateRandomSlice(allocator: *std.mem.Allocator, rng: std.rand.Random, size: usize) ![]u32 {
var array = std.ArrayList(u32).init(allocator);
try array.ensureTotalCapacity(size);