Browse Source

Lib: Speed up listing a lot of mapsets by 20%. (#281)

* Lib: Speed up listing a lot of mapsets by 20%.

* Lib: Minor optimisation & change documentation to match actual code

Co-authored-by: Māris Nartišs <maris.nartiss@lu.lv>
Māris Nartišs 5 years ago
parent
commit
a3791a8ca6
1 changed files with 11 additions and 6 deletions
  1. 11 6
      lib/gis/mapset_nme.c

+ 11 - 6
lib/gis/mapset_nme.c

@@ -139,10 +139,11 @@ void G_reset_mapsets(void)
 
    List is updated by each call to this function.
 
-   \return pointer to zero terminated array of available mapsets
+   \return pointer to NULL terminated array of available mapsets
  */
 char **G_get_available_mapsets(void)
 {
+    char *location;
     char **mapsets = NULL;
     int alloc = 50;
     int n = 0;
@@ -153,15 +154,18 @@ char **G_get_available_mapsets(void)
 
     mapsets = G_calloc(alloc, sizeof(char *));
 
-    dir = opendir(G_location_path());
-    if (!dir)
-	return mapsets;
+    location = G_location_path();
+    dir = opendir(location);
+    if (!dir) {
+        G_free(location);
+        return mapsets;
+    }
 
     while ((ent = readdir(dir))) {
 	char buf[GPATH_MAX];
 	struct stat st;
 
-	sprintf(buf, "%s/%s/WIND", G_location_path(), ent->d_name);
+	sprintf(buf, "%s/%s/WIND", location, ent->d_name);
 
 	if (G_stat(buf, &st) != 0) {
 	    G_debug(4, "%s is not mapset", ent->d_name);
@@ -176,10 +180,11 @@ char **G_get_available_mapsets(void)
 	}
 
 	mapsets[n++] = G_store(ent->d_name);
-	mapsets[n] = NULL;
     }
+	mapsets[n] = NULL;
 
     closedir(dir);
+    G_free(location);
 
     return mapsets;
 }