linker: Avoid linear search when looking up Haskell symbols via dlsym
See the primary Note [Looking up symbols in the relevant objects] for a
more in-depth explanation.
When dynamically loading a Haskell symbol (typical when running a splice or
GHCi expression), before this commit we would search for the symbol in
all dynamic libraries that were loaded. However, this could be very
inefficient when too many packages are loaded (which can happen if there are
many package dependencies) because the time to lookup the would be
linear in the number of packages loaded.
This commit drastically improves symbol loading performance by
introducing a mapping from units to the handles of corresponding loaded
dlls. These handles are returned by dlopen when we load a dll, and can
then be used to look up in a specific dynamic library.
Looking up a given Name is now much more precise because we can get
lookup its unit in the mapping and lookup the symbol solely in the
handles of the dynamic libraries loaded for that unit.
In one measurement, the wait time before the expression was executed
went from +-38 seconds down to +-2s.
This commit also includes Note [Symbols may not be found in pkgs_loaded],
explaining the fallback to the old behaviour in case no dll can be found
in the unit mapping for a given Name.
Fixes #23415
Co-authored-by:
Rodrigo Mesquita <(@alt-romes)>
Showing
- compiler/GHC/ByteCode/Linker.hs 48 additions, 21 deletionscompiler/GHC/ByteCode/Linker.hs
- compiler/GHC/Driver/Plugins.hs 6 additions, 6 deletionscompiler/GHC/Driver/Plugins.hs
- compiler/GHC/Linker/Loader.hs 34 additions, 26 deletionscompiler/GHC/Linker/Loader.hs
- compiler/GHC/Linker/MacOS.hs 2 additions, 2 deletionscompiler/GHC/Linker/MacOS.hs
- compiler/GHC/Linker/Types.hs 54 additions, 4 deletionscompiler/GHC/Linker/Types.hs
- compiler/GHC/Runtime/Interpreter.hs 9 additions, 6 deletionscompiler/GHC/Runtime/Interpreter.hs
- libraries/ghci/GHCi/Message.hs 9 additions, 2 deletionslibraries/ghci/GHCi/Message.hs
- libraries/ghci/GHCi/ObjLink.hs 26 additions, 12 deletionslibraries/ghci/GHCi/ObjLink.hs
- libraries/ghci/GHCi/Run.hs 3 additions, 1 deletionlibraries/ghci/GHCi/Run.hs
- rts/Linker.c 29 additions, 17 deletionsrts/Linker.c
- rts/RtsSymbols.c 1 addition, 0 deletionsrts/RtsSymbols.c
- rts/include/rts/Linker.h 3 additions, 1 deletionrts/include/rts/Linker.h
- testsuite/tests/rts/linker/T2615.hs 2 additions, 2 deletionstestsuite/tests/rts/linker/T2615.hs
Loading
Please register or sign in to comment