Skip to content

Commit

Permalink
update-ability-decks: increase structural sharing (?)
Browse files Browse the repository at this point in the history
The following micro-benchmark shows that repeatedly updating a hash is
consistently more memory efficient than reconstructing it:

    #lang racket

    (define base (hash 'a 1 'b 2 'c 3))
    (define f add1)

    (collect-garbage)
    (collect-garbage)
    (collect-garbage)

    (define m (current-memory-use))
    (void
     (for/fold ([x base])
               ([_i 1000])
       (for/hash ([(k v) (in-hash x)])
         (values k (f v)))))
    (println (- (current-memory-use) m))

    (collect-garbage)
    (collect-garbage)
    (collect-garbage)

    (define m2 (current-memory-use))
    (void
     (for/fold ([x base])
               ([_i 1000])
       (for/fold ([acc x])
                 ([k (in-hash-keys x)])
         (hash-update acc k f))))
    (println (- (current-memory-use) m2))

On my machine with _i up to 100, I consistently (no variation across runs at
all) get

    25984 ; reconstruction
    21216 ; updating

With _i up to 1000 as in the program above (same consistency):

    240624 ; reconstruction
    192640 ; updating

Cranking _i up to 100000 and adding (time) around the outer (for/fold)
shows that the latter method is actually _slower_ (by ~20ms of real
time), but my hashes have less than 10 keys on average, and we know
long GC pauses are part of the performance problem.
  • Loading branch information
benknoble committed Nov 30, 2024
1 parent dafecdc commit a48a01d
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions manager/ability-decks.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@
(void)))

(define ((update-ability-decks f) ads)
(for/hash ([(set ad) (in-hash ads)])
(values set (f set ad))))
(for/fold ([result ads])
([set (in-hash-keys ads)])
(hash-update result set (λ (ad) (f set ad)))))

(define (move-top-draw-to-bottom ads)
(define the-draw (ability-decks-draw ads))
Expand Down

0 comments on commit a48a01d

Please sign in to comment.