struct: don't generate excessive struct-field-index code

The expansion of `struct` created far too much code to parameterize
`struct-field-index`, making expansion of a `struct` form with just
100 or 200 fields take a noticeably long time to expand.
This commit is contained in:
Matthew Flatt 2020-03-07 09:27:59 -07:00
parent 00b6803e36
commit 0de88f203d

View File

@ -101,6 +101,19 @@
(lambda (stx)
(raise-syntax-error #f "allowed only within a structure type definition" stx)))
(define-for-syntax (make-struct-field-index fields)
(lambda (stx)
(syntax-case stx ()
[(_ id)
(identifier? #'id)
(let loop ([pos 0] [fields (syntax->list fields)])
(cond
[(null? fields)
(raise-syntax-error #f "no such field" stx #'name)]
[(free-identifier=? #'id (car fields))
(datum->syntax #'here pos stx)]
[else (loop (add1 pos) (cdr fields))]))])))
(define (check-struct-type name what)
(when what
(unless (struct-type? what)
@ -617,14 +630,7 @@
(define-values (#,struct: #,make- #,? #,@sels #,@sets)
(let-values ([(struct: make- ? -ref -set!)
(syntax-parameterize ([struct-field-index
(lambda (stx)
(syntax-case stx #,(map field-id fields)
#,@(let loop ([fields fields][pos 0])
(cond
[(null? fields) null]
[else (cons #`[(_ #,(field-id (car fields))) #'#,pos]
(loop (cdr fields) (add1 pos)))]))
[(_ name) (raise-syntax-error #f "no such field" stx #'name)]))])
(make-struct-field-index (quote-syntax #,(map field-id fields)))])
(make-struct-type #,reflect-name-expr
#,super-struct:
#,(- (length fields) auto-count)