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.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@ -39,7 +38,7 @@ import java.util.Map;
public final class MultiFormatReader implements Reader { public final class MultiFormatReader implements Reader {
private Map<DecodeHintType,?> hints; 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 * 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 { public Result decodeWithState(BinaryBitmap image) throws NotFoundException {
// Make sure to set up the default state so we don't crash // Make sure to set up the default state so we don't crash
if (readers.isEmpty()) { if (readers == null) {
setHints(null); setHints(null);
} }
return decodeInternal(image); return decodeInternal(image);
@ -99,7 +98,7 @@ public final class MultiFormatReader implements Reader {
boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER); boolean tryHarder = hints != null && hints.containsKey(DecodeHintType.TRY_HARDER);
Collection<BarcodeFormat> formats = Collection<BarcodeFormat> formats =
hints == null ? null : (Collection<BarcodeFormat>) hints.get(DecodeHintType.POSSIBLE_FORMATS); hints == null ? null : (Collection<BarcodeFormat>) hints.get(DecodeHintType.POSSIBLE_FORMATS);
readers.clear(); Collection<Reader> readers = new ArrayList<Reader>();
if (formats != null) { if (formats != null) {
boolean addOneDReader = boolean addOneDReader =
formats.contains(BarcodeFormat.UPC_A) || formats.contains(BarcodeFormat.UPC_A) ||
@ -152,16 +151,20 @@ public final class MultiFormatReader implements Reader {
readers.add(new MultiFormatOneDReader(hints)); readers.add(new MultiFormatOneDReader(hints));
} }
} }
this.readers = readers.toArray(new Reader[readers.size()]);
} }
@Override @Override
public void reset() { public void reset() {
if (readers != null) {
for (Reader reader : readers) { for (Reader reader : readers) {
reader.reset(); reader.reset();
} }
} }
}
private Result decodeInternal(BinaryBitmap image) throws NotFoundException { private Result decodeInternal(BinaryBitmap image) throws NotFoundException {
if (readers != null) {
for (Reader reader : readers) { for (Reader reader : readers) {
try { try {
return reader.decode(image, hints); return reader.decode(image, hints);
@ -169,6 +172,7 @@ public final class MultiFormatReader implements Reader {
// continue // continue
} }
} }
}
throw NotFoundException.getNotFoundInstance(); throw NotFoundException.getNotFoundInstance();
} }

View file

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

View file

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