
Ben Gamari pushed to branch wip/T26009 at Glasgow Haskell Compiler / GHC Commits: 7973f903 by Ben Gamari at 2025-05-19T15:34:19-04:00 rts/linker/PEi386: Don't repeated load DLLs Previously every DLL-imported symbol would result in a call to `LoadLibraryEx`. This ended up constituting over 40% of the runtime of `ghc --interactive -e 42` on Windows. Avoid this by maintaining a hash-set of loaded DLL names, skipping the call if we have already loaded the requested DLL. Addresses #26009. - - - - - af7b73da by Ben Gamari at 2025-05-19T15:39:26-04:00 fixup! rts/Hash: Factor out hashBuffer - - - - - 3 changed files: - rts/Hash.c - rts/Hash.h - rts/linker/PEi386.c Changes: ===================================== rts/Hash.c ===================================== @@ -94,9 +94,9 @@ hashWord(const HashTable *table, StgWord key) } int -hashBuffer(const void *buf, size_t len) +hashBuffer(const HashTable *table, const void *buf, size_t len) { - const char *key = (char*) w; + const char *key = (char*) buf; #if WORD_SIZE_IN_BITS == 64 StgWord h = XXH3_64bits_withSeed (key, strlen(key), 1048583); #else @@ -117,7 +117,7 @@ hashBuffer(const void *buf, size_t len) int hashStr(const HashTable *table, StgWord w) { - return hashBuffer(key, strlen(key)); + return hashBuffer(table, key, strlen(key)); } STATIC_INLINE int ===================================== rts/Hash.h ===================================== @@ -71,7 +71,7 @@ typedef int HashFunction(const HashTable *table, StgWord key); typedef int CompareFunction(StgWord key1, StgWord key2); // Helper for implementing hash functions -int hashBuffer(const void *buf, size_t len); +int hashBuffer(const HashTable *table, const void *buf, size_t len); int hashWord(const HashTable *table, StgWord key); int hashStr(const HashTable *table, StgWord w); ===================================== rts/linker/PEi386.c ===================================== @@ -427,8 +427,53 @@ const int default_alignment = 8; the pointer as a redirect. Essentially it's a DATA DLL reference. */ const void* __rts_iob_func = (void*)&__acrt_iob_func; +/* + * Note [Avoiding repeated DLL loading] + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * As LoadLibraryEx tends to be expensive and addDLL_PEi386 is called on every + * DLL-imported symbol, we use a hash-set to keep track of which DLLs have + * already been loaded. This hash-set is keyed on the dll_name passed to + * addDLL_PEi386 and serves as a quick check to avoid repeated calls to + * LoadLibraryEx for the identical DLL. See #26009. + */ + +typedef struct { + HashTable *hash; +} LoadedDllSet; + +LoadedDllSet loaded_dll_set; + +void initLoadedDllSet(LoadedDllSet *set) { + set->hash = allocHashTable(); +} + +int hash_path(const HashTable *table, StgWord key) +{ + const pathchar *key = (pathchar*) w; + return hashBuffer(table, key, sizeof(pathchar) * wcslen(key)); +} + +int compare_path(StgWord key1, StgWord key2) +{ + return wscmp((pathchar*) key1, (pathchar*) key2); +} + +void addLoadedDll(LoadedDllSet *set, pathchar *dll_name) +{ + insertHashTable_(set->hash, (StgWord) dll_name, (void*) 1, hash_path); +} + +bool isDllLoaded(LoadedDllSet *set, pathchar *dll_name) +{ + void * result = lookupHashTable_(set->hash, (StgWord) dll_name, hash_path, compare_path); + return result != NULL; +} + + void initLinker_PEi386(void) { + initLoadedDllSet(&loaded_dll_set); + if (!ghciInsertSymbolTable(WSTR("(GHCi/Ld special symbols)"), symhash, "__image_base__", GetModuleHandleW (NULL), HS_BOOL_TRUE, @@ -802,6 +847,10 @@ addDLL_PEi386( pathchar *dll_name, HINSTANCE *loaded ) /* ------------------- Win32 DLL loader ------------------- */ IF_DEBUG(linker, debugBelch("addDLL; dll_name = `%" PATH_FMT "'\n", dll_name)); + if (isDllLoaded(loaded_dll_set, dll_name)) { + return NULL; + } + /* The file name has no suffix (yet) so that we can try both foo.dll and foo.drv @@ -840,6 +889,7 @@ addDLL_PEi386( pathchar *dll_name, HINSTANCE *loaded ) goto error; } + addLoadedDll(&loaded_dll_set, dll_name); addDLLHandle(buf, instance); if (loaded) { *loaded = instance; View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/295ed26f4989dbe47b34c1257016c81... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/295ed26f4989dbe47b34c1257016c81... You're receiving this email because of your account on gitlab.haskell.org.