# Why Builtin Dict \`ht\_keyindex\` use & rather than %?

**URL:** <https://discourse.julialang.org/t/why-builtin-dict-ht-keyindex-use-rather-than/10681>\
**Category:** General Usage\
**Created:** [May 3, 2018, 1:28pm UTC](https://discourse.julialang.org/t/why-builtin-dict-ht-keyindex-use-rather-than/10681 "2018-05-03T13:28:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [May 3, 2018, 1:28pm UTC](https://discourse.julialang.org/t/why-builtin-dict-ht-keyindex-use-rather-than/10681/1 "2018-05-03T13:28:52Z")

</div>

I was reading the implementation of `Dict`, I don’t know why `ht_keyindex` use `(index & (sz-1)) + 1` rather than `index % sz + 1`, won’t the compiler optimize it? I use `BenchmarkTools` to check whether the previous implementation is faster, and it is a little bit faster indeed. But I would like to know why this is faster.

I tested this on Julia v0.6.3.

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [May 9, 2018, 5:37pm UTC](https://discourse.julialang.org/t/why-builtin-dict-ht-keyindex-use-rather-than/10681/2 "2018-05-09T17:37:51Z")

</div>

The compiler can only optimize it if it could prove they are equivalent. Since `sz` is an arbitrary number, the compiler can’t know that it’s always actually a power-of-two number.
