Subject: bug#42998: 27.1; gnutls-symmetric-encrypt slow when providing cipher as symbol

From: Vasilij Schneidermann <[email-masked]>
Date: Sun, 23 Aug 2020 18:43:30 +0200

I've recently debugged bad performance for a piece of code showing
abysmal performance in its encryption loop.  Compare the following
benchmark results:

(benchmark 10000
  '(gnutls-symmetric-encrypt 'AES-128-CBC (make-string 16 0)
                                          (make-string 16 0)
                                          (make-string 16 0)))
;; => "Elapsed time: 1.808647s (1.708427s in 124 GCs)"
(benchmark 10000
  '(gnutls-symmetric-encrypt 4 (make-string 16 0)
                               (make-string 16 0)
                               (make-string 16 0)))
;; => "Elapsed time: 0.103690s (0.089148s in 6 GCs)"

This seems to be the fault of how the cipher value is mapped to the
cipher, if it's a symbol, the C code performs the equivalent of `(assoc
cipher (gnutls-ciphers))`.  Generating the cipher list over and over
again seems like a waste of processing power.  I'd consider either to
document this behavior or making the mapping more efficient, for example
by caching the cipher list instead of recomputing it over and over
again.  The latter would match the behavior as seen in other
cryptographic library wrappers, for example the OpenSSL one in Ruby.

In GNU Emacs 27.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.22)
 of 2020-08-13 built on juergen
Windowing system distributor 'The X.Org Foundation', version 11.0.12008000
System Description: Arch Linux

Recent messages:
For information about GNU Emacs and the GNU system, type C-h C-a.

Configured using:
 'configure --prefix=/usr --sysconfdir=/etc --libexecdir=/usr/lib
 --localstatedir=/var --with-x-toolkit=gtk3 --with-xft --with-wide-int
 --with-modules 'CFLAGS=-march=x86-64 -mtune=generic -O2 -pipe -fno-plt'
 CPPFLAGS=-D_FORTIFY_SOURCE=2
 LDFLAGS=-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now'

Configured features:
XPM JPEG TIFF GIF PNG RSVG SOUND GPM DBUS GSETTINGS GLIB NOTIFY INOTIFY
ACL GNUTLS LIBXML2 FREETYPE HARFBUZZ M17N_FLT LIBOTF XFT ZLIB
TOOLKIT_SCROLL_BARS GTK3 X11 XDBE XIM MODULES THREADS LIBSYSTEMD JSON
PDUMPER LCMS2 GMP

Important settings:
  value of $LC_MESSAGES: 
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message rmc puny dired dired-loaddefs
format-spec rfc822 mml easymenu mml-sec password-cache epa derived epg
epg-config gnus-util rmail rmail-loaddefs text-property-search time-date
subr-x seq byte-opt gv bytecomp byte-compile cconv mm-decode mm-bodies
mm-encode mail-parse rfc2231 mailabbrev gmm-utils mailheader cl-loaddefs
cl-lib sendmail rfc2047 rfc2045 ietf-drums mm-util mail-prsvr mail-utils
tooltip eldoc electric uniquify ediff-hook vc-hooks lisp-float-type
mwheel term/x-win x-win term/common-win x-dnd tool-bar dnd fontset image
regexp-opt fringe tabulated-list replace newcomment text-mode elisp-mode
lisp-mode prog-mode register page tab-bar menu-bar rfn-eshadow isearch
timer select scroll-bar mouse jit-lock font-lock syntax facemenu
font-core term/tty-colors frame minibuffer cl-generic cham georgian
utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao korean
japanese eucjp-ms cp51932 hebrew greek romanian slovak czech european
ethiopic indian cyrillic chinese composite charscript charprop
case-table epa-hook jka-cmpr-hook help simple abbrev obarray
cl-preloaded nadvice loaddefs button faces cus-face macroexp files
text-properties overlay sha1 md5 base64 format env code-pages mule
custom widget hashtable-print-readable backquote threads dbusbind
inotify lcms2 dynamic-setting system-font-setting font-render-setting
move-toolbar gtk x-toolkit x multi-tty make-network-process emacs)

Memory information:
((conses 16 45005 6273)
 (symbols 48 6004 1)
 (strings 32 15483 2032)
 (string-bytes 1 504714)
 (vectors 16 10090)
 (vector-slots 8 130067 10258)
 (floats 8 19 27)
 (intervals 56 196 0)
 (buffers 1000 11))