Skip to content

Instantly share code, notes, and snippets.

@jeroenvandijk
Created February 12, 2018 11:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeroenvandijk/eddd9646cadfc4a12f16f4d5390e7de4 to your computer and use it in GitHub Desktop.
Save jeroenvandijk/eddd9646cadfc4a12f16f4d5390e7de4 to your computer and use it in GitHub Desktop.
Sets and identity

Empty sets are always identical (apparently)

(identical? #{} #{}) ;=> true

But when we add values this changes

(identical? #{1} #{1}) ;=> false

What about results of operations?

(def set1 (set (range 100)))
(def set2 (set (range 100)))

(identical? set1 set2) ;=> false, expected given above

(def union (clojure.set/union set1 set2))

(identical? union set1) ;=> true, ok cool!
(identical? union set2) ;=> false, too bad

What does this mean? It means that if we generate sets without thought we can easily use more memory that needed. This has implications for yes, memory usage, but also for gc and thus cpu.

How can we prevent this waste? By using identical? at the right places and if this is too expensive (for large sets) we could use signatures and lookup maps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment