guicho271828 / sbcl-wiki

maybe-wrong sbcl internals
55 stars 3 forks source link

A note on 'Avoid using funcall and apply' under Optimization techniques #2

Open digikar99 opened 4 years ago

digikar99 commented 4 years ago

Verify if you could:


Avoid using funcall and apply (for non-builtin) functions while declaiming inline. This is clear by disassemble-ing bar and baz below.

(declaim (inline foo bar baz))

(defun foo (a b)
  (declare (optimize (speed 3))
           (type fixnum a b))
  (+ a b))

(defun bar (a b)
  (declare (optimize (speed 3))
           (type fixnum a b))
  (funcall 'foo a b)) ; explicitly calling foo - inline-ing is semantically incorrect; not inline-d

(defun baz (a b)
  (declare (optimize (speed 3))
           (type fixnum a b))
  (foo a b)) ; foo is inline-d

Though, inline works when it's (funcall '+ ...) - what makes a function "built-in"? I guess deftransform - but not sure.

guicho271828 commented 4 years ago

see deftransform for funcall

digikar99 commented 4 years ago

Is this any preferable than define-compiler-macro? (Couldn't locate deftransform for funcall; but reading and learning about deftransform and related. Had taken a break from lisp.)

digikar99 commented 4 years ago

A brief testing suggests that define-compiler-macro is good for portable code, but one'd need to take care of cases individually, while deftransform handles the cases. deftransform seems convenient but comes at a cost of non-portability.

digikar99 commented 4 years ago

Also, this is not always possible with apply due to the last argument being a list, is it?