Don't expand-file-name, just error

Don't expand-file-name, just error

An insight from learning to write fast #elisp: "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 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: the other use of expand-file-name is faster with file-name-concat instead.

Alternatively, this is also good:

(let (file-name-handler-alist)
  (expand-file-name PATH))
Created (4 months ago)