Skip to content

Commit 56b95a0

Browse files
Copilotjackfirth
andcommitted
Address code review feedback: improve error handling and performance
Co-authored-by: jackfirth <[email protected]>
1 parent caf3108 commit 56b95a0

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

default-recommendations/analyzers/identifier-usage.rkt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
[identifier-usage-analyzer expansion-analyzer?]))
1010

1111

12-
(require racket/list
12+
(require racket/hash
13+
racket/list
1314
racket/stream
1415
rebellion/collection/entry
1516
rebellion/streaming/transducer
@@ -159,21 +160,24 @@
159160
;; Create expanded-id-table to track bound identifiers with empty usage lists
160161
(define table (make-expanded-id-table))
161162

163+
;; Group bound identifiers by phase for efficient lookup
164+
(define bound-by-phase (make-hash))
165+
162166
;; Initialize all bound identifiers with empty usage lists
163167
(for ([id (in-stream (binding-site-identifiers labeled-stx))])
164168
(define id-phase (syntax-property id 'phase))
165-
(expanded-id-table-set! table (expanded-identifier id id-phase) '()))
169+
(define expanded-id (expanded-identifier id id-phase))
170+
(expanded-id-table-set! table expanded-id '())
171+
(hash-update! bound-by-phase id-phase (λ (lst) (cons expanded-id lst)) '()))
166172

167-
;; For each usage, find its binding and add it to the usage list
173+
;; For each usage, find its binding within the same phase and add it to the usage list
168174
(for ([used-id (in-stream (usage-site-identifiers labeled-stx))])
169175
(define used-phase (syntax-property used-id 'phase))
170-
(for ([bound-entry (in-expanded-id-table table)])
171-
(define bound-expanded-id (entry-key bound-entry))
176+
(define bound-at-phase (hash-ref bound-by-phase used-phase '()))
177+
(for ([bound-expanded-id bound-at-phase])
172178
(define bound-id (expanded-identifier-syntax bound-expanded-id))
173-
(define bound-phase (expanded-identifier-phase bound-expanded-id))
174-
(when (and (equal? bound-phase used-phase)
175-
(free-identifier=? bound-id used-id))
176-
(define current-usages (entry-value bound-entry))
179+
(when (free-identifier=? bound-id used-id)
180+
(define current-usages (expanded-id-table-ref table bound-expanded-id '()))
177181
(expanded-id-table-set! table bound-expanded-id (cons used-id current-usages)))))
178182

179183
table)

private/expanded-id-table.rkt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
(contract-out
1010
[expanded-id-table? (-> any/c boolean?)]
1111
[make-expanded-id-table (-> expanded-id-table?)]
12-
[expanded-id-table-ref (-> expanded-id-table? expanded-identifier? any/c)]
12+
[expanded-id-table-ref
13+
(->* (expanded-id-table? expanded-identifier?) (failure-result/c) any/c)]
1314
[expanded-id-table-set! (-> expanded-id-table? expanded-identifier? any/c void?)]
1415
[in-expanded-id-table
1516
(-> expanded-id-table? (sequence/c (entry/c expanded-identifier? any/c)))]))
@@ -21,6 +22,7 @@
2122
racket/match
2223
racket/sequence
2324
racket/stream
25+
rebellion/base/result
2426
rebellion/collection/entry
2527
syntax/id-table)
2628

@@ -47,9 +49,15 @@
4749
(expanded-id-table (make-hasheq)))
4850

4951

50-
(define (expanded-id-table-ref table id)
51-
(define phase-table (hash-ref (expanded-id-table-table table) (expanded-identifier-phase id)))
52-
(free-id-table-ref phase-table (expanded-identifier-syntax id)))
52+
(define (expanded-id-table-ref table id [failure-result (λ () (error 'expanded-id-table-ref "no mapping for ~a" id))])
53+
(define phase (expanded-identifier-phase id))
54+
(define stx (expanded-identifier-syntax id))
55+
(define phase-table (hash-ref (expanded-id-table-table table) phase #false))
56+
(if phase-table
57+
(free-id-table-ref phase-table stx failure-result)
58+
(if (procedure? failure-result)
59+
(failure-result)
60+
failure-result)))
5361

5462

5563
(define (expanded-id-table-set! table id value)

0 commit comments

Comments
 (0)