From 85a1c57db287432014d29caac0ea1b82c777a9ee Mon Sep 17 00:00:00 2001 From: Guido Trotter <ultrotter@google.com> Date: Fri, 11 Jun 2010 12:17:52 +0100 Subject: [PATCH] Optimize _GetJobIDsUnlocked Currently we sort the list of job queue files twice (once in utils.ListVisibleFiles with sort and then later with NiceSort). We apply the _RE_JOB_FILE regular expression twice (once in _ListJobFiles and once in _ExtractJobID). This simplifies the code a little, and a couple of functions performing basically the same job are collapsed. Signed-off-by: Guido Trotter <ultrotter@google.com> Reviewed-by: Michael Hanselmann <hansmi@google.com> --- lib/jqueue.py | 43 +++++++++++-------------------------------- 1 file changed, 11 insertions(+), 32 deletions(-) diff --git a/lib/jqueue.py b/lib/jqueue.py index 0e18f0ae7..e2b6921dd 100644 --- a/lib/jqueue.py +++ b/lib/jqueue.py @@ -901,49 +901,28 @@ class JobQueue(object): return utils.PathJoin(constants.JOB_QUEUE_ARCHIVE_DIR, cls._GetArchiveDirectory(job_id), "job-%s" % job_id) - @classmethod - def _ExtractJobID(cls, name): - """Extract the job id from a filename. - - @type name: str - @param name: the job filename - @rtype: job id or None - @return: the job id corresponding to the given filename, - or None if the filename does not represent a valid - job file - - """ - m = cls._RE_JOB_FILE.match(name) - if m: - return m.group(1) - else: - return None - - def _GetJobIDsUnlocked(self): + def _GetJobIDsUnlocked(self, sort=True): """Return all known job IDs. The method only looks at disk because it's a requirement that all jobs are present on disk (so in the _memcache we don't have any extra IDs). + @type sort: boolean + @param sort: perform sorting on the returned job ids @rtype: list @return: the list of job IDs """ - jlist = [self._ExtractJobID(name) for name in self._ListJobFiles()] - jlist = utils.NiceSort(jlist) + jlist = [] + for filename in utils.ListVisibleFiles(constants.QUEUE_DIR, sort=False): + m = self._RE_JOB_FILE.match(filename) + if m: + jlist.append(m.group(1)) + if sort: + jlist = utils.NiceSort(jlist) return jlist - def _ListJobFiles(self): - """Returns the list of current job files. - - @rtype: list - @return: the list of job file names - - """ - return [name for name in utils.ListVisibleFiles(constants.QUEUE_DIR) - if self._RE_JOB_FILE.match(name)] - def _LoadJobUnlocked(self, job_id): """Loads a job from the disk or memory. @@ -1051,7 +1030,7 @@ class JobQueue(object): raise errors.JobQueueDrainError("Job queue is drained, refusing job") # Check job queue size - size = len(self._ListJobFiles()) + size = len(self._GetJobIDsUnlocked(sort=False)) if size >= constants.JOB_QUEUE_SIZE_SOFT_LIMIT: # TODO: Autoarchive jobs. Make sure it's not done on every job # submission, though. -- GitLab