From ee3ebed23cc74471b0cb511ad9c569870bb9e3be Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 25 Jul 2020 20:20:37 +0300 Subject: [PATCH] rand: further speed up of rand.uuid_v4 --- vlib/rand/rand.v | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/vlib/rand/rand.v b/vlib/rand/rand.v index 8872883c73..baf5272288 100644 --- a/vlib/rand/rand.v +++ b/vlib/rand/rand.v @@ -131,12 +131,11 @@ pub fn f64_in_range(min, max f64) f64 { const ( chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' - hex_chars = '0123456789abcdef' ) pub fn string(len int) string { mut buf := malloc(len) - for i in 0..len { + for i in 0 .. len { unsafe { buf[i] = chars[intn(chars.len)] } @@ -154,9 +153,6 @@ pub fn uuid_v4() string { mut c := 0 for c < 16 && i_buf < 36 { mut d := byte(x) & 0x0F - if i_buf == 19 { - d = (d & 0x03) + 0x08 - } unsafe { buf[i_buf] = if d > 9 { d + 87 } else { d + 48 } } @@ -165,11 +161,13 @@ pub fn uuid_v4() string { x = x >> 4 } } + n := ((byte(default_rng.u64()) & 0x0F) & 0x03) + 0x08 unsafe { buf[8] = `-` buf[13] = `-` buf[14] = `4` buf[18] = `-` + buf[19] = if n > 9 { n + 87 } else { n + 48 } buf[23] = `-` buf[36] = 0 }