making built-in functions (as length) inline

Discussion of Common Lisp
Post Reply
imba
Posts: 35
Joined: Sun Nov 21, 2010 2:13 pm

making built-in functions (as length) inline

Post by imba » Sat Nov 27, 2010 10:09 am

Hi,

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

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

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

Post by ramarren » Sat Nov 27, 2010 11:22 am

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

imba
Posts: 35
Joined: Sun Nov 21, 2010 2:13 pm

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

Post by imba » Sat Nov 27, 2010 11:31 am

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

findinglisp
Posts: 447
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX
Contact:

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

Post by findinglisp » Mon Nov 29, 2010 9:29 pm

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/

Post Reply