This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

making built-in functions (as length) inline

4 posts · 1162 views

Hi,

is there a possibility to make built-in functions (as length) inline? (without redefining them)

Re: making built-in functions (as length) inline

Making a function inline requires saving additional information during compilation, so it is impossible without redefining. But most implementations will either have appropriate operators already inline or treated specially by the compiler.

Compare (on SBCL):
CL-USER> (disassemble (compile nil (lambda (x) (declare (notinline length))(length x))))
; disassembly for (LAMBDA (X))
; 0BDC20E9:       8BD6             MOV EDX, ESI               ; no-arg-parsing entry point
;      0EB:       8B05B820DC0B     MOV EAX, [#xBDC20B8]       ; #<FDEFINITION object for LENGTH>
;      0F1:       B904000000       MOV ECX, 4
;      0F6:       FF7504           PUSH DWORD PTR [EBP+4]
;      0F9:       FF6005           JMP DWORD PTR [EAX+5]
;      0FC:       CC0A             BREAK 10                   ; error trap
;      0FE:       02               BYTE #X02
;      0FF:       18               BYTE #X18                  ; INVALID-ARG-COUNT-ERROR
;      100:       4F               BYTE #X4F                  ; ECX
NIL
CL-USER> (disassemble (compile nil (lambda (x) (length x))))
; disassembly for (LAMBDA (X))
; 0B17031A:       8B55FC           MOV EDX, [EBP-4]           ; no-arg-parsing entry point
;       1D:       83EC0C           SUB ESP, 12
;       20:       896C2404         MOV [ESP+4], EBP
;       24:       8D6C2404         LEA EBP, [ESP+4]
;       28:       B904000000       MOV ECX, 4
;       2D:       FF15EC071001     CALL DWORD PTR [#x11007EC]
;       33:       8BE5             MOV ESP, EBP
;       35:       F8               CLC
;       36:       5D               POP EBP
;       37:       C3               RET
;       38:       CC0A             BREAK 10                   ; error trap
;       3A:       02               BYTE #X02
;       3B:       18               BYTE #X18                  ; INVALID-ARG-COUNT-ERROR
;       3C:       4F               BYTE #X4F                  ; ECX
NIL

Re: making built-in functions (as length) inline

OK, thanks. I'm using LispWorks (free) Windows and your versions don't make a difference.

Re: making built-in functions (as length) inline

To add on to Ramarren's comments, most common, "small" functions in the standard library will be inlined (what is often termed "open coded") by the compiler of most implementations. This is not guaranteed, but most every implementation is smart enough to know that it will be a big performance hit if things like CONS, CAR, CDR, LENGTH, etc., are not inlined.
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/