# Racket set performance

**URL:** <https://racket.discourse.group/t/racket-set-performance/479>\
**Category:** General\
**Tags:** performance\
**Created:** [December 26, 2021, 2:54am UTC](https://racket.discourse.group/t/racket-set-performance/479 "2021-12-26T02:54:43Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![sorawee](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@sorawee](https://racket.discourse.group/u/sorawee)\
**Post date:** [December 26, 2021, 4:23pm UTC](https://racket.discourse.group/t/racket-set-performance/479/8 "2021-12-26T16:23:41Z")

</div>

A couple more variants using nested `hasheq`:

```scheme
(define mutable-set make-hasheq)

(define (in-set s)
  (for*/list ([(x hx) s] [(y hy) hx])
    (make-rectangular x y)))

(define (set-add! s v)
  (hash-set! (hash-ref! s (real-part v) make-hasheq)
             (imag-part v)
             #t))

(define (set-member? s v)
  (hash-has-key? (hash-ref s (real-part v) make-hasheq) 
                 (imag-part v)))

```

This takes 770ms on my machine, but `in-set` will create a potentially long list, so it’s not ideal.

We can change `in-set` to

```scheme
(define (in-set s)
  (for*/stream ([(x hx) s] [(y hy) hx])
    (make-rectangular x y)))

```

This takes 1.1s.

An efficient solution would use `define-sequence-syntax`, which uses `for*/stream` when it’s not used directly in a `for` form, and uses `:do-in` when it appears directly in a `for` form. This should make it even faster than the for\*/list variant.

---

_[View the full topic](https://racket.discourse.group/t/racket-set-performance/479)._
