[llvm-commits] PATCH: DenseMap::find_as()

Talin viridia at gmail.com
Sun Jan 29 22:16:21 PST 2012


This patch adds a new method to DenseMap, "find_as()", which allows more
efficient lookups in cases where the map keys are expensive to construct.

Motivation: There are several examples within LLVM of maps which have keys
that contain instances of std::vector. Doing a find() on these maps
requires allocating memory to construct a new key. The "find_as()" method
is identical in behavior to "find()", except that the key type is a
template parameter to the function, allowing the lookup key to have a
different type than the keys stored in the map. This alternate key type can
be one that is much cheaper to construct, such as an ArrayRef instead of a
vector.

The alternate key type must be equality-comparable with the primary key
type, and it must hash to the same value. This is handled by the
DenseMapInfo for the map. For each alternate key type used, two additional
methods must be defined in the DenseMapInfo:

  unsigned getHashValue(AltKeyType);
  bool isEqual(AltKeyType, PrimaryKeyType);

This also means that maps that use find_as() must use a custom
DenseMapInfo, instead of the template default.

With this change, it will be possible to make some fairly significant
optimizations in ConstantUniqueMap and other places.

Note that I didn't come up with the name "find_as", I got it from another
STL implementation that I worked on while I was at Electronic Arts.

-- 
-- Talin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20120129/541857db/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: findas.patch
Type: application/octet-stream
Size: 3875 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20120129/541857db/attachment.obj>


More information about the llvm-commits mailing list