Don't expand-file-name, just error

An insight from learning to write fast #emacs-lisp: "just-in-case" code can make things slower.

Example situation: you want to ensure that a provided string is an absolute filename, so you wrap it in expand-file-name or file-truename. But these are potentially expensive. Instead, if you know it's usually going to be absolute, just assert that it is:

(unless (file-name-absolute-p PATH)
  (error "Expected absolute filename but got: %s" PATH))

… and then proceed without ever calling expand-file-name.

Bonus tip: when you need to concatenate a file and directory, use file-name-concat, it is much faster than expand-file-name.

Created (5 weeks ago)