You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "nastra (via GitHub)" <gi...@apache.org> on 2024/03/22 14:12:02 UTC

[PR] Core: Lazily compute & cache hashCode in CharSequenceWrapper [iceberg]

nastra opened a new pull request, #10023:
URL: https://github.com/apache/iceberg/pull/10023

   (no comment)


-- 
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: issues-unsubscribe@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Core: Lazily compute & cache hashCode in CharSequenceWrapper [iceberg]

Posted by "nastra (via GitHub)" <gi...@apache.org>.
nastra commented on code in PR #10023:
URL: https://github.com/apache/iceberg/pull/10023#discussion_r1567311373


##########
api/src/main/java/org/apache/iceberg/util/CharSequenceWrapper.java:
##########
@@ -29,13 +29,16 @@ public static CharSequenceWrapper wrap(CharSequence seq) {
   }
 
   private CharSequence wrapped;
+  // lazily computed & cached hashCode
+  private transient int hashCode = 0;

Review Comment:
   ah good point, thanks for finding that. Instead of making this an `Integer` I actually introduced a boolean flag. The `String' implementation does the same thing so that the hash isn't re-calculated when the hash is actually 0



-- 
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: issues-unsubscribe@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Core: Lazily compute & cache hashCode in CharSequenceWrapper [iceberg]

Posted by "nastra (via GitHub)" <gi...@apache.org>.
nastra commented on code in PR #10023:
URL: https://github.com/apache/iceberg/pull/10023#discussion_r1535671891


##########
api/src/test/java/org/apache/iceberg/util/TestCharSequenceWrapper.java:
##########
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.util;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import org.junit.jupiter.api.Test;
+
+public class TestCharSequenceWrapper {
+
+  @Test
+  public void nullWrapper() {

Review Comment:
   this test uncovered a NPE in `JavaHashes`



-- 
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: issues-unsubscribe@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Core: Lazily compute & cache hashCode in CharSequenceWrapper [iceberg]

Posted by "nastra (via GitHub)" <gi...@apache.org>.
nastra commented on code in PR #10023:
URL: https://github.com/apache/iceberg/pull/10023#discussion_r1535670174


##########
api/src/main/java/org/apache/iceberg/types/JavaHashes.java:
##########
@@ -26,6 +26,10 @@ public class JavaHashes {
   private JavaHashes() {}
 
   public static int hashCode(CharSequence str) {
+    if (null == str) {

Review Comment:
   there are places in the codebase that use `CharSequenceWrapper.wrap(...)` and then set the wrapper to `null`. The issue was never uncovered because equals/hashCode was never called on those `CharSequenceWrapper` instances.
   
   When adding `TestCharSequenceWrapper` I noticed that this failed with a NPE, so I fixed this here and also added some tests around this



-- 
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: issues-unsubscribe@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Core: Lazily compute & cache hashCode in CharSequenceWrapper [iceberg]

Posted by "nastra (via GitHub)" <gi...@apache.org>.
nastra commented on code in PR #10023:
URL: https://github.com/apache/iceberg/pull/10023#discussion_r1535663017


##########
api/src/main/java/org/apache/iceberg/util/CharSequenceWrapper.java:
##########
@@ -67,7 +78,13 @@ public boolean equals(Object other) {
 
   @Override
   public int hashCode() {
-    return JavaHashes.hashCode(wrapped);
+    int hash = hashCode;

Review Comment:
   this uses a local `hash` var on purpose due to https://jeremymanson.blogspot.com/2008/12/benign-data-races-in-java.html. Also the `String` class itself uses the same approach when lazily calculating and caching a `hashCode`



-- 
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: issues-unsubscribe@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Core: Lazily compute & cache hashCode in CharSequenceWrapper [iceberg]

Posted by "amogh-jahagirdar (via GitHub)" <gi...@apache.org>.
amogh-jahagirdar commented on code in PR #10023:
URL: https://github.com/apache/iceberg/pull/10023#discussion_r1558672872


##########
api/src/main/java/org/apache/iceberg/util/CharSequenceWrapper.java:
##########
@@ -29,13 +29,16 @@ public static CharSequenceWrapper wrap(CharSequence seq) {
   }
 
   private CharSequence wrapped;
+  // lazily computed & cached hashCode
+  private transient int hashCode = 0;

Review Comment:
   Hmm, I'm not sure I'd set a default value of 0, since that technically can be a legit hashCode value for some series of characters no? 
   I think I'd make this an Integer hashCode and have it be null to identify that nothing is cached yet.



##########
api/src/main/java/org/apache/iceberg/util/CharSequenceWrapper.java:
##########
@@ -29,13 +29,16 @@ public static CharSequenceWrapper wrap(CharSequence seq) {
   }
 
   private CharSequence wrapped;
+  // lazily computed & cached hashCode
+  private transient int hashCode = 0;

Review Comment:
   But good call making it transient, that's important since it shouldn't be serialized and the cache somehow used across JVMs



-- 
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: issues-unsubscribe@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Core: Lazily compute & cache hashCode in CharSequenceWrapper [iceberg]

Posted by "amogh-jahagirdar (via GitHub)" <gi...@apache.org>.
amogh-jahagirdar commented on code in PR #10023:
URL: https://github.com/apache/iceberg/pull/10023#discussion_r1572637783


##########
api/src/main/java/org/apache/iceberg/util/CharSequenceWrapper.java:
##########
@@ -29,13 +29,16 @@ public static CharSequenceWrapper wrap(CharSequence seq) {
   }
 
   private CharSequence wrapped;
+  // lazily computed & cached hashCode
+  private transient int hashCode = 0;

Review Comment:
   Awesome, maybe just comment in the code that we're following what java.lang.String does



-- 
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: issues-unsubscribe@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Core: Lazily compute & cache hashCode in CharSequenceWrapper [iceberg]

Posted by "nastra (via GitHub)" <gi...@apache.org>.
nastra commented on code in PR #10023:
URL: https://github.com/apache/iceberg/pull/10023#discussion_r1567311373


##########
api/src/main/java/org/apache/iceberg/util/CharSequenceWrapper.java:
##########
@@ -29,13 +29,16 @@ public static CharSequenceWrapper wrap(CharSequence seq) {
   }
 
   private CharSequence wrapped;
+  // lazily computed & cached hashCode
+  private transient int hashCode = 0;

Review Comment:
   ah good point, thanks for finding that. Instead of making this an `Integer` I actually introduced a boolean flag. The `String` implementation does the same thing so that the hash isn't re-calculated when the hash is actually 0



-- 
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: issues-unsubscribe@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Core: Lazily compute & cache hashCode in CharSequenceWrapper [iceberg]

Posted by "nastra (via GitHub)" <gi...@apache.org>.
nastra merged PR #10023:
URL: https://github.com/apache/iceberg/pull/10023


-- 
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: issues-unsubscribe@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Core: Lazily compute & cache hashCode in CharSequenceWrapper [iceberg]

Posted by "amogh-jahagirdar (via GitHub)" <gi...@apache.org>.
amogh-jahagirdar commented on code in PR #10023:
URL: https://github.com/apache/iceberg/pull/10023#discussion_r1558676985


##########
api/src/main/java/org/apache/iceberg/util/CharSequenceWrapper.java:
##########
@@ -29,13 +29,16 @@ public static CharSequenceWrapper wrap(CharSequence seq) {
   }
 
   private CharSequence wrapped;
+  // lazily computed & cached hashCode
+  private transient int hashCode = 0;

Review Comment:
   I went down this rabbit hole: https://stackoverflow.com/questions/18746394/can-a-non-empty-string-have-a-hashcode-of-zero



-- 
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: issues-unsubscribe@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org


Re: [PR] Core: Lazily compute & cache hashCode in CharSequenceWrapper [iceberg]

Posted by "nastra (via GitHub)" <gi...@apache.org>.
nastra commented on code in PR #10023:
URL: https://github.com/apache/iceberg/pull/10023#discussion_r1574204162


##########
api/src/main/java/org/apache/iceberg/util/CharSequenceWrapper.java:
##########
@@ -29,13 +29,16 @@ public static CharSequenceWrapper wrap(CharSequence seq) {
   }
 
   private CharSequence wrapped;
+  // lazily computed & cached hashCode
+  private transient int hashCode = 0;

Review Comment:
   good point, I've added a comment mentioning that this follows java.lang.String



-- 
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: issues-unsubscribe@iceberg.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org