Avoid some allocation hotspots by reusing arrays

git-svn-id: https://zxing.googlecode.com/svn/trunk@2049 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2011-11-23 22:18:45 +00:00
parent d0850c3baf
commit 35cf29b720
4 changed files with 42 additions and 22 deletions

View file

@ -96,7 +96,8 @@ public final class Code39Reader extends OneDReader {
public Result decodeRow(int rowNumber, BitArray row, Map<DecodeHintType,?> hints)
throws NotFoundException, ChecksumException, FormatException {
int[] start = findAsteriskPattern(row);
int[] counters = new int[9];
int[] start = findAsteriskPattern(row, counters);
int nextStart = start[1];
int end = row.getSize();
@ -106,7 +107,6 @@ public final class Code39Reader extends OneDReader {
}
StringBuilder result = new StringBuilder(20);
int[] counters = new int[9];
char decodedChar;
int lastStart;
do {
@ -176,7 +176,7 @@ public final class Code39Reader extends OneDReader {
}
private static int[] findAsteriskPattern(BitArray row) throws NotFoundException {
private static int[] findAsteriskPattern(BitArray row, int[] counters) throws NotFoundException {
int width = row.getSize();
int rowOffset = 0;
while (rowOffset < width) {
@ -187,7 +187,6 @@ public final class Code39Reader extends OneDReader {
}
int counterPosition = 0;
int[] counters = new int[9];
int patternStart = rowOffset;
boolean isWhite = false;
int patternLength = counters.length;

View file

@ -28,6 +28,7 @@ import com.google.zxing.ResultMetadataType;
import com.google.zxing.ResultPoint;
import com.google.zxing.common.BitArray;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.Map;
@ -193,9 +194,7 @@ public abstract class OneDReader implements Reader {
int start,
int[] counters) throws NotFoundException {
int numCounters = counters.length;
for (int i = 0; i < numCounters; i++) {
counters[i] = 0;
}
Arrays.fill(counters, 0, numCounters, 0);
int end = row.getSize();
if (start >= end) {
throw NotFoundException.getNotFoundInstance();

View file

@ -28,6 +28,7 @@ import com.google.zxing.ResultPoint;
import com.google.zxing.ResultPointCallback;
import com.google.zxing.common.BitArray;
import java.util.Arrays;
import java.util.Map;
/**
@ -104,8 +105,10 @@ public abstract class UPCEANReader extends OneDReader {
boolean foundStart = false;
int[] startRange = null;
int nextStart = 0;
int[] counters = new int[START_END_PATTERN.length];
while (!foundStart) {
startRange = findGuardPattern(row, nextStart, false, START_END_PATTERN);
Arrays.fill(counters, 0, START_END_PATTERN.length, 0);
startRange = findGuardPattern(row, nextStart, false, START_END_PATTERN, counters);
int start = startRange[0];
nextStart = startRange[1];
// Make sure there is a quiet zone at least as big as the start pattern before the barcode.
@ -249,6 +252,13 @@ public abstract class UPCEANReader extends OneDReader {
return findGuardPattern(row, endStart, false, START_END_PATTERN);
}
static int[] findGuardPattern(BitArray row,
int rowOffset,
boolean whiteFirst,
int[] pattern) throws NotFoundException {
return findGuardPattern(row, rowOffset, whiteFirst, pattern, new int[pattern.length]);
}
/**
* @param row row of black/white values to search
* @param rowOffset position to start search
@ -256,15 +266,16 @@ public abstract class UPCEANReader extends OneDReader {
* pixel counts, otherwise, it is interpreted as black/white/black/...
* @param pattern pattern of counts of number of black and white pixels that are being
* searched for as a pattern
* @param counters array of counters, as long as pattern, to re-use
* @return start/end horizontal offset of guard pattern, as an array of two ints
* @throws NotFoundException if pattern is not found
*/
static int[] findGuardPattern(BitArray row,
int rowOffset,
boolean whiteFirst,
int[] pattern) throws NotFoundException {
int[] pattern,
int[] counters) throws NotFoundException {
int patternLength = pattern.length;
int[] counters = new int[patternLength];
int width = row.getSize();
boolean isWhite = false;
while (rowOffset < width) {

View file

@ -24,6 +24,7 @@ import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.DetectorResult;
import com.google.zxing.common.GridSampler;
import java.util.Arrays;
import java.util.Map;
/**
@ -137,9 +138,11 @@ public final class Detector {
ResultPoint[] result = new ResultPoint[8];
boolean found = false;
int[] counters = new int[START_PATTERN.length];
// Top Left
for (int i = 0; i < height; i++) {
int[] loc = findGuardPattern(matrix, 0, i, width, false, START_PATTERN);
int[] loc = findGuardPattern(matrix, 0, i, width, false, START_PATTERN, counters);
if (loc != null) {
result[0] = new ResultPoint(loc[0], i);
result[4] = new ResultPoint(loc[1], i);
@ -151,7 +154,7 @@ public final class Detector {
if (found) { // Found the Top Left vertex
found = false;
for (int i = height - 1; i > 0; i--) {
int[] loc = findGuardPattern(matrix, 0, i, width, false, START_PATTERN);
int[] loc = findGuardPattern(matrix, 0, i, width, false, START_PATTERN, counters);
if (loc != null) {
result[1] = new ResultPoint(loc[0], i);
result[5] = new ResultPoint(loc[1], i);
@ -160,11 +163,14 @@ public final class Detector {
}
}
}
counters = new int[STOP_PATTERN.length];
// Top right
if (found) { // Found the Bottom Left vertex
found = false;
for (int i = 0; i < height; i++) {
int[] loc = findGuardPattern(matrix, 0, i, width, false, STOP_PATTERN);
int[] loc = findGuardPattern(matrix, 0, i, width, false, STOP_PATTERN, counters);
if (loc != null) {
result[2] = new ResultPoint(loc[1], i);
result[6] = new ResultPoint(loc[0], i);
@ -177,7 +183,7 @@ public final class Detector {
if (found) { // Found the Top right vertex
found = false;
for (int i = height - 1; i > 0; i--) {
int[] loc = findGuardPattern(matrix, 0, i, width, false, STOP_PATTERN);
int[] loc = findGuardPattern(matrix, 0, i, width, false, STOP_PATTERN, counters);
if (loc != null) {
result[3] = new ResultPoint(loc[1], i);
result[7] = new ResultPoint(loc[0], i);
@ -216,9 +222,11 @@ public final class Detector {
ResultPoint[] result = new ResultPoint[8];
boolean found = false;
int[] counters = new int[START_PATTERN_REVERSE.length];
// Top Left
for (int i = height - 1; i > 0; i--) {
int[] loc = findGuardPattern(matrix, halfWidth, i, halfWidth, true, START_PATTERN_REVERSE);
int[] loc = findGuardPattern(matrix, halfWidth, i, halfWidth, true, START_PATTERN_REVERSE, counters);
if (loc != null) {
result[0] = new ResultPoint(loc[1], i);
result[4] = new ResultPoint(loc[0], i);
@ -230,7 +238,7 @@ public final class Detector {
if (found) { // Found the Top Left vertex
found = false;
for (int i = 0; i < height; i++) {
int[] loc = findGuardPattern(matrix, halfWidth, i, halfWidth, true, START_PATTERN_REVERSE);
int[] loc = findGuardPattern(matrix, halfWidth, i, halfWidth, true, START_PATTERN_REVERSE, counters);
if (loc != null) {
result[1] = new ResultPoint(loc[1], i);
result[5] = new ResultPoint(loc[0], i);
@ -239,11 +247,14 @@ public final class Detector {
}
}
}
counters = new int[STOP_PATTERN_REVERSE.length];
// Top Right
if (found) { // Found the Bottom Left vertex
found = false;
for (int i = height - 1; i > 0; i--) {
int[] loc = findGuardPattern(matrix, 0, i, halfWidth, false, STOP_PATTERN_REVERSE);
int[] loc = findGuardPattern(matrix, 0, i, halfWidth, false, STOP_PATTERN_REVERSE, counters);
if (loc != null) {
result[2] = new ResultPoint(loc[0], i);
result[6] = new ResultPoint(loc[1], i);
@ -256,7 +267,7 @@ public final class Detector {
if (found) { // Found the Top Right vertex
found = false;
for (int i = 0; i < height; i++) {
int[] loc = findGuardPattern(matrix, 0, i, halfWidth, false, STOP_PATTERN_REVERSE);
int[] loc = findGuardPattern(matrix, 0, i, halfWidth, false, STOP_PATTERN_REVERSE, counters);
if (loc != null) {
result[3] = new ResultPoint(loc[0], i);
result[7] = new ResultPoint(loc[1], i);
@ -411,6 +422,7 @@ public final class Detector {
* @param width the number of pixels to search on this row
* @param pattern pattern of counts of number of black and white pixels that are
* being searched for as a pattern
* @param counters array of counters, as long as pattern, to re-use
* @return start/end horizontal offset of guard pattern, as an array of two ints.
*/
private static int[] findGuardPattern(BitMatrix matrix,
@ -418,11 +430,10 @@ public final class Detector {
int row,
int width,
boolean whiteFirst,
int[] pattern) {
int[] pattern,
int[] counters) {
Arrays.fill(counters, 0, counters.length, 0);
int patternLength = pattern.length;
// TODO: Find a way to cache this array, as this method is called hundreds of times
// per image, and we want to allocate as seldom as possible.
int[] counters = new int[patternLength];
boolean isWhite = whiteFirst;
int counterPosition = 0;