From 454070587645bf840be610ec31f9783b5d0dbce0 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sat, 16 Jul 2016 02:09:49 +0300 Subject: Improve a bit performance in bl_getall_area. --- src/map/map.c | 100 ++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 73 insertions(+), 27 deletions(-) (limited to 'src/map') diff --git a/src/map/map.c b/src/map/map.c index c5ea7c1f3..d6425b94e 100644 --- a/src/map/map.c +++ b/src/map/map.c @@ -683,49 +683,95 @@ static int bl_getall_area(int type, int m, int x0, int y0, int x1, int y1, int ( x1 = min(x1, map->list[m].xs - 1); y1 = min(y1, map->list[m].ys - 1); - for (by = y0 / BLOCK_SIZE; by <= y1 / BLOCK_SIZE; by++) { - for (bx = x0 / BLOCK_SIZE; bx <= x1 / BLOCK_SIZE; bx++) { - if (type&~BL_MOB) { - for (bl = map->list[m].block[bx + by * map->list[m].bxs]; bl != NULL; bl = bl->next) { - if (bl->type&type && bl->x >= x0 && bl->x <= x1 && bl->y >= y0 && bl->y <= y1) { - if( map->bl_list_count >= map->bl_list_size ) - map_bl_list_expand(); - if (func) { - va_start(args, func); - if (func(bl, args)) { + { + const int x0b = x0 / BLOCK_SIZE; + const int x1b = x1 / BLOCK_SIZE; + const int y0b = y0 / BLOCK_SIZE; + const int y1b = y1 / BLOCK_SIZE; + const struct map_data *const listm = &map->list[m]; + const int bxs0 = listm->bxs; + + // duplication for better performance + if (func != NULL) { + if (type & ~BL_MOB) { + for (by = y0b; by <= y1b; by++) { + const int bxs = by * bxs0; + for (bx = x0b; bx <= x1b; bx++) { + for (bl = listm->block[bx + bxs]; bl != NULL; bl = bl->next) { + const int x = bl->x; + const int y = bl->y; + if (bl->type & type && x >= x0 && x <= x1 && y >= y0 && y <= y1) { + va_start(args, func); + if (func(bl, args)) { + if (map->bl_list_count >= map->bl_list_size) + map_bl_list_expand(); + map->bl_list[map->bl_list_count++] = bl; + found++; + } + va_end(args); + } + } + } + } + } + if (type & BL_MOB) { + for (by = y0b; by <= y1b; by++) { + const int bxs = by * bxs0; + for (bx = x0b; bx <= x1b; bx++) { + for (bl = listm->block_mob[bx + bxs]; bl != NULL; bl = bl->next) { + const int x = bl->x; + const int y = bl->y; + if (x >= x0 && x <= x1 && y >= y0 && y <= y1) { + va_start(args, func); + if (func(bl, args)) { + if (map->bl_list_count >= map->bl_list_size) + map_bl_list_expand(); + map->bl_list[map->bl_list_count++] = bl; + found++; + } + va_end(args); + } + } + } + } + } + } else { // func != NULL + if (type & ~BL_MOB) { + for (by = y0b; by <= y1b; by++) { + const int bxs = by * bxs0; + for (bx = x0b; bx <= x1b; bx++) { + for (bl = listm->block[bx + bxs]; bl != NULL; bl = bl->next) { + const int x = bl->x; + const int y = bl->y; + if (bl->type & type && x >= x0 && x <= x1 && y >= y0 && y <= y1) { + if (map->bl_list_count >= map->bl_list_size) + map_bl_list_expand(); map->bl_list[map->bl_list_count++] = bl; found++; } - va_end(args); - } else { - map->bl_list[map->bl_list_count++] = bl; - found++; } } } } - if (type&BL_MOB) { // TODO: fix this code duplication - for (bl = map->list[m].block_mob[bx + by * map->list[m].bxs]; bl != NULL; bl = bl->next) { - if (bl->x >= x0 && bl->x <= x1 && bl->y >= y0 && bl->y <= y1) { - if( map->bl_list_count >= map->bl_list_size ) - map_bl_list_expand(); - if (func) { - va_start(args, func); - if (func(bl, args)) { + if (type & BL_MOB) { + for (by = y0b; by <= y1b; by++) { + const int bxs = by * bxs0; + for (bx = x0b; bx <= x1b; bx++) { + for (bl = listm->block_mob[bx + bxs]; bl != NULL; bl = bl->next) { + const int x = bl->x; + const int y = bl->y; + if (x >= x0 && x <= x1 && y >= y0 && y <= y1) { + if (map->bl_list_count >= map->bl_list_size) + map_bl_list_expand(); map->bl_list[map->bl_list_count++] = bl; found++; } - va_end(args); - } else { - map->bl_list[map->bl_list_count++] = bl; - found++; } } } } } } - return found; } -- cgit v1.2.3-60-g2f50