-
Notifications
You must be signed in to change notification settings - Fork 38.7k
Optimize WebFlux multipart upload performance #35366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xyraclius
wants to merge
4
commits into
spring-projects:main
Choose a base branch
from
xyraclius:34651
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+87
β7
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -61,6 +61,7 @@ | |
* | ||
* @author Arjen Poutsma | ||
* @author Brian Clozel | ||
* @author Nabil Fawwaz Elqayyim | ||
* @since 5.0 | ||
*/ | ||
public abstract class DataBufferUtils { | ||
|
@@ -859,14 +860,28 @@ public void reset() { | |
|
||
|
||
/** | ||
* Base class for a {@link NestedMatcher}. | ||
* Base {@link NestedMatcher} implementation that scans a {@link DataBuffer} | ||
* for a specific delimiter. | ||
* | ||
* <p>Relies on a per-instance reusable buffer to scan data in chunks, | ||
* minimizing allocations and improving performance for large or streaming data.</p> | ||
* | ||
* <p>Each matcher maintains its own state and buffer, ensuring safe use | ||
* in reactive pipelines where execution may shift across threads.</p> | ||
* | ||
* <p>Subclasses may extend this class to customize matching strategies | ||
* while reusing the built-in delimiter tracking and scanning logic.</p> | ||
* | ||
* @see NestedMatcher | ||
* @see DataBuffer | ||
*/ | ||
private abstract static class AbstractNestedMatcher implements NestedMatcher { | ||
|
||
private final byte[] delimiter; | ||
|
||
private int matches = 0; | ||
|
||
private final byte[] localBuffer = new byte[8 * 1024]; // Reusable buffer per matcher instance | ||
|
||
protected AbstractNestedMatcher(byte[] delimiter) { | ||
this.delimiter = delimiter; | ||
|
@@ -882,14 +897,79 @@ protected int getMatches() { | |
|
||
@Override | ||
public int match(DataBuffer dataBuffer) { | ||
for (int pos = dataBuffer.readPosition(); pos < dataBuffer.writePosition(); pos++) { | ||
byte b = dataBuffer.getByte(pos); | ||
if (match(b)) { | ||
final int readPos = dataBuffer.readPosition(); | ||
final int writePos = dataBuffer.writePosition(); | ||
final int length = writePos - readPos; | ||
|
||
final byte[] delimiterBytes = this.delimiter; | ||
final int delimiterLength = delimiterBytes.length; | ||
final byte delimiterFirstByte = delimiterBytes[0]; | ||
|
||
final byte[] chunk = localBuffer; | ||
final int chunkSize = Math.min(chunk.length, length); | ||
|
||
int matchIndex = this.matches; | ||
|
||
try { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice approach to improve performance by using chunk based scanning as it reduces overhead of processing large buffers especially when uploading large files |
||
for (int offset = 0; offset < length; offset += chunkSize) { | ||
int currentChunkSize = Math.min(chunkSize, length - offset); | ||
|
||
dataBuffer.readPosition(readPos + offset); | ||
dataBuffer.read(chunk, 0, currentChunkSize); | ||
|
||
matchIndex = processChunk(chunk, currentChunkSize, delimiterBytes, delimiterLength, delimiterFirstByte, matchIndex, readPos, offset); | ||
if (matchIndex < 0) { | ||
return -(matchIndex + 1); // found, returning actual position | ||
} | ||
} | ||
|
||
this.matches = matchIndex; | ||
return -1; | ||
} | ||
finally { | ||
dataBuffer.readPosition(readPos); // restore original position | ||
} | ||
} | ||
|
||
private int processChunk(byte[] chunk, int currentChunkSize, byte[] delimiterBytes, int delimiterLen, byte delimiterFirstByte, int matchIndex, int readPos, int offset) { | ||
int i = 0; | ||
while (i < currentChunkSize) { | ||
if (matchIndex == 0) { | ||
i = findNextCandidate(chunk, i, currentChunkSize, delimiterFirstByte); | ||
if (i >= currentChunkSize) { | ||
return matchIndex; // no candidate in this chunk | ||
} | ||
} | ||
|
||
matchIndex = updateMatchIndex(chunk[i], delimiterBytes, delimiterLen, delimiterFirstByte, matchIndex); | ||
if (matchIndex == -1) { | ||
return -(readPos + offset + i + 1); // return found delimiter position (encoded as negative) | ||
} | ||
i++; | ||
} | ||
return matchIndex; | ||
} | ||
|
||
private int findNextCandidate(byte[] chunk, int start, int limit, byte delimiterFirstByte) { | ||
int j = start; | ||
while (j < limit && chunk[j] != delimiterFirstByte) { | ||
j++; | ||
} | ||
return j; | ||
} | ||
|
||
private int updateMatchIndex(byte b, byte[] delimiterBytes, int delimiterLen, byte delimiterFirstByte, int matchIndex) { | ||
if (b == delimiterBytes[matchIndex]) { | ||
matchIndex++; | ||
if (matchIndex == delimiterLen) { | ||
reset(); | ||
return pos; | ||
return -1; | ||
} | ||
} | ||
return -1; | ||
else { | ||
matchIndex = (b == delimiterFirstByte) ? 1 : 0; | ||
} | ||
return matchIndex; | ||
} | ||
|
||
@Override | ||
|
@@ -1026,7 +1106,7 @@ private static class ReadCompletionHandler implements CompletionHandler<Integer, | |
private final AtomicReference<State> state = new AtomicReference<>(State.IDLE); | ||
|
||
public ReadCompletionHandler(AsynchronousFileChannel channel, | ||
FluxSink<DataBuffer> sink, long position, DataBufferFactory dataBufferFactory, int bufferSize) { | ||
FluxSink<DataBuffer> sink, long position, DataBufferFactory dataBufferFactory, int bufferSize) { | ||
|
||
this.channel = channel; | ||
this.sink = sink; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good switch from a threadLocal buffer to per-instance buffer as in WebFlux, where requests may span across threads using threadLocal can cause concurrency issues so the change is much better for multi-threaded environments.