(put 'nthcdr 'byte-optimizer #'byte-optimize-nthcdr)
(defun byte-optimize-nthcdr (form)
(if (= (safe-length form) 3)
- (if (memq (nth 1 form) '(0 1 2))
- (let ((count (nth 1 form)))
- (setq form (nth 2 form))
- (while (>= (setq count (1- count)) 0)
- (setq form (list 'cdr form)))
- form)
- form)
+ (let ((count (nth 1 form)))
+ (cond ((and (integerp count) (<= count 3))
+ (setq form (nth 2 form))
+ (while (>= (setq count (1- count)) 0)
+ (setq form (list 'cdr form)))
+ form)
+ ((not (eq (car form) 'nthcdr))
+ (cons 'nthcdr (cdr form))) ; use the nthcdr byte-op
+ (t form)))
form))
(put 'cons 'byte-optimizer #'byte-optimize-cons)