guicho271828 / trivia

Pattern Matcher Compatible with Optima
Other
335 stars 22 forks source link

Avoid type checking if the type is known #140

Closed commander-trashdin closed 2 years ago

commander-trashdin commented 2 years ago

Here's the difference:

CL-USER> (disassemble
          (lambda (x)
           (declare (type (simple-array fixnum (*)) x)
                    (values fixnum &optional)
                    (optimize (speed 3) (safety 0) (debug 0)))
           (trivia:match x
            ((vector a b)
             (+ a b)))))
; disassembly for (LAMBDA (X))
; Size: 37 bytes. Origin: #x53448E26                          ; (LAMBDA (X))
; 26:       488B4AF9         MOV RCX, [RDX-7]
; 2A:       4883F904         CMP RCX, 4
; 2E:       740E             JEQ L1
; 30:       B917010050       MOV ECX, #x50000117              ; NIL
; 35: L0:   488BD1           MOV RDX, RCX
; 38:       488BE5           MOV RSP, RBP
; 3B:       F8               CLC
; 3C:       5D               POP RBP
; 3D:       C3               RET
; 3E: L1:   488B4A01         MOV RCX, [RDX+1]
; 42:       488B5209         MOV RDX, [RDX+9]
; 46:       4801D1           ADD RCX, RDX
; 49:       EBEA             JMP L0
NIL
CL-USER> (disassemble
          (lambda (x)
           (declare (type (simple-array fixnum (*)) x)
                    (values fixnum &optional)
                    (optimize (speed 3) (safety 0) (debug 0)))
           (+ (aref x 0) (aref x 1))))
; disassembly for (LAMBDA (X))
; Size: 20 bytes. Origin: #x53448EC6                          ; (LAMBDA (X))
; C6:       488B4A01         MOV RCX, [RDX+1]
; CA:       488B5209         MOV RDX, [RDX+9]
; CE:       4801D1           ADD RCX, RDX
; D1:       488BD1           MOV RDX, RCX
; D4:       488BE5           MOV RSP, RBP
; D7:       F8               CLC
; D8:       5D               POP RBP
; D9:       C3               RET
NIL

So my question is -- is it possible to not check for types if type is already declared, at least with some safety settings maybe?

commander-trashdin commented 2 years ago

Nevermind, I got what I was doing wrong.