Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WFLY-11626 Reduce unnecessary per-request array allocation/copies whe…
…n parsing jsessionid
- Loading branch information
Showing
12 changed files
with
239 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
web-common/src/main/java/org/jboss/as/web/session/CompositeCharSequence.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2019, Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
|
||
package org.jboss.as.web.session; | ||
|
||
import java.nio.CharBuffer; | ||
import java.util.Arrays; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
/** | ||
* A {@link CharSequence} composed of other character sequences. | ||
* All methods delegate to one or more of the underlying character sequences, using relative indexes. | ||
* @author Paul Ferraro | ||
*/ | ||
class CompositeCharSequence implements CharSequence { | ||
private final List<CharSequence> sequences; | ||
|
||
CompositeCharSequence(CharSequence... sequences) { | ||
this(Arrays.asList(sequences)); | ||
} | ||
|
||
CompositeCharSequence(List<CharSequence> sequences) { | ||
this.sequences = sequences; | ||
} | ||
|
||
@Override | ||
public int length() { | ||
int length = 0; | ||
for (CharSequence sequence : this.sequences) { | ||
length += sequence.length(); | ||
} | ||
return length; | ||
} | ||
|
||
@Override | ||
public char charAt(int index) { | ||
int relativeIndex = index; | ||
for (CharSequence sequence : this.sequences) { | ||
if (relativeIndex < sequence.length()) { | ||
return sequence.charAt(relativeIndex); | ||
} | ||
relativeIndex -= sequence.length(); | ||
} | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
@Override | ||
public CharSequence subSequence(int start, int end) { | ||
if ((start < 0) || (start > end) || (end > this.length())) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
if (start == end) return ""; | ||
List<CharSequence> result = null; | ||
int relativeStart = start; | ||
int relativeEnd = end; | ||
for (CharSequence sequence : this.sequences) { | ||
if ((relativeStart < sequence.length()) && (relativeEnd > 0)) { | ||
CharSequence subSequence = sequence.subSequence(Math.max(relativeStart, 0), Math.min(relativeEnd, sequence.length())); | ||
if (result == null) { | ||
// If subsequence falls within a single sequence | ||
if ((relativeStart >= 0) && (relativeEnd <= sequence.length())) { | ||
return subSequence; | ||
} | ||
result = new LinkedList<>(); | ||
} | ||
result.add(subSequence); | ||
} | ||
relativeStart -= sequence.length(); | ||
relativeEnd -= sequence.length(); | ||
} | ||
return new CompositeCharSequence(result); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
CharBuffer buffer = CharBuffer.allocate(this.length()); | ||
for (CharSequence sequence : this.sequences) { | ||
buffer.put(CharBuffer.wrap(sequence)); | ||
} | ||
return String.valueOf(buffer.array()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
web-common/src/test/java/org/jboss/as/web/session/CompositeCharSequenceTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2019, Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
|
||
package org.jboss.as.web.session; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.util.stream.IntStream; | ||
|
||
import org.junit.Test; | ||
|
||
/** | ||
* Unit test for {@link CompositeCharSequence}. | ||
* @author Paul Ferraro | ||
*/ | ||
public class CompositeCharSequenceTestCase { | ||
@Test | ||
public void test() { | ||
CharSequence sequence = new CompositeCharSequence("01", "23", "45"); | ||
assertEquals(6, sequence.length()); | ||
assertEquals("012345", sequence.toString()); | ||
|
||
for (int i = -1; i < 7; ++i) { | ||
try { | ||
char result = sequence.charAt(i); | ||
if ((i < 0) || (i >= 6)) { | ||
fail(String.format("charAt(%d) returned '%s', but IndexOutOfBoundsException was expected", i, result)); | ||
} | ||
assertEquals('0' + i, result); | ||
} catch (IndexOutOfBoundsException e) { | ||
if ((i >= 0) && (i < 6)) { | ||
fail(String.format("Unexpected IndexOutOfBoundsException during charAt(%d)", i)); | ||
} | ||
} | ||
} | ||
|
||
for (int i = -1; i < 7; ++i) { | ||
for (int j = -1; j <= 7; ++j) { | ||
try { | ||
CharSequence result = sequence.subSequence(i, j); | ||
if ((i < 0) || (i > j) || (j > 6)) { | ||
fail(String.format("subSequence(%d, %d) returned '%s', but IndexOutOfBoundsException was expected", i, j, result.toString())); | ||
} | ||
StringBuilder expected = new StringBuilder(j - i); | ||
IntStream.range(i, j).forEach(value -> expected.append((char) ('0' + value))); | ||
assertEquals(expected.toString(), result.toString()); | ||
} catch (IndexOutOfBoundsException e) { | ||
if ((i >= 0) && (j <= 6) && (i <= j)) { | ||
fail(String.format("Unexpected IndexOutOfBoundsException during subSequence(%d, %d)", i, j)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.