From 1dfc89567ebb7b6509a0fd6080120da91b658df7 Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Thu, 20 Jun 2024 11:27:51 -0400 Subject: [PATCH] Optimize default RNG for the common case --- base/runtime/random_generator.odin | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/base/runtime/random_generator.odin b/base/runtime/random_generator.odin index 0a9cab860..5e0ba2540 100644 --- a/base/runtime/random_generator.odin +++ b/base/runtime/random_generator.odin @@ -86,16 +86,23 @@ default_random_generator_proc :: proc(data: rawptr, mode: Random_Generator_Mode, init(r, 0) } - pos := i8(0) - val := u64(0) - for &v in p { - if pos == 0 { - val = read_u64(r) - pos = 7 + switch len(p) { + case size_of(u64): + // Fast path for a 64-bit destination. + (transmute(^u64)raw_data(p))^ = read_u64(r) + case: + // All other cases. + pos := i8(0) + val := u64(0) + for &v in p { + if pos == 0 { + val = read_u64(r) + pos = 7 + } + v = byte(val) + val >>= 8 + pos -= 1 } - v = byte(val) - val >>= 8 - pos -= 1 } case .Reset: