Page 1 of 1

making built-in functions (as length) inline

Posted: Sat Nov 27, 2010 10:09 am
by imba
Hi,

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

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

Posted: Sat Nov 27, 2010 11:22 am
by ramarren
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):

Code: Select all

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

Posted: Sat Nov 27, 2010 11:31 am
by imba
OK, thanks. I'm using LispWorks (free) Windows and your versions don't make a difference.

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

Posted: Mon Nov 29, 2010 9:29 pm
by findinglisp
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.