aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLaytan Laats <laytanlaats@hotmail.com>2024-03-28 18:44:21 +0100
committerLaytan Laats <laytanlaats@hotmail.com>2024-03-28 18:44:21 +0100
commit63f30a8207cf7729f4791f3d0429a428abc0ca4a (patch)
tree533d915b41215180654579a73d2e9b40466acff4 /src
parent308e9112f23a37322878968f388a4751d3d54278 (diff)
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.
Diffstat (limited to 'src')
-rw-r--r--src/build_settings.cpp17
1 files changed, 13 insertions, 4 deletions
diff --git a/src/build_settings.cpp b/src/build_settings.cpp
index 3a9951cb2..f1a21161f 100644
--- a/src/build_settings.cpp
+++ b/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