<div dir="ltr">In earlyCSE case, the size of DenseMap can be determined ahead of time, so it is fine to use (assuming the iteration overhead is low). There are other factors to consider when using DenseMap. It reduces one level of indirection by making the buckets an array of key/value pairs (unlike StringMap where buckets is an array of pointers to key/value pairs). When the value type is large and is expensive to construct, the overhead of rehashing DenseMap becomes even higher. The memory overhead also becomes larger as it has more large holes when the map is sparse.<div><br></div><div>I don't have a good answer to what the right alternatives (unordered_map, std::map etc -- it depends many factors depending on insertion and query patterns, value type, average size of the table etc and worse case scenarios). It is case by case so doing some experiment with performance data will be a helpful exercise. </div><div><br></div><div>thanks,</div><div><br></div><div>david</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Mar 15, 2016 at 3:30 PM, <span dir="ltr"><<a href="mailto:escha@apple.com" target="_blank">escha@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">What should we use instead of DenseMap?<div><br></div><div>—escha</div><div><div class="h5"><div><br><div><blockquote type="cite"><div>On Mar 15, 2016, at 3:30 PM, Xinliang David Li <<a href="mailto:xinliangli@gmail.com" target="_blank">xinliangli@gmail.com</a>> wrote:</div><br><div><div dir="ltr">yes it makes sense. Avoid using DenseMap when the size of the map is expected to be large but can not be pre-determined.<div><br></div><div>David</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Mar 15, 2016 at 3:07 PM, via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">There’s a few passes in LLVM that make heavy use of a big DenseMap, one that potentially gets filled with up to 1 entry for each instruction in the function. EarlyCSE is the best example, but Reassociate and MachineCSE have this to some degree as well (there might be others?). To put it simply: at least in my profile, EarlyCSE spends ~1/5 of its time growing DenseMaps. This is kind of… bad.<div><br></div><div>grow() is inherently slow because it needs to rehash and reinsert everything. This means growing a DenseMap costs much, much more than growing, for example, a vector. I talked about this with a few people and here are some possibilities we’ve come up with to improve this (some of which probably aren’t what we want):</div><div><br></div><div>1. Use a map that doesn’t require rehashing and reinsertion to grow. Chaining lets you do this, but std::unordered_map is probably so much slower than DenseMap we’d lose more than we gain.</div><div>2. Include the hash code in the map so that we don’t have to rehash. 32 bits more per entry (or whatever), and it might not help that much, since we still have to do the whole reinsertion routine.</div><div>3. Pre-calculate an estimate as to the map size we need. For example, in EarlyCSE, this is possibly gross overestimate of size needed:</div><div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">unsigned</span> InstCount = <span style="color:#272ad8">0</span>;</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">unsigned</span> LoadCount = <span style="color:#272ad8">0</span>;</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">unsigned</span> CallCount = <span style="color:#272ad8">0</span>;</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">for</span> (<span style="color:#4f8187">inst_iterator</span> FI = <span style="color:#31595d">inst_begin</span>(<span style="color:#4f8187">F</span>), FE = <span style="color:#31595d">inst_end</span>(<span style="color:#4f8187">F</span>); FI != FE; ++FI) {</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(49,89,93)"><span> </span><span style="color:#bb2ca2">if</span><span> (FI-></span>mayReadOrWriteMemory<span>())</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> ++LoadCount;</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">else</span> <span style="color:#bb2ca2">if</span> (<span style="color:#31595d">isa</span><<span style="color:#4f8187">CallInst</span>>(*FI))</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> ++CallCount;</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#bb2ca2">else</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> ++InstCount;</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> }</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#4f8187">AvailableValues</span>.<span style="color:#3d1d81">resize</span>(InstCount);</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#4f8187">AvailableLoads</span>.<span style="color:#3d1d81">resize</span>(LoadCount);</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"> <span style="color:#4f8187">AvailableCalls</span>.<span style="color:#3d1d81">resize</span>(CallCount);</div></div><div><br></div><div>But it does the job, and saves ~20% of time in EarlyCSE on my profiles. Yes, iterating over the entire function is way cheaper than grow(). Downsides are that while it’s still bounded by function size, it could end up allocating a good bit more depending on — in EarlyCSE’s case — the control flow/dominator structure.</div><div><br></div><div>Any thoughts on this, or other less ugly alternatives? I estimate that, at least in our pass pipeline, we’re losing at least ~1% of total time to avoidable DenseMap::grow() operations, which feels a little bit… unnecessary.</div><div><br></div><div>—escha</div></div><br>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div><br></div>
</div></blockquote></div><br></div></div></div></div></blockquote></div><br></div>