Compare commits

...

2 Commits

Author SHA1 Message Date
veclavtalica
8d67e44009 /apps/twnlua: use lua_numberx for slightly more optimized defaults 2025-01-12 03:51:02 +03:00
veclavtalica
192907a0db use slot size of 128 for twnlua allocator 2025-01-12 03:21:05 +03:00
2 changed files with 16 additions and 15 deletions

View File

@ -8,9 +8,6 @@ with open(sys.argv[1], 'r') if sys.argv[1] != "-" else sys.stdin as f:
api = json.loads(api_source)
# TODO: use lua_to--x functions for defaults, so that fewer function calls are made
# TODO: use lua_createtable for preallocation
def default(parameter):
basetype = parameter["type"].rsplit(' *', 1)[0]
if parameter["type"] == "float":
@ -72,13 +69,18 @@ for procedure, procedure_desc in api["procedures"].items():
binding += " %s %s;\n" % (parameter["type"], parameter["name"])
binding += " lua_getfield(L, 1, \"%s\");\n" % parameter["name"]
if "default" in parameter:
if "default" in parameter and parameter["type"] != "float":
binding += " if (lua_isnoneornil(L, -1))\n"
binding += " %s = %s;\n" % (parameter["name"], default(parameter))
binding += " else\n "
if parameter["type"] == "float":
binding += " %s = (float)lua_tonumber(L, -1);\n" % (parameter["name"]);
if "default" in parameter:
binding += " int is_%s_num;\n" % parameter["name"]
binding += " %s = (float)lua_tonumberx(L, -1, &is_%s_num);\n" % (parameter["name"], parameter["name"]);
binding += " if (!is_%s_num) %s = %s;\n" % (parameter["name"], parameter["name"], default(parameter))
else:
binding += " %s = (float)lua_tonumber(L, -1);\n" % (parameter["name"]);
elif parameter["type"] == "bool":
binding += " %s = lua_toboolean(L, -1);\n" % (parameter["name"]);
elif parameter["type"] == "char *":

View File

@ -44,13 +44,12 @@ static int physfs_loader(lua_State *L) {
/* WARN! experimental and will probably be removed */
/* it was an attempt to ease memory usage problems posed by using lua */
/* it saved ~100MiB, but it still hogs ~500MiB for no reason on my PC */
/* it is an attempt to ease memory usage problems posed by using lua, partially successful */
static void *custom_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
(void)ud;
/* small allocations are placed in slots, as there's a big chance they will not need to be resized */
static char slots[1024][64];
static char slots[1024][128];
static int16_t free_slots[1024] = { [0] = -1 };
static size_t free_slot_count = 1024;
@ -59,26 +58,26 @@ static void *custom_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
free_slots[i] = (int16_t)i;
if (nsize == 0) {
if (ptr && (char *)ptr >= &slots[0][0] && (char *)ptr <= &slots[1024-1][64-1])
free_slots[free_slot_count++] = (int16_t)(((uintptr_t)ptr - (uintptr_t)slots) / 64);
else
if (ptr && (char *)ptr >= &slots[0][0] && (char *)ptr <= &slots[1024-1][128-1])
free_slots[free_slot_count++] = (int16_t)(((uintptr_t)ptr - (uintptr_t)slots) / 128);
else if (osize)
SDL_free(ptr);
return NULL;
} else {
if (!ptr && nsize <= 64 && free_slot_count > 0) {
if (!ptr && nsize <= 128 && free_slot_count > 0) {
/* use a slot */
return slots[free_slots[--free_slot_count]];
}
if ((char *)ptr >= &slots[0][0] && (char *)ptr <= &slots[1024-1][64-1]) {
if ((char *)ptr >= &slots[0][0] && (char *)ptr <= &slots[1024-1][128-1]) {
/* still fits */
if (nsize <= 64)
if (nsize <= 128)
return ptr;
/* move from slot to dynamic memory */
void *mem = SDL_malloc(nsize);
SDL_memcpy(mem, ptr, osize);
free_slots[free_slot_count++] = (int16_t)(((uintptr_t)ptr - (uintptr_t)slots) / 64);
free_slots[free_slot_count++] = (int16_t)(((uintptr_t)ptr - (uintptr_t)slots) / 128);
return mem;
}