Github user vrozov commented on a diff in the pull request:
https://github.com/apache/drill/pull/1144#discussion_r179226227
--- Diff: exec/memory/base/src/main/java/io/netty/buffer/DrillBuf.java ---
@@ -777,23 +778,20 @@ public int getActualMemoryConsumed() {
* @return A hex dump in a String.
*/
public String toHexString(final int start, final int length) {
- final int roundedStart = (start / LOG_BYTES_PER_ROW) *
LOG_BYTES_PER_ROW;
-
- final StringBuilder sb = new StringBuilder("buffer byte dump\n");
- int index = roundedStart;
- for (int nLogged = 0; nLogged < length; nLogged += LOG_BYTES_PER_ROW) {
- sb.append(String.format(" [%05d-%05d]", index, index +
LOG_BYTES_PER_ROW - 1));
- for (int i = 0; i < LOG_BYTES_PER_ROW; ++i) {
- try {
- final byte b = getByte(index++);
- sb.append(String.format(" 0x%02x", b));
- } catch (IndexOutOfBoundsException ioob) {
- sb.append(" <ioob>");
- }
+ Preconditions.checkArgument(start >= 0);
+ final StringBuilder sb = new StringBuilder("buffer byte dump");
+ final int end = Math.min(length, this.length - start);
+ for (int i = 0; i < end; i++) {
+ if (i % LOG_BYTES_PER_ROW == 0) {
+ sb.append(String.format("%n [%05d-%05d]", i + start, Math.min(i +
LOG_BYTES_PER_ROW - 1, end - 1) + start));
}
- sb.append('\n');
+ byte b = _getByte(i + start);
+ sb.append(" 0x").append(HEX_CHAR[b >> 4]).append(HEX_CHAR[b & 0x0F]);
--- End diff --
The goal is to avoid new String allocation for every output byte inside
DrillBuf.
---