Skip to content

Commit a42fe81

Browse files
committed
Add strings to heap diagrams; fix up a bug in heap diagrams.
1 parent 962ff86 commit a42fe81

File tree

2 files changed

+94
-8
lines changed

2 files changed

+94
-8
lines changed

www/notes/diagrams.rkt

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88

99
(define n 40)
1010

11+
(define (make-str-cells s)
12+
(hc-append
13+
(foldl (λ (c p)
14+
(hc-append p
15+
(cc-superimpose (rectangle (/ n 2) n) ((current-code-tt) (string c)))))
16+
(cc-superimpose (rectangle n n) (code #,(string-length s)))
17+
(string->list s))
18+
(if (even? (string-length s))
19+
(rectangle 0 n)
20+
(cc-superimpose (rectangle (/ n 2) n) (code 0)))))
21+
1122
(define (make-imm-cell i)
1223
(cc-superimpose
1324
(code #,i)
@@ -17,6 +28,9 @@
1728
(cb-superimpose (rectangle n n)
1829
(code cons)))
1930

31+
(define (make-str-cell)
32+
(cb-superimpose (rectangle n n)
33+
(code str)))
2034

2135
(define (make-box-cell)
2236
(cb-superimpose (rectangle n n)
@@ -72,13 +86,16 @@
7286
[`(cons ,_) (make-cons-cell)]
7387
[`(box ,_) (make-box-cell)]
7488
[`(vect ,_) (make-vect-cell)]
89+
[`(str ,_) (make-str-cell)]
90+
[(? string?) (make-str-cells v)]
7591
[_ (make-imm-cell v)]))
7692

7793
(define (add-arrows spec cells p)
7894
;(printf "~a~n" spec)
7995
(match spec
8096
['() p]
81-
[(cons `(_ ,i) s)
97+
[(cons `(,_ ,(? integer? i)) s)
98+
8299
(add-arrows s
83100
cells
84101
(fwd-pts-to (list-ref cells (sub1 (- (length cells) (length s))))
@@ -99,17 +116,62 @@
99116
heap)))
100117

101118
(define heap/arrows/label
102-
(vc-append
103-
0
104-
heap/arrows
105-
(text "heap")))
119+
(vc-append 10
120+
(vc-append
121+
0
122+
heap/arrows
123+
(text "heap"))
124+
(text "← lower addresses, higher addresses →")))
106125

107126
(define rax/label
108-
(vc-append 0 rax (text "rax")))
127+
(vc-append 10 (vc-append 0 rax (text "rax"))
128+
(text " ")))
109129

110130
(inset
111-
(fwd-pts-to rax (list-ref heap i) (hc-append n rax/label heap/arrows/label))
112-
(* n 2))]))
131+
(fwd-pts-to rax (list-ref heap i)
132+
(hc-append n rax/label heap/arrows/label))
133+
0 (* n 2) 0 0)]))
134+
135+
#;
136+
(define cons-quiz
137+
(list (make-heap-diagram
138+
'((cons 4)
139+
3
140+
'()
141+
2
142+
(cons 0)
143+
1
144+
(cons 2)))
145+
(make-heap-diagram
146+
'((cons 0)
147+
1
148+
(cons 2)
149+
2
150+
(cons 4)
151+
3
152+
'()))
153+
(make-heap-diagram
154+
'((cons 4)
155+
'()
156+
3
157+
(cons 0)
158+
2
159+
(cons 2)
160+
1))
161+
(make-heap-diagram
162+
'((cons 0)
163+
(cons 2)
164+
1
165+
(cons 4)
166+
2
167+
'()
168+
3))
169+
))
170+
171+
172+
173+
;(text "← lower addresses, higher addresses →")
174+
113175
#;
114176
(make-heap-diagram
115177
'((cons 0)

www/notes/hoax.scrbl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,18 @@ So, suppose we want to create the string @racket["abc"]:
899899
(Pop rbx)
900900
(Ret))))
901901

902+
This creates a string in memory like this:
903+
904+
@make-heap-diagram['((str 0) "abc")]
905+
906+
907+
In this diagram, we use half-sized boxes to indicate that only 4 bytes
908+
are used per codepoint. Also the contents of the array are not
909+
values, but codepoints (shown as the letters the codepoint encodes).
910+
In odd length strings, we write @racket[0] for the padded element, but
911+
in reality there are arbitrary bits at that memory location, which is
912+
fine because they will never be read.
913+
902914
At first glance, this looks remarkably similar to creating a vector,
903915
however there are some imporant things to notice:
904916

@@ -926,6 +938,18 @@ you are seeing here.}
926938

927939
]
928940

941+
Notice that a string like @racket["fred"] is not represented the same
942+
as a vector of characters @racket[#(#\f #\r #\e #\d)], which uses more
943+
space. Compare:
944+
945+
@make-heap-diagram['((str 0) "fred")]
946+
947+
versus:
948+
949+
@make-heap-diagram['((vect 0) 4 #\f #\r #\e #\d)]
950+
951+
952+
929953
Now let's set things up like we did before to be able to interactively
930954
write examples in order to arrive at the code for
931955
@racket[string-length], @racket[string-ref], and @racket[make-string]:

0 commit comments

Comments
 (0)