database: don't call socket.getfqdn() on every write (#46554)

We've seen `getfqdn()` cause slowdowns on macOS in CI when added elsewhere. It's also
called by database.py every time we write the DB file.

- [x] replace the call with a memoized version so that it is only called once per process.

Signed-off-by: Todd Gamblin <tgamblin@llnl.gov>
This commit is contained in:
Todd Gamblin 2024-09-23 23:59:07 -07:00 committed by GitHub
parent 679770b02c
commit c070ddac97
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -50,6 +50,7 @@
pass
import llnl.util.filesystem as fs
import llnl.util.lang
import llnl.util.tty as tty
import spack.deptypes as dt
@ -121,6 +122,17 @@
)
@llnl.util.lang.memoized
def _getfqdn():
"""Memoized version of `getfqdn()`.
If we call `getfqdn()` too many times, DNS can be very slow. We only need to call it
one time per process, so we cache it here.
"""
return socket.getfqdn()
def reader(version: vn.StandardVersion) -> Type["spack.spec.SpecfileReaderBase"]:
reader_cls = {
vn.Version("5"): spack.spec.SpecfileV1,
@ -1084,7 +1096,7 @@ def _write(self, type, value, traceback):
self._state_is_inconsistent = True
return
temp_file = self._index_path + (".%s.%s.temp" % (socket.getfqdn(), os.getpid()))
temp_file = self._index_path + (".%s.%s.temp" % (_getfqdn(), os.getpid()))
# Write a temporary database file them move it into place
try: