diff --git a/src/textures.c b/src/textures.c index 9042b35..bd766e3 100644 --- a/src/textures.c +++ b/src/textures.c @@ -361,7 +361,39 @@ void textures_update_atlas(struct texture_cache *cache) { arrfree(rects); } +/* EXPERIMANTAL: LIKELY TO BE REMOVED! */ +/* todo: If it's proven to be useful: add runtime checking for .rodata > .data */ +#ifdef __unix__ /* use rodata elf section for fast lookups of repeating textures */ +extern const char start_rodata_address[]; +extern const char stop_rodata_heuristic[]; + +asm(".set start_rodata_address, .rodata"); +asm(".set stop_rodata_heuristic, .data"); /* there's nothing in default linker script to know the size of .rodata */ + +t_texture_key textures_get_key(struct texture_cache *cache, const char *path) { + static const char *last_path = NULL; + static t_texture_key last_texture; + + /* fast path */ + if (path == last_path && path >= start_rodata_address && path < stop_rodata_heuristic) + return last_texture; + + /* hash tables are assumed to be stable, so we just return indices */ + int texture = (int)shgeti(cache->hash, path); + + /* load it if it isn't */ + if (texture == -1) { + last_texture = textures_load(cache, path); + } else + last_texture = (t_texture_key){ texture }; + + last_path = path; + + return last_texture; +} + +#else t_texture_key textures_get_key(struct texture_cache *cache, const char *path) { /* hash tables are assumed to be stable, so we just return indices */ ptrdiff_t texture = shgeti(cache->hash, path);