| 1 |
;;; Ikarus Scheme -- A compiler for R6RS Scheme. |
| 2 |
;;; Copyright (C) 2008,2009 Abdulaziz Ghuloum |
| 3 |
;;; |
| 4 |
;;; This program is free software: you can redistribute it and/or modify |
| 5 |
;;; it under the terms of the GNU General Public License version 3 as |
| 6 |
;;; published by the Free Software Foundation. |
| 7 |
;;; |
| 8 |
;;; This program is distributed in the hope that it will be useful, but |
| 9 |
;;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
;;; General Public License for more details. |
| 12 |
;;; |
| 13 |
;;; You should have received a copy of the GNU General Public License |
| 14 |
;;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
(library (ikarus.symbol-table) |
| 19 |
(export string->symbol initialize-symbol-table! $symbol-table-size) |
| 20 |
(import |
| 21 |
(except (ikarus) string->symbol) |
| 22 |
(except (ikarus system $symbols) $symbol-table-size)) |
| 23 |
|
| 24 |
(define-struct symbol-table (length mask vec guardian)) |
| 25 |
|
| 26 |
(define (extend-table st) |
| 27 |
(let* ([v1 (symbol-table-vec st)] |
| 28 |
[n1 (vector-length v1)] |
| 29 |
[n2 (+ n1 n1)] |
| 30 |
[mask (- n2 1)] |
| 31 |
[v2 (make-vector n2 '())]) |
| 32 |
(define (insert p) |
| 33 |
(unless (null? p) |
| 34 |
(let ([a (car p)] [rest (cdr p)]) |
| 35 |
(let ([idx (fxand (symbol-hash a) mask)]) |
| 36 |
(set-cdr! p (vector-ref v2 idx)) |
| 37 |
(vector-set! v2 idx p)) |
| 38 |
(insert rest)))) |
| 39 |
(vector-for-each insert v1) |
| 40 |
(set-symbol-table-vec! st v2) |
| 41 |
(set-symbol-table-mask! st mask))) |
| 42 |
|
| 43 |
(define intern-symbol! |
| 44 |
(case-lambda |
| 45 |
[(s idx st) |
| 46 |
(let ([v (symbol-table-vec st)]) |
| 47 |
(vector-set! v idx (weak-cons s (vector-ref v idx))) |
| 48 |
((symbol-table-guardian st) s) |
| 49 |
(let ([n (fx+ (symbol-table-length st) 1)]) |
| 50 |
(set-symbol-table-length! st n) |
| 51 |
(when (fx=? n (symbol-table-mask st)) |
| 52 |
(extend-table st))))] |
| 53 |
[(s st) |
| 54 |
(intern-symbol! s |
| 55 |
(fxand (symbol-hash s) (symbol-table-mask st)) |
| 56 |
st)])) |
| 57 |
|
| 58 |
(define (intern str idx st) |
| 59 |
(let ([s ($make-symbol str)]) |
| 60 |
($set-symbol-unique-string! s #f) |
| 61 |
(intern-symbol! s idx st) |
| 62 |
s)) |
| 63 |
|
| 64 |
(define (unintern x st) |
| 65 |
(let ([n (fx- (symbol-table-length st) 1)]) |
| 66 |
(set-symbol-table-length! st n)) |
| 67 |
(let ([idx (fxand (symbol-hash x) (symbol-table-mask st))] |
| 68 |
[v (symbol-table-vec st)]) |
| 69 |
(let ([ls (vector-ref v idx)]) |
| 70 |
(cond |
| 71 |
[(eq? (car ls) x) |
| 72 |
(vector-set! v idx (cdr ls))] |
| 73 |
[else |
| 74 |
(let f ([prev ls] [ls (cdr ls)]) |
| 75 |
(cond |
| 76 |
[(eq? (car ls) x) |
| 77 |
(set-cdr! prev (cdr ls))] |
| 78 |
[else (f ls (cdr ls))]))])))) |
| 79 |
|
| 80 |
(define (dead? sym) |
| 81 |
(and ($unbound-object? ($symbol-value sym)) |
| 82 |
(null? ($symbol-plist sym)))) |
| 83 |
|
| 84 |
(define (bleed-guardian sym st) |
| 85 |
(let ([g (symbol-table-guardian st)]) |
| 86 |
(cond |
| 87 |
[(g) => |
| 88 |
(lambda (a) |
| 89 |
(let loop ([a a]) |
| 90 |
(cond |
| 91 |
[(eq? a sym) (g a)] |
| 92 |
[(begin |
| 93 |
(if (dead? a) (unintern a st) (g a)) |
| 94 |
(g)) => loop])))])) |
| 95 |
sym) |
| 96 |
|
| 97 |
(define (chain-lookup str idx st ls) |
| 98 |
(if (null? ls) |
| 99 |
(let ([sym (intern str idx st)]) |
| 100 |
;;; doesn't need eq? check there |
| 101 |
(bleed-guardian sym st)) |
| 102 |
(let ([a (car ls)]) |
| 103 |
(if (string=? str (symbol->string a)) |
| 104 |
(bleed-guardian a st) |
| 105 |
(chain-lookup str idx st (cdr ls)))))) |
| 106 |
|
| 107 |
(define (lookup str ih st) |
| 108 |
(let ([idx (fxand ih (symbol-table-mask st))]) |
| 109 |
(let ([v (symbol-table-vec st)]) |
| 110 |
(chain-lookup str idx st (vector-ref v idx))))) |
| 111 |
|
| 112 |
|
| 113 |
(module (string->symbol initialize-symbol-table! $symbol-table-size) |
| 114 |
(define st (make-symbol-table 0 3 (make-vector 4 '()) (make-guardian))) |
| 115 |
(define ($symbol-table-size) |
| 116 |
(symbol-table-length st)) |
| 117 |
(define (string->symbol x) |
| 118 |
(if (string? x) |
| 119 |
(lookup x (string-hash x) st) |
| 120 |
(die 'string->symbol "not a string" x))) |
| 121 |
(define (initialize-symbol-table!) |
| 122 |
(define (f x) |
| 123 |
(when (pair? x) |
| 124 |
(intern-symbol! (car x) st) |
| 125 |
(f (cdr x)))) |
| 126 |
(vector-for-each f (foreign-call "ikrt_get_symbol_table"))))) |
| 127 |
|
| 128 |
#!eof |
| 129 |
|
| 130 |
(define (gen-list i n) |
| 131 |
(let f ([i i]) |
| 132 |
(if (= i n) |
| 133 |
'() |
| 134 |
(let ([x (string->symbol (format "s~a" i))]) |
| 135 |
(cons x (f (+ i 1))))))) |
| 136 |
|
| 137 |
(initialize-symbol-table!) |
| 138 |
|
| 139 |
|
| 140 |
(time |
| 141 |
(let () |
| 142 |
(gen-list 0 1000) |
| 143 |
(gen-list 0 1000) |
| 144 |
(time (do ((i 0 (+ i 1))) ((= i 1000)) (collect))) |
| 145 |
(gen-list 0 1000) |
| 146 |
(gen-list 1000 1001) |
| 147 |
; (gen-list 1000 2000) |
| 148 |
#f)) |
| 149 |
|
| 150 |
|
| 151 |
|