-
Notifications
You must be signed in to change notification settings - Fork 26
Improve XML paging serialization performance #3690
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
Draft
astruebi
wants to merge
7
commits into
samply:main
Choose a base branch
from
astruebi:codex/xml-direct-writer-review
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.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
68b84d3
Optimize XML response output
astruebi 37484fb
Reduce generic XML writer overhead
astruebi cddb538
Specialize XML search bundle entries
astruebi 53928a0
Add direct XML writers for common complex types
astruebi 2f81c10
Write XML through a UTF-8 writer
astruebi 3970432
Expand direct XML writing for common values
astruebi 993f31e
Document XML optimization handover
astruebi 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
208 changes: 208 additions & 0 deletions
208
modules/fhir-structure/java/blaze/fhir/XmlUtf8Writer.java
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 |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| package blaze.fhir; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.io.Writer; | ||
|
|
||
| public final class XmlUtf8Writer extends Writer { | ||
|
|
||
| private final OutputStream out; | ||
| private final byte[] buffer; | ||
| private int pos; | ||
|
|
||
| public XmlUtf8Writer(OutputStream out, int bufferSize) { | ||
| this.out = out; | ||
| this.buffer = new byte[bufferSize]; | ||
| } | ||
|
|
||
| private void ensureOpen() throws IOException { | ||
| if (out == null) { | ||
| throw new IOException("Stream closed"); | ||
| } | ||
| } | ||
|
|
||
| private void ensureCapacity(int n) throws IOException { | ||
| if (buffer.length - pos < n) { | ||
| flushBuffer(); | ||
| } | ||
| } | ||
|
|
||
| private void flushBuffer() throws IOException { | ||
| if (pos > 0) { | ||
| out.write(buffer, 0, pos); | ||
| pos = 0; | ||
| } | ||
| } | ||
|
|
||
| private void writeByte(int b) throws IOException { | ||
| ensureCapacity(1); | ||
| buffer[pos++] = (byte) b; | ||
| } | ||
|
|
||
| private void writeCodePoint(int codePoint) throws IOException { | ||
| if (codePoint < 0x80) { | ||
| writeByte(codePoint); | ||
| } else if (codePoint < 0x800) { | ||
| ensureCapacity(2); | ||
| buffer[pos++] = (byte) (0xC0 | (codePoint >> 6)); | ||
| buffer[pos++] = (byte) (0x80 | (codePoint & 0x3F)); | ||
| } else if (codePoint < 0x10000) { | ||
| ensureCapacity(3); | ||
| buffer[pos++] = (byte) (0xE0 | (codePoint >> 12)); | ||
| buffer[pos++] = (byte) (0x80 | ((codePoint >> 6) & 0x3F)); | ||
| buffer[pos++] = (byte) (0x80 | (codePoint & 0x3F)); | ||
| } else { | ||
| ensureCapacity(4); | ||
| buffer[pos++] = (byte) (0xF0 | (codePoint >> 18)); | ||
| buffer[pos++] = (byte) (0x80 | ((codePoint >> 12) & 0x3F)); | ||
| buffer[pos++] = (byte) (0x80 | ((codePoint >> 6) & 0x3F)); | ||
| buffer[pos++] = (byte) (0x80 | (codePoint & 0x3F)); | ||
| } | ||
| } | ||
|
|
||
| private void writeAscii(String str, int off, int end) throws IOException { | ||
| while (off < end) { | ||
| ensureCapacity(1); | ||
| int n = Math.min(buffer.length - pos, end - off); | ||
| for (int i = 0; i < n; i++) { | ||
| buffer[pos + i] = (byte) str.charAt(off + i); | ||
| } | ||
| pos += n; | ||
| off += n; | ||
| } | ||
| } | ||
|
|
||
| private void writeAscii(char[] cbuf, int off, int end) throws IOException { | ||
| while (off < end) { | ||
| ensureCapacity(1); | ||
| int n = Math.min(buffer.length - pos, end - off); | ||
| for (int i = 0; i < n; i++) { | ||
| buffer[pos + i] = (byte) cbuf[off + i]; | ||
| } | ||
| pos += n; | ||
| off += n; | ||
| } | ||
| } | ||
|
|
||
| private static boolean validXmlChar(char c) { | ||
| return c == 0x09 || c == 0x0A || c == 0x0D || | ||
| (0x20 <= c && c <= 0xD7FF) || | ||
| (0xE000 <= c && c <= 0xFFFD); | ||
| } | ||
|
|
||
| public void writeEscaped(String str) throws IOException { | ||
| ensureOpen(); | ||
| int len = str.length(); | ||
| for (int i = 0; i < len; i++) { | ||
| char c = str.charAt(i); | ||
| if (c < 0x80) { | ||
| if (c >= 0x20) { | ||
| switch (c) { | ||
| case '&' -> writeAscii("&", 0, 5); | ||
| case '<' -> writeAscii("<", 0, 4); | ||
| case '>' -> writeAscii(">", 0, 4); | ||
| case '"' -> writeAscii(""", 0, 6); | ||
| default -> { | ||
| if (pos == buffer.length) { | ||
| flushBuffer(); | ||
| } | ||
| buffer[pos++] = (byte) c; | ||
| } | ||
| } | ||
| } else if (c == 0x09 || c == 0x0A || c == 0x0D) { | ||
| if (pos == buffer.length) { | ||
| flushBuffer(); | ||
| } | ||
| buffer[pos++] = (byte) c; | ||
| } else { | ||
| writeByte('?'); | ||
| } | ||
| } else if (Character.isHighSurrogate(c)) { | ||
| int j = i + 1; | ||
| if (j < len && Character.isLowSurrogate(str.charAt(j))) { | ||
| writeCodePoint(Character.toCodePoint(c, str.charAt(j))); | ||
| i = j; | ||
| } else { | ||
| writeByte('?'); | ||
| } | ||
| } else if (Character.isLowSurrogate(c) || !validXmlChar(c)) { | ||
| writeByte('?'); | ||
| } else { | ||
| writeCodePoint(c); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void write(int c) throws IOException { | ||
| ensureOpen(); | ||
| writeCodePoint(Character.isSurrogate((char) c) ? '?' : c); | ||
| } | ||
|
|
||
| @Override | ||
| public void write(char[] cbuf, int off, int len) throws IOException { | ||
| ensureOpen(); | ||
| int end = off + len; | ||
| for (int i = off; i < end; i++) { | ||
| char c = cbuf[i]; | ||
| if (c < 0x80) { | ||
| if (pos == buffer.length) { | ||
| flushBuffer(); | ||
| } | ||
| buffer[pos++] = (byte) c; | ||
| } else if (Character.isHighSurrogate(c)) { | ||
| int j = i + 1; | ||
| if (j < end && Character.isLowSurrogate(cbuf[j])) { | ||
| writeCodePoint(Character.toCodePoint(c, cbuf[j])); | ||
| i = j; | ||
| } else { | ||
| writeByte('?'); | ||
| } | ||
| } else if (Character.isLowSurrogate(c)) { | ||
| writeByte('?'); | ||
| } else { | ||
| writeCodePoint(c); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void write(String str, int off, int len) throws IOException { | ||
| ensureOpen(); | ||
| int end = off + len; | ||
| for (int i = off; i < end; i++) { | ||
| char c = str.charAt(i); | ||
| if (c < 0x80) { | ||
| if (pos == buffer.length) { | ||
| flushBuffer(); | ||
| } | ||
| buffer[pos++] = (byte) c; | ||
| } else if (Character.isHighSurrogate(c)) { | ||
| int j = i + 1; | ||
| if (j < end && Character.isLowSurrogate(str.charAt(j))) { | ||
| writeCodePoint(Character.toCodePoint(c, str.charAt(j))); | ||
| i = j; | ||
| } else { | ||
| writeByte('?'); | ||
| } | ||
| } else if (Character.isLowSurrogate(c)) { | ||
| writeByte('?'); | ||
| } else { | ||
| writeCodePoint(c); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void flush() throws IOException { | ||
| ensureOpen(); | ||
| flushBuffer(); | ||
| out.flush(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| flush(); | ||
| out.close(); | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package blaze.fhir; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.Writer; | ||
|
|
||
| public final class XmlUtil { | ||
|
|
||
| private XmlUtil() { | ||
| } | ||
|
|
||
| private static boolean validXmlChar(char c) { | ||
| return c == 0x09 || c == 0x0A || c == 0x0D || | ||
| (0x20 <= c && c <= 0xD7FF) || | ||
| (0xE000 <= c && c <= 0xFFFD); | ||
| } | ||
|
|
||
| public static void writeEscaped(Writer writer, String s) throws IOException { | ||
| if (writer instanceof XmlUtf8Writer xmlWriter) { | ||
| xmlWriter.writeEscaped(s); | ||
| return; | ||
| } | ||
| int len = s.length(); | ||
| int start = 0; | ||
| for (int i = 0; i < len; i++) { | ||
| char c = s.charAt(i); | ||
| switch (c) { | ||
| case '&' -> { | ||
| if (start < i) writer.write(s, start, i - start); | ||
| writer.write("&"); | ||
| start = i + 1; | ||
| } | ||
| case '<' -> { | ||
| if (start < i) writer.write(s, start, i - start); | ||
| writer.write("<"); | ||
| start = i + 1; | ||
| } | ||
| case '>' -> { | ||
| if (start < i) writer.write(s, start, i - start); | ||
| writer.write(">"); | ||
| start = i + 1; | ||
| } | ||
| case '"' -> { | ||
| if (start < i) writer.write(s, start, i - start); | ||
| writer.write("""); | ||
| start = i + 1; | ||
| } | ||
| default -> { | ||
| if (Character.isHighSurrogate(c)) { | ||
| int j = i + 1; | ||
| if (j < len && Character.isLowSurrogate(s.charAt(j))) { | ||
| i = j; | ||
| } else { | ||
| if (start < i) writer.write(s, start, i - start); | ||
| writer.write("?"); | ||
| start = j; | ||
| } | ||
| } else if (Character.isLowSurrogate(c) || !validXmlChar(c)) { | ||
| if (start < i) writer.write(s, start, i - start); | ||
| writer.write("?"); | ||
| start = i + 1; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if (start < len) writer.write(s, start, len - start); | ||
| } | ||
| } |
Oops, something went wrong.
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.
This class is a bit to low-level.