# HG changeset patch # User Kim Alvefur # Date 1512309805 -3600 # Node ID 200f4f1b7833aea8593d0b88aef616dc06541064 # Parent a72898dde1a0e79e16cbffc1954f137c0c16705f util.crand: Use a small buffer on the stack for small pieces of random, should be faster diff -r a72898dde1a0 -r 200f4f1b7833 util-src/crand.c --- a/util-src/crand.c Sun Dec 03 14:14:35 2017 +0100 +++ b/util-src/crand.c Sun Dec 03 15:03:25 2017 +0100 @@ -58,9 +58,19 @@ #error util.crand compiled without a random source #endif +#ifndef SMALLBUFSIZ +#define SMALLBUFSIZ 32 +#endif + int Lrandom(lua_State *L) { + char smallbuf[SMALLBUFSIZ]; + char *buf = &smallbuf[0]; const size_t len = luaL_checkinteger(L, 1); - void *buf = lua_newuserdata(L, len); + + if(len > SMALLBUFSIZ) { + buf = lua_newuserdata(L, len); + } + #if defined(WITH_GETRANDOM) /*