Changeset 237522 in webkit


Ignore:
Timestamp:
Oct 28, 2018 11:18:57 AM (6 years ago)
Author:
ggaren@apple.com
Message:

HashMap should support selecting a random entry
https://bugs.webkit.org/show_bug.cgi?id=190814

Reviewed by Ryosuke Niwa.

Source/WTF:

  • wtf/HashTable.h:

(WTF::HashTable::random):
(WTF::HashTable::random const): Merge the empty and deleted checks, and
use more idiomatic addressing.

Tools:

  • TestWebKitAPI/Tests/WTF/HashMap.cpp: Renamed IsRandom to

IsEvenlyDistributed to reflect the fact that we're only testing the
distribution. Added a test case that covers more table densities and
the remove() operation.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r237486 r237522  
     12018-10-28  Geoffrey Garen  <ggaren@apple.com>
     2
     3        HashMap should support selecting a random entry
     4        https://bugs.webkit.org/show_bug.cgi?id=190814
     5
     6        Reviewed by Ryosuke Niwa.
     7
     8        * wtf/HashTable.h:
     9        (WTF::HashTable::random):
     10        (WTF::HashTable::random const): Merge the empty and deleted checks, and
     11        use more idiomatic addressing.
     12
    1132018-10-26  Commit Queue  <commit-queue@webkit.org>
    214
  • trunk/Source/WTF/wtf/HashTable.h

    r237476 r237522  
    387387
    388388            while (1) {
    389                 ValueType* entry = m_table + (weakRandomUint32() & m_tableSizeMask);
    390                 if (isEmptyBucket(*entry) || isDeletedBucket(*entry))
    391                     continue;
    392                 return makeKnownGoodIterator(entry);
     389                auto& bucket = m_table[weakRandomUint32() & m_tableSizeMask];
     390                if (!isEmptyOrDeletedBucket(bucket))
     391                    return makeKnownGoodIterator(&bucket);
    393392            };
    394393        }
  • trunk/Tools/ChangeLog

    r237490 r237522  
     12018-10-28  Geoffrey Garen  <ggaren@apple.com>
     2
     3        HashMap should support selecting a random entry
     4        https://bugs.webkit.org/show_bug.cgi?id=190814
     5
     6        Reviewed by Ryosuke Niwa.
     7
     8        * TestWebKitAPI/Tests/WTF/HashMap.cpp: Renamed IsRandom to
     9        IsEvenlyDistributed to reflect the fact that we're only testing the
     10        distribution. Added a test case that covers more table densities and
     11        the remove() operation.
     12
    1132018-10-27  Charlie Turner  <cturner@igalia.com>
    214
  • trunk/Tools/TestWebKitAPI/Tests/WTF/HashMap.cpp

    r237476 r237522  
    10141014}
    10151015
    1016 TEST(WTF_HashMap, Random_IsRandom)
    1017 {
    1018     HashMap<unsigned, unsigned> map;
     1016TEST(WTF_HashMap, Random_IsEvenlyDistributed)
     1017{
     1018    HashMap<unsigned, unsigned, DefaultHash<unsigned>::Hash, WTF::UnsignedWithZeroKeyHashTraits<unsigned>> map;
     1019    map.add(0, 0);
    10191020    map.add(1, 1);
    1020     map.add(2, 2);
    1021 
     1021
     1022    unsigned zeros = 0;
    10221023    unsigned ones = 0;
    1023     unsigned twos = 0;
    10241024
    10251025    for (unsigned i = 0; i < 1000; ++i) {
    10261026        auto it = map.random();
    1027         if (it->value == 1)
     1027        if (!it->value)
     1028            ++zeros;
     1029        else {
     1030            ASSERT_EQ(it->value, 1u);
    10281031            ++ones;
    1029         else {
    1030             ASSERT_EQ(it->value, 2u);
    1031             ++twos;
    10321032        }
    10331033    }
    10341034
    1035     ASSERT_EQ(ones + twos, 1000u);
     1035    ASSERT_EQ(zeros + ones, 1000u);
     1036    ASSERT_LE(zeros, 600u);
    10361037    ASSERT_LE(ones, 600u);
    1037     ASSERT_LE(twos, 600u);
     1038}
     1039
     1040TEST(WTF_HashMap, Random_IsEvenlyDistributedAfterRemove)
     1041{
     1042    for (size_t tableSize = 2; tableSize <= 2 * 6; ++tableSize) { // Our hash tables shrink at a load factor of 1 / 6.
     1043        HashMap<unsigned, unsigned, DefaultHash<unsigned>::Hash, WTF::UnsignedWithZeroKeyHashTraits<unsigned>> map;
     1044        for (size_t i = 0; i < tableSize; ++i)
     1045            map.add(i, i);
     1046        for (size_t i = 2; i < tableSize; ++i)
     1047            map.remove(i);
     1048
     1049        unsigned zeros = 0;
     1050        unsigned ones = 0;
     1051
     1052        for (unsigned i = 0; i < 1000; ++i) {
     1053            auto it = map.random();
     1054            if (!it->value)
     1055                ++zeros;
     1056            else {
     1057                ASSERT_EQ(it->value, 1u);
     1058                ++ones;
     1059            }
     1060        }
     1061
     1062        ASSERT_EQ(zeros + ones, 1000u);
     1063        ASSERT_LE(zeros, 600u);
     1064        ASSERT_LE(ones, 600u);
     1065    }
    10381066}
    10391067
Note: See TracChangeset for help on using the changeset viewer.