Bug 104890 - Attempt to rationalize and simplify WTF::binarySearch
Summary: Attempt to rationalize and simplify WTF::binarySearch
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Web Template Framework (show other bugs)
Version: 528+ (Nightly build)
Hardware: All All
: P2 Normal
Assignee: Filip Pizlo
URL:
Keywords:
Depends on:
Blocks: 105767
  Show dependency treegraph
 
Reported: 2012-12-13 01:08 PST by Filip Pizlo
Modified: 2012-12-26 05:52 PST (History)
17 users (show)

See Also:


Attachments
the patch (26.98 KB, patch)
2012-12-13 01:13 PST, Filip Pizlo
no flags Details | Formatted Diff | Diff
the patch (26.96 KB, patch)
2012-12-13 01:14 PST, Filip Pizlo
webkit-ews: commit-queue-
Details | Formatted Diff | Diff
the patch (28.05 KB, patch)
2012-12-13 01:58 PST, Filip Pizlo
webkit-ews: commit-queue-
Details | Formatted Diff | Diff
StdLibExtras.h (10.94 KB, text/plain)
2012-12-13 02:05 PST, Filip Pizlo
no flags Details
the patch (27.78 KB, patch)
2012-12-13 02:12 PST, Filip Pizlo
mjs: review+
buildbot: commit-queue-
Details | Formatted Diff | Diff
patch for landing (27.86 KB, patch)
2012-12-13 17:00 PST, Filip Pizlo
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Filip Pizlo 2012-12-13 01:08:00 PST
Currently the WTF::binarySearch API is a bit of a mess.  Here are the things that are wrong with it:

1) Three versions of binarySearch(), one that uses template specialization to determine the key extractor, one that uses a functor, and one that accepts array-like types that support operator[] but not operator++/--.

2) Inability to easily attempt a binary search an return some "error code" when the element is not found.  Instead the only mode like this returns an adjacent element, so then you have to do more checks to validate whether the element belonged to the array.

3) Inaccurate description of the approximate search.  To do an approximate search you pass WTF::KeyMustNotBePresentInArray, which to me implies that you want to assert that the key *is not* present.  In fact this mode instead asking the search to *not assert* that the key *is* present in the array.  That's kind of a bid difference.

We should fix this.
Comment 1 Filip Pizlo 2012-12-13 01:13:36 PST
Created attachment 179226 [details]
the patch
Comment 2 Filip Pizlo 2012-12-13 01:14:38 PST
Created attachment 179227 [details]
the patch
Comment 3 Early Warning System Bot 2012-12-13 01:21:44 PST
Comment on attachment 179227 [details]
the patch

Attachment 179227 [details] did not pass qt-ews (qt):
Output: http://queues.webkit.org/results/15316198
Comment 4 Filip Pizlo 2012-12-13 01:58:13 PST
Created attachment 179231 [details]
the patch
Comment 5 Filip Pizlo 2012-12-13 02:05:21 PST
Created attachment 179232 [details]
StdLibExtras.h

Diff really did a poor job of showing what I changed.  Uploading my copy of StdLibExtras.h for clarity.  For most purposes, the code relating to binarySearch was just completely changed so this might make for easier reviewing.
Comment 6 Maciej Stachowiak 2012-12-13 02:05:57 PST
Comment on attachment 179231 [details]
the patch

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

Here's review comments on the implementation, looking at call sites now.

> Source/WTF/ChangeLog:16
> +        Binary search now has three modes:
> +
> +        The default, aka KeyMustBePresentInArray: assert that the key was found, but return
> +        an adjacent element if it wasn't found, if asserts are turned off.
> +        
> +        KeyMightNotBePresentInArray, aka tryBinarySearch: return 0 if the key wasn't found.
> +        
> +        ReturnAdjacentElementIfKeyIsNotPresent, aka approximateBinarySearch: if the key is
> +        not found, return an adjacent element (either the left or right; no guarantee which).

This comment reflects the older version more. You should probably mention the three public interface functions, and as a side note maybe the corresponding flags passed to binarySearchImpl

> Source/WTF/wtf/StdLibExtras.h:188
> +    // The array must contain at least one element (pre-condition, array does conatin key).

tyop - should be "contain", not "contain"

> Source/WTF/wtf/StdLibExtras.h:228
> +// If the element is not found, crash if asserts are enabled, and behave like AlwaysReturnAdjacentElement in release builds.

Should probably say "behave like tryBinarySearch" instead of "behave like AlwaysReturnAdjacentElement" (especially since the actual name is ReturnAdjacentElementIfKeyIsNotPresent)
Comment 7 Early Warning System Bot 2012-12-13 02:09:50 PST
Comment on attachment 179231 [details]
the patch

Attachment 179231 [details] did not pass qt-ews (qt):
Output: http://queues.webkit.org/results/15316213
Comment 8 Filip Pizlo 2012-12-13 02:12:52 PST
Created attachment 179233 [details]
the patch

Build fixes, and style fixes.
Comment 9 Maciej Stachowiak 2012-12-13 02:23:54 PST
Comment on attachment 179233 [details]
the patch

r=me
Comment 10 Build Bot 2012-12-13 02:50:39 PST
Comment on attachment 179233 [details]
the patch

Attachment 179233 [details] did not pass win-ews (win):
Output: http://queues.webkit.org/results/15314201
Comment 11 Filip Pizlo 2012-12-13 17:00:21 PST
Created attachment 179378 [details]
patch for landing

Attempting to work around VS weirdness.
Comment 12 Filip Pizlo 2012-12-13 20:43:07 PST
Landed in http://trac.webkit.org/changeset/137709