You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/03/14 13:21:26 UTC

[GitHub] [pulsar] Jason918 commented on a change in pull request #14678: [Performance] Optimize PositionImpl toString, compareTo and hashCode methods

Jason918 commented on a change in pull request #14678:
URL: https://github.com/apache/pulsar/pull/14678#discussion_r825936547



##########
File path: managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/PositionImpl.java
##########
@@ -102,20 +99,27 @@ public PositionImpl getNext() {
      */
     @Override
     public String toString() {
-        return String.format("%d:%d", ledgerId, entryId);
+        return ledgerId + ":" + entryId;
     }
 
     @Override
-    public int compareTo(PositionImpl other) {
-        checkNotNull(other);
+    public int compareTo(PositionImpl that) {
+        if (this.ledgerId != that.ledgerId) {
+            return (this.ledgerId < that.ledgerId ? -1 : 1);

Review comment:
       `java.lang.Long#compare(this.ledgerId, other.ledgerId)` is more readable.

##########
File path: managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/PositionImpl.java
##########
@@ -102,20 +99,27 @@ public PositionImpl getNext() {
      */
     @Override
     public String toString() {
-        return String.format("%d:%d", ledgerId, entryId);
+        return ledgerId + ":" + entryId;
     }
 
     @Override
-    public int compareTo(PositionImpl other) {
-        checkNotNull(other);
+    public int compareTo(PositionImpl that) {
+        if (this.ledgerId != that.ledgerId) {
+            return (this.ledgerId < that.ledgerId ? -1 : 1);
+        }
+
+        if (this.entryId != that.entryId) {
+            return (this.entryId < that.entryId ? -1 : 1);
+        }
 
-        return ComparisonChain.start().compare(this.ledgerId, other.ledgerId).compare(this.entryId, other.entryId)
-                .result();
+        return 0;
     }
 
     @Override
     public int hashCode() {
-        return Objects.hashCode(ledgerId, entryId);
+        int result = (int) (ledgerId ^ (ledgerId >>> 32));
+        result = 31 * result + (int) (entryId ^ (entryId >>> 32));
+        return result;

Review comment:
       I am curious if we drop `ackSet` from hashCode on purpose or not.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org