Changeset 237476 in webkit


Ignore:
Timestamp:
Oct 26, 2018 11:58:50 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 Antti Koivisto.

Source/WTF:

  • wtf/HashTable.h:

(WTF::HashTable::random):
(WTF::HashTable::random const): Draw a new random bucket any time we
have a miss, to avoid bias caused by lopsided tables.

Tools:

  • TestWebKitAPI/Tests/WTF/HashMap.cpp: Updated the Random_IsRandom to

more thoroughly test for randomness.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r237461 r237476  
     12018-10-26  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 Antti Koivisto.
     7
     8        * wtf/HashTable.h:
     9        (WTF::HashTable::random):
     10        (WTF::HashTable::random const): Draw a new random bucket any time we
     11        have a miss, to avoid bias caused by lopsided tables.
     12
    1132018-10-26  Antti Koivisto  <antti@apple.com>
    214
  • trunk/Source/WTF/wtf/HashTable.h

    r237461 r237476  
    385385            if (isEmpty())
    386386                return end();
    387             auto it = makeIterator(m_table + (weakRandomUint32() & m_tableSizeMask));
    388             if (it == end())
    389                 return begin();
    390             return it;
     387
     388            while (1) {
     389                ValueType* entry = m_table + (weakRandomUint32() & m_tableSizeMask);
     390                if (isEmptyBucket(*entry) || isDeletedBucket(*entry))
     391                    continue;
     392                return makeKnownGoodIterator(entry);
     393            };
    391394        }
    392395
  • trunk/Tools/ChangeLog

    r237461 r237476  
     12018-10-26  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 Antti Koivisto.
     7
     8        * TestWebKitAPI/Tests/WTF/HashMap.cpp: Updated the Random_IsRandom to
     9        more thoroughly test for randomness.
     10
    1112018-10-26  Antti Koivisto  <antti@apple.com>
    212
  • trunk/Tools/TestWebKitAPI/Tests/WTF/HashMap.cpp

    r237419 r237476  
    10231023    unsigned twos = 0;
    10241024
    1025     for (unsigned i = 0; i < 100; ++i) {
     1025    for (unsigned i = 0; i < 1000; ++i) {
    10261026        auto it = map.random();
    10271027        if (it->value == 1)
     
    10331033    }
    10341034
    1035     ASSERT_EQ(ones + twos, 100u);
    1036     ASSERT_LE(ones, 99u);
    1037     ASSERT_LE(twos, 99u);
     1035    ASSERT_EQ(ones + twos, 1000u);
     1036    ASSERT_LE(ones, 600u);
     1037    ASSERT_LE(twos, 600u);
    10381038}
    10391039
Note: See TracChangeset for help on using the changeset viewer.