You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@zookeeper.apache.org by narahari92 <gi...@git.apache.org> on 2018/06/08 16:42:38 UTC

[GitHub] zookeeper pull request #539: ZOOKEEPER-3058: Do length check first before ac...

GitHub user narahari92 opened a pull request:

    https://github.com/apache/zookeeper/pull/539

    ZOOKEEPER-3058: Do length check first before actual byte check in compareBytes method of Utils class

    In compareBytes method of org.apache.jute.Utils class, all the individual bytes of 2 byte arrays are compared first and then their lengths are compared. We can improve the performance by first having length check, since we can rule out that they aren't equal by a single if condition(O(1) operation) rather than looping through arrays(O( n ) operation).

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/narahari92/zookeeper ZOOKEEPER-3058

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/zookeeper/pull/539.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #539
    
----
commit 89239f84b1d322f170973ccdafa989508a6b8f09
Author: Hosur Narahari <ho...@...>
Date:   2018-06-08T16:32:00Z

    improving compareBytes method of Utils class

----


---

[GitHub] zookeeper pull request #539: ZOOKEEPER-3058: Do length check first before ac...

Posted by maoling <gi...@git.apache.org>.
Github user maoling commented on a diff in the pull request:

    https://github.com/apache/zookeeper/pull/539#discussion_r194248152
  
    --- Diff: src/java/main/org/apache/jute/Utils.java ---
    @@ -268,15 +268,14 @@ static String toCSVBuffer(byte barr[]) {
             return stream.toByteArray();
         }
         public static int compareBytes(byte b1[], int off1, int len1, byte b2[], int off2, int len2) {
    -        int i;
    -        for(i=0; i < len1 && i < len2; i++) {
    +        if (len1 != len2) {
    +            return len1 < len2 ? -1 : 1;
    +        }
    +        for(int i=0; i < len1; i++) {
                 if (b1[off1+i] != b2[off2+i]) {
    --- End diff --
    
    no a issue?
    This function is for comparison byte one by one.
    Some UT for understanding:
    zbcd : abcde  --> 1
    abcd :  abce --> 1
    abcd :  abca --> -1
    abcd :  abcde --> 1
    abcd :  abc --> -1


---

[GitHub] zookeeper pull request #539: ZOOKEEPER-3058: Do length check first before ac...

Posted by narahari92 <gi...@git.apache.org>.
Github user narahari92 closed the pull request at:

    https://github.com/apache/zookeeper/pull/539


---

[GitHub] zookeeper issue #539: ZOOKEEPER-3058: Do length check first before actual by...

Posted by lvfangmin <gi...@git.apache.org>.
Github user lvfangmin commented on the issue:

    https://github.com/apache/zookeeper/pull/539
  
    This do improve the performance, but it changed the behavior of compareBytes function, for example, if b1='ab' and b2='c', previously it will return -1, but now it returns 1, which is not correct.


---

[GitHub] zookeeper issue #539: ZOOKEEPER-3058: Do length check first before actual by...

Posted by narahari92 <gi...@git.apache.org>.
Github user narahari92 commented on the issue:

    https://github.com/apache/zookeeper/pull/539
  
    Yes it does change functionality. My bad


---