RESOLVED FIXED 205449
Allow Vectors as hash keys
https://bugs.webkit.org/show_bug.cgi?id=205449
Summary Allow Vectors as hash keys
Antti Koivisto
Reported 2019-12-19 07:18:25 PST
Add traits to allow Vectors of hashable types to act as hash keys.
Attachments
patch (11.92 KB, patch)
2019-12-19 07:28 PST, Antti Koivisto
no flags
patch (12.14 KB, patch)
2019-12-19 08:03 PST, Antti Koivisto
no flags
patch (12.19 KB, patch)
2019-12-19 09:30 PST, Antti Koivisto
ggaren: review+
patch (12.21 KB, patch)
2019-12-19 10:41 PST, Antti Koivisto
no flags
Antti Koivisto
Comment 1 2019-12-19 07:28:55 PST
Antti Koivisto
Comment 2 2019-12-19 08:03:36 PST
Saam Barati
Comment 3 2019-12-19 08:55:28 PST
Comment on attachment 386102 [details] patch View in context: https://bugs.webkit.org/attachment.cgi?id=386102&action=review > Source/WTF/wtf/VectorHash.h:52 > + static constexpr bool emptyValueIsZero = false; Don’t you need to make the empty value too? What is the empty value?
Saam Barati
Comment 4 2019-12-19 08:58:49 PST
Comment on attachment 386102 [details] patch View in context: https://bugs.webkit.org/attachment.cgi?id=386102&action=review > Source/WTF/ChangeLog:22 > + Add traits. Empty Vector is the empty value. I see this now. I’m not a huge fan of this. What if there is a scenario where empty vector is a legit key? Maybe just make the empty value Max - 1 and nullptr > Source/WTF/wtf/Vector.h:670 > + : Base(0, -1) Should we use numeric_limits Max for cleanliness?
Antti Koivisto
Comment 5 2019-12-19 09:17:40 PST
> I see this now. I’m not a huge fan of this. What if there is a scenario > where empty vector is a legit key? Maybe just make the empty value Max - 1 > and nullptr Things are simpler and more understandable if the empty value is a regular valid value. I think for types that have well-defined "empty" it is better to stick with that. This can be reconsider if a use case where this matters appears.
Antti Koivisto
Comment 6 2019-12-19 09:30:24 PST
Antti Koivisto
Comment 7 2019-12-19 09:30:58 PST
now with numeric_limits
Geoffrey Garen
Comment 8 2019-12-19 10:09:05 PST
Comment on attachment 386111 [details] patch r=me I think you can use emptyValueIsZero if inlineCapacity is zero.
Antti Koivisto
Comment 9 2019-12-19 10:41:34 PST
WebKit Commit Bot
Comment 10 2019-12-19 11:29:10 PST
Comment on attachment 386120 [details] patch Clearing flags on attachment: 386120 Committed r253775: <https://trac.webkit.org/changeset/253775>
WebKit Commit Bot
Comment 11 2019-12-19 11:29:12 PST
All reviewed patches have been landed. Closing bug.
Radar WebKit Bug Importer
Comment 12 2019-12-19 11:30:24 PST
Saam Barati
Comment 13 2019-12-19 11:39:23 PST
(In reply to Antti Koivisto from comment #5) > > I see this now. I’m not a huge fan of this. What if there is a scenario > > where empty vector is a legit key? Maybe just make the empty value Max - 1 > > and nullptr > > Things are simpler and more understandable if the empty value is a regular > valid value. I think for types that have well-defined "empty" it is better > to stick with that. > > This can be reconsider if a use case where this matters appears. Why introduce this footgun? We already know from experience people want to put nullptr, zero, etc, in hash tables as keys. I don't see why we wouldn't also want an empty vector. I don't understand why having a well-defined "empty" is any more important than a well defined "deleted" value? If we're introducing something new, we might as well introduce it without the footgun.
Antti Koivisto
Comment 14 2019-12-19 12:31:02 PST
> Why introduce this footgun? We already know from experience people want to > put nullptr, zero, etc, in hash tables as keys. I don't see why we wouldn't > also want an empty vector. I don't understand why having a well-defined > "empty" is any more important than a well defined "deleted" value? If we're > introducing something new, we might as well introduce it without the footgun. Patches welcome. Deleted value is just some unique bits written to a hash table slot. The templates appear to expect empty value to be movable and destructible. It is not immediately obvious to me how to do without complicating Vector disproportionally.
Alexey Proskuryakov
Comment 15 2019-12-19 12:32:15 PST
Using a mutable container as key is a pretty big footgun by itself.
Antti Koivisto
Comment 16 2019-12-19 12:48:33 PST
(In reply to Alexey Proskuryakov from comment #15) > Using a mutable container as key is a pretty big footgun by itself. As opposed to any other mutable type?
Alexey Proskuryakov
Comment 17 2019-12-19 13:27:36 PST
A mutable container is more dangerous than e.g. an int. That's because those get passed over by reference all the time.
Note You need to log in before you can comment on or make changes to this bug.