comparison src/org/tmatesoft/hg/internal/DataAccess.java @ 681:4f93bbc73b64

Do not instantiate thousands of small arrays(numerous readInt/readLong calls)
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Sun, 21 Jul 2013 17:48:05 +0200
parents e1b29756f901
children
comparison
equal deleted inserted replaced
680:58a6900f845d 681:4f93bbc73b64
27 * 27 *
28 * @author Artem Tikhomirov 28 * @author Artem Tikhomirov
29 * @author TMate Software Ltd. 29 * @author TMate Software Ltd.
30 */ 30 */
31 public class DataAccess { 31 public class DataAccess {
32 private byte[] longBuffer;
33
32 public boolean isEmpty() throws IOException { 34 public boolean isEmpty() throws IOException {
33 return true; 35 return true;
34 } 36 }
35 // IOException due to few subclasses that have non-trivial length() operation 37 // IOException due to few subclasses that have non-trivial length() operation
36 // long length and offset are needed only in RevlogStream, makes no sense elsewhere 38 // long length and offset are needed only in RevlogStream, makes no sense elsewhere
76 // shall be called once this object no longer needed 78 // shall be called once this object no longer needed
77 public void done() { 79 public void done() {
78 // no-op in this empty implementation 80 // no-op in this empty implementation
79 } 81 }
80 public int readInt() throws IOException { 82 public int readInt() throws IOException {
81 byte[] b = new byte[4]; 83 if (longBuffer == null) {
84 longBuffer = new byte[8];
85 }
86 byte[] b = longBuffer;
82 readBytes(b, 0, 4); 87 readBytes(b, 0, 4);
83 return b[0] << 24 | (b[1] & 0xFF) << 16 | (b[2] & 0xFF) << 8 | (b[3] & 0xFF); 88 return b[0] << 24 | (b[1] & 0xFF) << 16 | (b[2] & 0xFF) << 8 | (b[3] & 0xFF);
84 } 89 }
85 90
86 /** 91 /**
87 * Read 8 bytes as long value, big-endian. 92 * Read 8 bytes as long value, big-endian.
88 */ 93 */
89 public long readLong() throws IOException { 94 public long readLong() throws IOException {
90 byte[] b = new byte[8]; 95 if (longBuffer == null) {
96 longBuffer = new byte[8];
97 }
98 byte[] b = longBuffer;
91 readBytes(b, 0, 8); 99 readBytes(b, 0, 8);
92 int i1 = b[0] << 24 | (b[1] & 0xFF) << 16 | (b[2] & 0xFF) << 8 | (b[3] & 0xFF); 100 int i1 = b[0] << 24 | (b[1] & 0xFF) << 16 | (b[2] & 0xFF) << 8 | (b[3] & 0xFF);
93 int i2 = b[4] << 24 | (b[5] & 0xFF) << 16 | (b[6] & 0xFF) << 8 | (b[7] & 0xFF); 101 int i2 = b[4] << 24 | (b[5] & 0xFF) << 16 | (b[6] & 0xFF) << 8 | (b[7] & 0xFF);
94 return ((long) i1) << 32 | ((long) i2 & 0xFFFFFFFFl); 102 return ((long) i1) << 32 | ((long) i2 & 0xFFFFFFFFl);
95 } 103 }