Bug 140858

Summary: Provide more efficient implementation for WTF::DefaultHash<bool>
Product: WebKit Reporter: Chris Dumez <cdumez>
Component: Web Template FrameworkAssignee: Chris Dumez <cdumez>
Status: RESOLVED INVALID    
Severity: Normal CC: andersca, benjamin, cmarcelo, commit-queue, darin, koivisto
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: Unspecified   
OS: Unspecified   
Bug Depends on: 140848    
Bug Blocks:    
Attachments:
Description Flags
Patch darin: review-, darin: commit-queue-

Description Chris Dumez 2015-01-24 11:25:22 PST
Provide more efficient implementation for WTF::DefaultHash<bool>. Right now, it merely calls intHash(uint8_t) which is a bit wasteful.
Comment 1 Chris Dumez 2015-01-24 11:27:56 PST
Created attachment 245287 [details]
Patch
Comment 2 WebKit Commit Bot 2015-01-24 11:29:41 PST
Attachment 245287 [details] did not pass style-queue:


ERROR: Source/WTF/wtf/HashFunctions.h:182:  More than one command on the same line  [whitespace/newline] [4]
Total errors found: 1 in 2 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 3 Darin Adler 2015-01-24 15:06:38 PST
Comment on attachment 245287 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=245287&action=review

Not sure this optimization is important. But this patch doesn’t successfully apply it.

> Source/WTF/wtf/HashFunctions.h:41
> +        return key1 ? 0x62baf5a0 : 0x4636b9c9;

WebKit mostly uses uppercase hex; this is lowercase. How did you choose the branching version of this over the arithmetic version?

> Source/WTF/wtf/HashFunctions.h:182
> +    template<> struct DefaultHash<bool> { typedef IntHash<bool> Hash; };

This is not enough to get intHash(bool) called. The problem is that the IntHash struct template casts to IntTypes<sizeof(T)>::UnsignedType, so it’s going to choose some kind of integer type based on sizeof(bool). That’s not what we want. I think we’ll need:

    struct BoolHash {
        static unsigned hash(bool key) { return intHash(key); }
        static bool equal(bool a, bool b) { return a == b; }
        static const bool safeToCompareToEmptyOrDeleted = true;
    };

You can test by setting a breakpoint in a debug build and seeing the new function isn’t called.
Comment 4 Chris Dumez 2015-01-24 15:15:50 PST
Comment on attachment 245287 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=245287&action=review

I am not sure this optimization is very important either, but since you commented, I figured I would write a quick patch.

>> Source/WTF/wtf/HashFunctions.h:41
>> +        return key1 ? 0x62baf5a0 : 0x4636b9c9;
> 
> WebKit mostly uses uppercase hex; this is lowercase. How did you choose the branching version of this over the arithmetic version?

I only considered simplicity / readability. I haven't actually measured if one version is faster than the others. Wouldn't be difficult to benchmark those though if we want.

>> Source/WTF/wtf/HashFunctions.h:182
>> +    template<> struct DefaultHash<bool> { typedef IntHash<bool> Hash; };
> 
> This is not enough to get intHash(bool) called. The problem is that the IntHash struct template casts to IntTypes<sizeof(T)>::UnsignedType, so it’s going to choose some kind of integer type based on sizeof(bool). That’s not what we want. I think we’ll need:
> 
>     struct BoolHash {
>         static unsigned hash(bool key) { return intHash(key); }
>         static bool equal(bool a, bool b) { return a == b; }
>         static const bool safeToCompareToEmptyOrDeleted = true;
>     };
> 
> You can test by setting a breakpoint in a debug build and seeing the new function isn’t called.

Right, sorry about that. I didn't see IntHash was using typename IntTypes<sizeof(T)>::UnsignedType.