Package: command-not-found
Version: 20.10.1-1
Severity: normal
Tags: patch

Dear Maintainer,

cnf-update-db is fairly slow on my machine. After I remove
/var/lib/command-not-found/commands.db.metadata, cnf-update-db takes about 16
seconds on my machine.

A large chunk of this time is checking whether files are in /usr/bin and
similar directories. I'm attaching a small patch that optimizes this check,
taking the same update down to about 10 seconds on my machine. It's a 4-line
change that seems like an easy win.

I confirmed that I have the same number of commands and packages in the
commands.db database with and without this change.

Please let me know if you prefer to receive this patch in a different format or
channel.

Regards,
Diego

-- System Information:
Debian Release: 10.7
  APT prefers stable-updates
  APT policy: (560, 'stable-updates'), (550, 'stable'), (160, 'testing'), (150, 
'unstable')
Architecture: amd64 (x86_64)
Foreign Architectures: i386

Kernel: Linux 5.9.0-0.bpo.2-amd64 (SMP w/16 CPU cores)
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8), 
LANGUAGE=en_US.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /usr/bin/dash
Init: systemd (via /run/systemd/system)
LSM: AppArmor: enabled

Versions of packages command-not-found depends on:
ii  apt-file     3.2.2
ii  lsb-release  10.2019051400
ii  python3      3.7.3-1
ii  python3-apt  1.8.4.3

command-not-found recommends no packages.

Versions of packages command-not-found suggests:
pn  snapd  <none>
From: Diego Ongaro <ongar...@gmail.com>
Date: Wed, 13 Jan 2021 17:44:50 -0800
Subject: cnf-update-db: Speed up Contents files

---
 CommandNotFound/db/creator.py | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/CommandNotFound/db/creator.py b/CommandNotFound/db/creator.py
index 08564f5..fce5e67 100755
--- a/CommandNotFound/db/creator.py
+++ b/CommandNotFound/db/creator.py
@@ -4,6 +4,7 @@ import errno
 import json
 import logging
 import os
+import re
 import sqlite3
 import subprocess
 import sys
@@ -234,12 +235,12 @@ class DbCreator:
     def _parse_single_contents_file(self, con, f, fp):
         # read header
         suite=None      # FIXME
+        pattern = re.compile(b'usr/sbin|usr/bin|sbin|bin')
 
         for l in fp:
-            l = l.decode("utf-8")
-            if not (l.startswith('usr/sbin') or l.startswith('usr/bin') or
-                    l.startswith('bin') or l.startswith('sbin')):
+            if not pattern.match(l):
                 continue
+            l = l.decode("utf-8")
             try:
                 command, pkgnames = l.split(None, 1)
             except ValueError:

Reply via email to