From c070ddac97546bf8f54ec78141e8ce374054513d Mon Sep 17 00:00:00 2001 From: Todd Gamblin Date: Mon, 23 Sep 2024 23:59:07 -0700 Subject: [PATCH] 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 --- lib/spack/spack/database.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/spack/spack/database.py b/lib/spack/spack/database.py index 404288ff83a..907b73c5db7 100644 --- a/lib/spack/spack/database.py +++ b/lib/spack/spack/database.py @@ -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: