Bug 140858 - Provide more efficient implementation for WTF::DefaultHash<bool>
Summary: Provide more efficient implementation for WTF::DefaultHash<bool>
Status: RESOLVED INVALID
Alias: None
Product: WebKit
Classification: Unclassified
Component: Web Template Framework (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Chris Dumez
URL:
Keywords:
Depends on: 140848
Blocks:
  Show dependency treegraph
 
Reported: 2015-01-24 11:25 PST by Chris Dumez
Modified: 2015-01-30 18:45 PST (History)
6 users (show)

See Also:


Attachments
Patch (2.11 KB, patch)
2015-01-24 11:27 PST, Chris Dumez
darin: review-
darin: commit-queue-
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
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.