Browse Source

speed up path_to_fullpath on Linux/MacOS

We did some profiling for #3343 and this seems to be the biggest
problem. `realpath` is expensive, and we are locking here for no reason
that I can think of.

This improves the "check procedure bodies" timing (of the linked issue)
from 2.4s to .4s on my machine.
Laytan Laats 1 year ago
parent
commit
63f30a8207
1 changed files with 13 additions and 4 deletions
  1. 13 4
      src/build_settings.cpp

+ 13 - 4
src/build_settings.cpp

@@ -1190,11 +1190,16 @@ gb_internal String path_to_fullpath(gbAllocator a, String s, bool *ok_) {
 }
 #elif defined(GB_SYSTEM_OSX) || defined(GB_SYSTEM_UNIX)
 gb_internal String path_to_fullpath(gbAllocator a, String s, bool *ok_) {
+	static gb_thread_local StringMap<String> cache;
+
+	String* cached = string_map_get(&cache, s);
+	if (cached != nullptr) {
+		return copy_string(a, *cached);
+	}
+
 	char *p;
-	mutex_lock(&fullpath_mutex);
 	p = realpath(cast(char *)s.text, 0);
 	defer (free(p));
-	mutex_unlock(&fullpath_mutex);
 	if(p == nullptr) {
 		if (ok_) *ok_ = false;
 
@@ -1207,10 +1212,14 @@ gb_internal String path_to_fullpath(gbAllocator a, String s, bool *ok_) {
 		//
 		// I have opted for 2 because it is much simpler + we already return `ok = false` + further
 		// checks and processes will use the path and cause errors (which we want).
-		return copy_string(a, s);
+		String result = copy_string(a, s);
+		string_map_set(&cache, copy_string(permanent_allocator(), s), copy_string(permanent_allocator(), result));
+		return result;
 	}
 	if (ok_) *ok_ = true;
-	return copy_string(a, make_string_c(p));
+	String result = copy_string(a, make_string_c(p));
+	string_map_set(&cache, copy_string(permanent_allocator(), s), copy_string(permanent_allocator(), result));
+	return result;
 }
 #else
 #error Implement system