Github user franz1981 commented on a diff in the pull request:

    https://github.com/apache/activemq-artemis/pull/1786#discussion_r162272578
  
    --- Diff: 
artemis-commons/src/main/java/org/apache/activemq/artemis/api/core/SimpleString.java
 ---
    @@ -361,13 +371,68 @@ public int hashCode() {
           }
        }
     
    +   private static SimpleString[] splitWithCachedString(final SimpleString 
simpleString, final int delim) {
    +      final String str = simpleString.str;
    +      final byte[] data = simpleString.data;
    +      final int length = str.length();
    +      List<SimpleString> all = null;
    +      int index = 0;
    +      while (index < length) {
    +         final int delimIndex = str.indexOf(delim, index);
    +         if (delimIndex == -1) {
    +            //just need to add the last one
    +            break;
    +         } else {
    +            all = addSimpleStringPart(all, data, index, delimIndex);
    +         }
    +         index = delimIndex + 1;
    +      }
    +      if (all == null) {
    +         return new SimpleString[]{simpleString};
    +      } else {
    +         // Adding the last one
    +         all = addSimpleStringPart(all, data, index, length);
    +         // Converting it to arrays
    +         final SimpleString[] parts = new SimpleString[all.size()];
    +         return all.toArray(parts);
    +      }
    +   }
    +
    +   private static List<SimpleString> 
addSimpleStringPart(List<SimpleString> all,
    +                                                         final byte[] data,
    +                                                         final int 
startIndex,
    +                                                         final int 
endIndex) {
    +      final int expectedLength = endIndex - startIndex;
    +      final SimpleString ss;
    +      if (expectedLength == 0) {
    +         ss = EMPTY;
    +      } else {
    +         //extract a byte[] copy from this
    +         final int ssIndex = startIndex << 1;
    +         final int delIndex = endIndex << 1;
    +         final byte[] bytes = Arrays.copyOfRange(data, ssIndex, delIndex);
    +         ss = new SimpleString(bytes);
    +      }
    +      // We will create the ArrayList lazily
    +      if (all == null) {
    +         // There will be at least 3 strings on this case (which is the 
actual common usecase)
    --- End diff --
    
    Good point: the ArrayList is temporary and although it won't be allocated 
on the stack having 2 instead of 3 won't make a big difference considering that 
will die young. I've noticed that most use cases need at least 3 to avoid 
enlarging of capacity so I've changed it: I haven't changed the original 
version yet as you have noticed and I could do :)
    2 or 3 are very "magical" numbers anyway :P


---

Reply via email to