Surprising, but these small changes to avoid allocation of a foreach loop iterator save 1-2% runtime.

git-svn-id: https://zxing.googlecode.com/svn/trunk@2047 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
srowen 2011-11-22 21:52:47 +00:00
parent ab0ec7ebc8
commit 39c51ad06b
3 changed files with 21 additions and 17 deletions

View file

@ -25,7 +25,6 @@ import com.google.zxing.qrcode.QRCodeReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
@ -39,7 +38,7 @@ import java.util.Map;
public final class MultiFormatReader implements Reader {
private Map<DecodeHintType,?> hints;
private final List<Reader> readers = new ArrayList<Reader>();
private Reader[] readers;
/**
* This version of decode honors the intent of Reader.decode(BinaryBitmap) in that it
@ -80,7 +79,7 @@ public final class MultiFormatReader implements Reader {
*/
public Result decodeWithState(BinaryBitmap image) throws NotFoundException {
// Make sure to set up the default state so we don't crash
if (readers.isEmpty()) {
if (readers == null) {
setHints(null);
}
return decodeInternal(image);
@ -99,7 +98,7 @@ public final class MultiFormatReader implements Reader {
boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
Collection<BarcodeFormat> formats =
hints == null ? null : (Collection<BarcodeFormat>) hints.get(DecodeHintType.POSSIBLE_FORMATS);
readers.clear();
Collection<Reader> readers = new ArrayList<Reader>();
if (formats != null) {
boolean addOneDReader =
formats.contains(BarcodeFormat.UPC_A) ||
@ -152,21 +151,26 @@ public final class MultiFormatReader implements Reader {
readers.add(new MultiFormatOneDReader(hints));
}
}
this.readers = readers.toArray(new Reader[readers.size()]);
}
@Override
public void reset() {
for (Reader reader : readers) {
reader.reset();
if (readers != null) {
for (Reader reader : readers) {
reader.reset();
}
}
}
private Result decodeInternal(BinaryBitmap image) throws NotFoundException {
for (Reader reader : readers) {
try {
return reader.decode(image, hints);
} catch (ReaderException re) {
// continue
if (readers != null) {
for (Reader reader : readers) {
try {
return reader.decode(image, hints);
} catch (ReaderException re) {
// continue
}
}
}
throw NotFoundException.getNotFoundInstance();

View file

@ -28,7 +28,6 @@ import com.google.zxing.oned.rss.expanded.RSSExpandedReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
@ -37,14 +36,14 @@ import java.util.Map;
*/
public final class MultiFormatOneDReader extends OneDReader {
private final List<OneDReader> readers;
private final OneDReader[] readers;
public MultiFormatOneDReader(Map<DecodeHintType,?> hints) {
Collection<BarcodeFormat> possibleFormats = hints == null ? null :
(Collection<BarcodeFormat>) hints.get(DecodeHintType.POSSIBLE_FORMATS);
boolean useCode39CheckDigit = hints != null &&
hints.get(DecodeHintType.ASSUME_CODE_39_CHECK_DIGIT) != null;
readers = new ArrayList<OneDReader>();
Collection<OneDReader> readers = new ArrayList<OneDReader>();
if (possibleFormats != null) {
if (possibleFormats.contains(BarcodeFormat.EAN_13) ||
possibleFormats.contains(BarcodeFormat.UPC_A) ||
@ -84,6 +83,7 @@ public final class MultiFormatOneDReader extends OneDReader {
readers.add(new RSS14Reader());
readers.add(new RSSExpandedReader());
}
this.readers = readers.toArray(new OneDReader[readers.size()]);
}
@Override

View file

@ -26,7 +26,6 @@ import com.google.zxing.common.BitArray;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
@ -38,12 +37,12 @@ import java.util.Map;
*/
public final class MultiFormatUPCEANReader extends OneDReader {
private final List<UPCEANReader> readers;
private final UPCEANReader[] readers;
public MultiFormatUPCEANReader(Map<DecodeHintType,?> hints) {
Collection<BarcodeFormat> possibleFormats = hints == null ? null :
(Collection<BarcodeFormat>) hints.get(DecodeHintType.POSSIBLE_FORMATS);
readers = new ArrayList<UPCEANReader>();
Collection<UPCEANReader> readers = new ArrayList<UPCEANReader>();
if (possibleFormats != null) {
if (possibleFormats.contains(BarcodeFormat.EAN_13)) {
readers.add(new EAN13Reader());
@ -63,6 +62,7 @@ public final class MultiFormatUPCEANReader extends OneDReader {
readers.add(new EAN8Reader());
readers.add(new UPCEReader());
}
this.readers = readers.toArray(new UPCEANReader[readers.size()]);
}
@Override