util.crand: Use a small buffer on the stack for small pieces of random, should be faster
authorKim Alvefur <zash@zash.se>
Sun, 03 Dec 2017 15:03:25 +0100
changeset 8450 200f4f1b7833
parent 8449 a72898dde1a0
child 8451 f516a52f19e8
util.crand: Use a small buffer on the stack for small pieces of random, should be faster
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)
 	/*