This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-10-05
Channels
- # announcements (19)
- # babashka (28)
- # beginners (62)
- # biff (3)
- # calva (19)
- # cider (24)
- # clj-kondo (8)
- # cljdoc (15)
- # clojure (32)
- # clojure-europe (16)
- # clojure-nl (1)
- # clojure-norway (17)
- # clojure-uk (8)
- # clojuredesign-podcast (26)
- # cursive (64)
- # datomic (43)
- # deps-new (1)
- # fulcro (4)
- # honeysql (1)
- # hyperfiddle (46)
- # kaocha (16)
- # lsp (15)
- # missionary (51)
- # music (1)
- # nbb (4)
- # off-topic (55)
- # pedestal (11)
- # podcasts-discuss (1)
- # polylith (7)
- # practicalli (1)
- # releases (4)
- # shadow-cljs (120)
- # tools-build (34)
- # vscode (1)
- # xtdb (2)
could anyone help me understand why {:a 1}
creates an array-map instead of a hash-map?
IMO hash-map has O(1) lookup time which is pretty nice. On the other hand, array-map has the benefit of maintaining insertion order, but I don’t think we really need that property.
This doesn't seem to be a clj-kondo question but I'll answer it here anyway: small hash maps (up to 8 entries) use an array map, then it switches to a hash map. This is an implementation detail and you shouldn't rely on it. Array map has a much simpler implementation than a regular hash map so it's better for overall performance/memory usage for small maps, as I understand it.
ah nice! that actually makes sense! Thank you Sean. And sorry I meant to ask in #C03S1KBA2
@U023C0QKU4W purely as a fun exercise you can try measuring the perf of looking for something (membership check) in a small fixed sized array vs a map, say upto 10-15 elements. the results might surprise you 😉 the C times O(n) vs C times O(1) starts to matter when n is small
I should also have mentioned that hash maps use a chunked hash trie, with a chunk size of 32, which is really efficient for large maps but still involves a number of sequential comparisons.
lookups in a hash map are not O(1), they are average O(1), which can be quite different from O(1) worst case