From da570eb0a423c412d44ddc0855826037232d928f Mon Sep 17 00:00:00 2001
From: Kim Alvefur <zash@zash.se>
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(-)

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