From a2f2567e8fa4a8c9cdca1f1a909c287b2935f899 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 3 Dec 2017 15:03:25 +0100 Subject: util.crand: Use a small buffer on the stack for small pieces of random, should be faster --- util-src/crand.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'util-src') diff --git a/util-src/crand.c b/util-src/crand.c index d15fc090..ab2f4ad1 100644 --- a/util-src/crand.c +++ b/util-src/crand.c @@ -58,9 +58,19 @@ int getrandom(void *buf, size_t buflen, unsigned int flags) { #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) /* -- cgit v1.2.3