[Freeciv-Dev] (PR#11886) faster city_map_iterate
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
<URL: http://bugs.freeciv.org/Ticket/Display.html?id=11886 >
This patch makes city_map_iterate much faster. It is changed to be a
wrapper for city_map_iterate_outwards. This means that simple pointer
lookups take the place of a function call and arithmetic in
is_valid_city_coords.
This speeds up the CM run on Boston in the 10203 savegame from 2210 ms
to 2100 ms. Of course it should also have an affect anywhere else
city_map_iterate is used (but generic_city_refresh is surely the biggest
user).
The only tricky part is that city_map_iterate may no longer be used in
generating the city map indices, for obvious reasons. This means a
small change to city.c with a comment about it.
-jason
? diff
? gmon.out
Index: common/city.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/city.c,v
retrieving revision 1.303
diff -u -r1.303 city.c
--- common/city.c 8 Jan 2005 09:44:39 -0000 1.303
+++ common/city.c 10 Jan 2005 19:41:45 -0000
@@ -172,10 +172,17 @@
int i = 0, dx, dy;
struct iter_index *array = city_map_iterate_outwards_indices;
+ /* We don't use city-map iterators in this function because they may
+ * rely on the indices that have not yet been generated. */
+
city_tiles = 0;
- city_map_iterate(city_x, city_y) {
- city_tiles++;
- } city_map_iterate_end;
+ for (dx = -CITY_MAP_RADIUS; dx <= CITY_MAP_RADIUS; dx++) {
+ for (dy = -CITY_MAP_RADIUS; dy <= CITY_MAP_RADIUS; dy++) {
+ if (is_valid_city_coords(dx + CITY_MAP_RADIUS, dy + CITY_MAP_RADIUS)) {
+ city_tiles++;
+ }
+ }
+ }
/* Realloc is used because this function may be called multiple times. */
array = fc_realloc(array, CITY_TILES * sizeof(*array));
Index: common/city.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/city.h,v
retrieving revision 1.193
diff -u -r1.193 city.h
--- common/city.h 8 Jan 2005 05:14:17 -0000 1.193
+++ common/city.h 10 Jan 2005 19:41:45 -0000
@@ -80,19 +80,8 @@
* city map (i.e., positions that are workable by the city) in unspecified
* order.
*/
-#define city_map_iterate(x, y) \
-{ \
- int _itr; \
- \
- for (_itr = 0; _itr < CITY_MAP_SIZE * CITY_MAP_SIZE; _itr++) { \
- const int x = _itr % CITY_MAP_SIZE, y = _itr / CITY_MAP_SIZE; \
- \
- if (is_valid_city_coords(x, y)) {
-
-#define city_map_iterate_end \
- } \
- } \
-}
+#define city_map_iterate(x, y) city_map_iterate_outwards(x, y)
+#define city_map_iterate_end city_map_iterate_outwards_end
/* Cost in luxuries to make one citizen happier by one level. */
#define HAPPY_COST 2
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Freeciv-Dev] (PR#11886) faster city_map_iterate,
Jason Short <=
|
|