]> git.lizzy.rs Git - dragonblocks_alpha.git/commitdiff
Cache last accessed MapBlock
authorElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 10 Jul 2021 12:21:43 +0000 (14:21 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 10 Jul 2021 12:21:43 +0000 (14:21 +0200)
src/map.c
src/map.h

index 46c56728390e6e364da011258c150f55710175e3..e3d66ef44eaf44183ee51b276210c7c4dbb64d91 100644 (file)
--- a/src/map.c
+++ b/src/map.c
@@ -25,6 +25,7 @@ Map *map_create()
        pthread_rwlock_init(&map->rwlck, NULL);
        map->sectors = array_create(sizeof(MapSector *));
        map->sectors.cmp = &sector_compare;
+       map->cached = NULL;
        return map;
 }
 
@@ -92,6 +93,9 @@ MapSector *map_get_sector(Map *map, v2s32 pos, bool create)
 
 MapBlock *map_get_block(Map *map, v3s32 pos, bool create)
 {
+       if (map->cached && map->cached->pos.x == pos.x && map->cached->pos.y == pos.y && map->cached->pos.z == pos.z)
+               return map->cached;
+
        MapSector *sector = map_get_sector(map, (v2s32) {pos.x, pos.z}, create);
        if (! sector)
                return NULL;
@@ -116,7 +120,7 @@ MapBlock *map_get_block(Map *map, v3s32 pos, bool create)
 
        pthread_rwlock_unlock(&sector->rwlck);
 
-       return block;
+       return map->cached = block;
 }
 
 MapBlock *map_allocate_block(v3s32 pos)
index e7045f60e20472a50d00830c0066af3c4dd7b58c..706e3d3acd8209a8a470d2445e52747864ad4a49 100644 (file)
--- a/src/map.h
+++ b/src/map.h
@@ -50,6 +50,7 @@ typedef struct
 {
        pthread_rwlock_t rwlck;
        Array sectors;
+       MapBlock *cached;
 } Map;
 
 Map *map_create();