20613b1aeff2c61eb73a78d741db49f543c92114
max
  Mon Oct 2 07:52:20 2023 -0700
avoiding fstat calls in hReplaceGbdb, refs #32299

diff --git src/hg/lib/hdb.c src/hg/lib/hdb.c
index deafa1a..3a0f5c0 100644
--- src/hg/lib/hdb.c
+++ src/hg/lib/hdb.c
@@ -1446,42 +1446,42 @@
 
 char *hReplaceGbdb(char* fileName)
  /* Returns a gbdb filename, potentially rewriting it according to hg.conf
   * If the settings gbdbLoc1 and gbdbLoc2 are found, try them in order, by
   * replacing /gbdb/ with the new locations.
   * Typically, gbdbLoc1 is /gbdb/ and gbdbLoc2 is http://hgdownload.soe.ucsc.edu/gbdb/
   * If after the replacement of gbdbLoc1 the resulting fileName does not exist,
   * gbdbLoc2 is used.
   * This function does not guarantee that the returned filename exists.
   * We assume /gbdb/ does not appear somewhere inside a fileName.
   * Result has to be free'd.
  * */
 {
 if (fileName == NULL)
     return fileName;
-if (!startsWith("/gbdb/", fileName))
+
+// if the gbdbLoc2 system is not used at all, like on the RR, stop now. 
+// This is important, as we would be doing tens of thousands of stats
+// otherwise on the RR when going over trackDb.
+char* newGbdbLoc = cfgOption("gbdbLoc2");
+if (newGbdbLoc == NULL || !startsWith("/gbdb/", fileName))
     return cloneString(fileName);
 
 char *path = hReplaceGbdbLocal(fileName);
 if (fileExists(path))
     return path;
 
-// if the file did not exist, replace with gbdbLoc2
-char* newGbdbLoc = cfgOption("gbdbLoc2");
-if (newGbdbLoc==NULL)
-    return path;
-
 freeMem(path);
 path = replaceChars(fileName, "/gbdb/", newGbdbLoc);
 if (cfgOptionBooleanDefault("traceGbdb", FALSE))
     fprintf(stderr, "REDIRECT gbdbLoc2 %s ", path);
 
 return path;
 }
 
 char *hReplaceGbdbSeqDir(char *path, char *db)
 /* similar to hReplaceGbdb, but accepts a nib or 2bit "directory" (basename) under
  * gbdb, like /gbdb/hg19 (which by jkLib is translated to /gbdb/hg19/hg19.2bit).
  hReplaceGbdb would check only if the dir exists. For 2bit basename, we
  have to check if the 2bit file exists, do the rewriting, then strip off the
  2bit filename again.
  This function works with .nib directories, but nib does not support opening