Re: [webkit-dev] Hash table empty value

2018-12-19 Thread Maciej Stachowiak


> On Dec 19, 2018, at 12:54 PM, Michael Catanzaro  wrote:
> 
> On Tue, Dec 18, 2018 at 2:31 PM, Ryosuke Niwa  wrote:
>> I tend to agree but then we'd come up with other numbers for the empty & 
>> deleted values.
>> I've been thinking that we could use -1 and -2 but that's also somewhat 
>> arbitrary restriction.
> 
> Using min/max values seems much safer. E.g. we already have in HashTraits.h:

I think that’s true for integers, but for pointers, 0 and -1 are likely safer 
(if you don’t need to store null pointers) or -1 and -2 if you do, since all 1 
bits is less likely to be a valid address. There’s also some optimization for 
the special case where the empty value is 0, but probably not that important if 
we often need to store null pointers. 

> // Default unsigned traits disallow both 0 and max as keys -- use these 
> traits to allow zero and disallow max - 1.
> template struct UnsignedWithZeroKeyHashTraits : 
> GenericHashTraits {
>   static const bool emptyValueIsZero = false;
>   static T emptyValue() { return std::numeric_limits::max(); }
>   static void constructDeletedValue(T& slot) { slot = 
> std::numeric_limits::max() - 1; }
>   static bool isDeletedValue(T value) { return value == 
> std::numeric_limits::max() - 1; }
> };
> 
> And:
> 
> template struct SignedWithZeroKeyHashTraits : 
> GenericHashTraits {
>   static const bool emptyValueIsZero = false;
>   static T emptyValue() { return std::numeric_limits::min(); }
>   static void constructDeletedValue(T& slot) { slot = 
> std::numeric_limits::max(); }
>   static bool isDeletedValue(T value) { return value == 
> std::numeric_limits::max(); }
> };
> 
> Which both seem much safer than the current default:
> 
> // Default integer traits disallow both 0 and -1 as keys (max value instead 
> of -1 for unsigned).
> template struct GenericHashTraitsBase : 
> GenericHashTraitsBase {
>   static const bool emptyValueIsZero = true;
>   static void constructDeletedValue(T& slot) { slot = static_cast(-1); }
>   static bool isDeletedValue(T value) { return value == static_cast(-1); }
> };
> 
> Michael
> 
> ___
> webkit-dev mailing list
> webkit-dev@lists.webkit.org
> https://lists.webkit.org/mailman/listinfo/webkit-dev

___
webkit-dev mailing list
webkit-dev@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-dev


[webkit-dev] Hash table empty value

2018-12-19 Thread Michael Catanzaro

On Tue, Dec 18, 2018 at 2:31 PM, Ryosuke Niwa  wrote:
I tend to agree but then we'd come up with other numbers for the 
empty & deleted values.
I've been thinking that we could use -1 and -2 but that's also 
somewhat arbitrary restriction.


Using min/max values seems much safer. E.g. we already have in 
HashTraits.h:


// Default unsigned traits disallow both 0 and max as keys -- use these 
traits to allow zero and disallow max - 1.
template struct UnsignedWithZeroKeyHashTraits : 
GenericHashTraits {

   static const bool emptyValueIsZero = false;
   static T emptyValue() { return std::numeric_limits::max(); }
   static void constructDeletedValue(T& slot) { slot = 
std::numeric_limits::max() - 1; }
   static bool isDeletedValue(T value) { return value == 
std::numeric_limits::max() - 1; }

};

And:

template struct SignedWithZeroKeyHashTraits : 
GenericHashTraits {

   static const bool emptyValueIsZero = false;
   static T emptyValue() { return std::numeric_limits::min(); }
   static void constructDeletedValue(T& slot) { slot = 
std::numeric_limits::max(); }
   static bool isDeletedValue(T value) { return value == 
std::numeric_limits::max(); }

};

Which both seem much safer than the current default:

// Default integer traits disallow both 0 and -1 as keys (max value 
instead of -1 for unsigned).
template struct GenericHashTraitsBase : 
GenericHashTraitsBase {

   static const bool emptyValueIsZero = true;
   static void constructDeletedValue(T& slot) { slot = 
static_cast(-1); }
   static bool isDeletedValue(T value) { return value == 
static_cast(-1); }

};

Michael

___
webkit-dev mailing list
webkit-dev@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-dev