Skip to content

Commit 96856ee

Browse files
committed
PDFBOX-5998: avoid unnecessary map lookups based on a proposal by Axel Howind
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1925396 13f79535-47bb-0310-9956-ffa450edef68
1 parent 25c1ac8 commit 96856ee

File tree

8 files changed

+35
-57
lines changed

8 files changed

+35
-57
lines changed

fontbox/src/main/java/org/apache/fontbox/cff/CFFCIDFont.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ private int getDefaultWidthX(int gid)
179179
{
180180
return 1000;
181181
}
182-
Map<String, Object> privDict = this.privateDictionaries.get(fdArrayIndex);
183-
return privDict.containsKey("defaultWidthX") ? ((Number)privDict.get("defaultWidthX")).intValue() : 1000;
182+
Object privDictValue = this.privateDictionaries.get(fdArrayIndex).get("defaultWidthX");
183+
return privDictValue instanceof Number ? ((Number) privDictValue).intValue() : 1000;
184184
}
185185

186186
/**
@@ -195,8 +195,8 @@ private int getNominalWidthX(int gid)
195195
{
196196
return 0;
197197
}
198-
Map<String, Object> privDict = this.privateDictionaries.get(fdArrayIndex);
199-
return privDict.containsKey("nominalWidthX") ? ((Number)privDict.get("nominalWidthX")).intValue() : 0;
198+
Object privDictValue = this.privateDictionaries.get(fdArrayIndex).get("nominalWidthX");
199+
return privDictValue instanceof Number ? ((Number) privDictValue).intValue() : 0;
200200
}
201201

202202
/**

fontbox/src/main/java/org/apache/fontbox/cff/CFFParser.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -674,13 +674,13 @@ else if (charStringsIndex.length > 0)
674674

675675
parseCIDFontDicts(input, topDict, (CFFCIDFont) font, numEntries);
676676

677-
List<Number> privMatrix = null;
678677
List<Map<String, Object>> fontDicts = ((CFFCIDFont) font).getFontDicts();
679-
if (!fontDicts.isEmpty() && fontDicts.get(0).containsKey("FontMatrix"))
678+
List<Number> privMatrix = null;
679+
if (!fontDicts.isEmpty())
680680
{
681-
privMatrix = (List<Number>) fontDicts.get(0).get("FontMatrix");
681+
privMatrix = (List<Number>) fontDicts.get(0).getOrDefault("FontMatrix", null);
682682
}
683-
// some malformed fonts have FontMatrix in their Font DICT, see PDFBOX-2495
683+
// some malformed fonts have FontMatrix in their Font DICT, seePDFBOX-2495
684684
List<Number> matrix = topDict.getArray("FontMatrix", null);
685685
if (matrix == null)
686686
{

fontbox/src/main/java/org/apache/fontbox/cmap/CMap.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,12 @@ public int toCID(byte[] code)
242242
return 0;
243243
}
244244
Integer cid = null;
245-
if (codeToCid.containsKey(code.length))
246-
{
247-
cid = codeToCid.get(code.length).get(toInt(code));
248-
}
249-
if (cid == null)
245+
Map<Integer, Integer> codeToCidMap = codeToCid.get(code.length);
246+
if (codeToCidMap != null)
250247
{
251-
cid = toCIDFromRanges(code);
248+
cid = codeToCidMap.get(toInt(code));
252249
}
253-
return cid;
250+
return cid != null ? cid : toCIDFromRanges(code);
254251
}
255252

256253
/**
@@ -294,9 +291,10 @@ public int toCID(int code, int length)
294291
return 0;
295292
}
296293
Integer cid = null;
297-
if (codeToCid.containsKey(length))
294+
Map<Integer, Integer> codeToCidMap = codeToCid.get(length);
295+
if (codeToCidMap != null)
298296
{
299-
cid = codeToCid.get(length).get(code);
297+
cid = codeToCidMap.get(code);
300298
}
301299
return cid != null ? cid : toCIDFromRanges(code, length);
302300
}

fontbox/src/main/java/org/apache/fontbox/ttf/VerticalOriginTable.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,6 @@ public float getVersion()
9191
*/
9292
public int getOriginY(int gid)
9393
{
94-
if (origins.containsKey(gid))
95-
{
96-
return origins.get(gid);
97-
}
98-
else
99-
{
100-
return defaultVertOriginY;
101-
}
94+
return origins.getOrDefault(gid, defaultVertOriginY);
10295
}
10396
}

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/documentinterchange/logicalstructure/PDStructureElement.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -603,14 +603,10 @@ public void setActualText(String actualText)
603603
public String getStandardStructureType()
604604
{
605605
String type = this.getStructureType();
606-
Map<String,Object> roleMap = getRoleMap();
607-
if (roleMap.containsKey(type))
606+
Object mappedValue = getRoleMap().get(type);
607+
if (mappedValue instanceof String)
608608
{
609-
Object mappedValue = getRoleMap().get(type);
610-
if (mappedValue instanceof String)
611-
{
612-
type = (String)mappedValue;
613-
}
609+
type = (String) mappedValue;
614610
}
615611
return type;
616612
}

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDTrueTypeFont.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@ public class PDTrueTypeFont extends PDSimpleFont implements PDVectorFont
7878
{
7979
MacOSRomanEncoding.INSTANCE.getCodeToNameMap().forEach((key, value) ->
8080
{
81-
if (!INVERTED_MACOS_ROMAN.containsKey(value))
82-
{
83-
INVERTED_MACOS_ROMAN.put(value, key);
84-
}
81+
INVERTED_MACOS_ROMAN.putIfAbsent(value, key);
8582
});
8683
}
8784

@@ -93,7 +90,7 @@ public class PDTrueTypeFont extends PDSimpleFont implements PDVectorFont
9390
private CmapSubtable cmapWinSymbol = null;
9491
private CmapSubtable cmapMacRoman = null;
9592
private boolean cmapInitialized = false;
96-
private Map<Integer, Integer> gidToCode; // for embedding
93+
private final Map<Integer, Integer> gidToCode = new HashMap<>(); // for embedding
9794
private BoundingBox fontBBox;
9895

9996
/**
@@ -454,19 +451,15 @@ protected byte[] encode(int unicode) throws IOException
454451
*/
455452
protected Map<Integer, Integer> getGIDToCode() throws IOException
456453
{
457-
if (gidToCode != null)
454+
if (!gidToCode.isEmpty())
458455
{
459456
return gidToCode;
460457
}
461458

462-
gidToCode = new HashMap<>();
463459
for (int code = 0; code <= 255; code++)
464460
{
465461
int gid = codeToGID(code);
466-
if (!gidToCode.containsKey(gid))
467-
{
468-
gidToCode.put(gid, code);
469-
}
462+
gidToCode.putIfAbsent(gid, code);
470463
}
471464
return gidToCode;
472465
}

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/encoding/GlyphList.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,6 @@ private void loadList(InputStream input) throws IOException
136136
String name = parts[0];
137137
String[] unicodeList = parts[1].split(" ");
138138

139-
if (nameToUnicode.containsKey(name))
140-
{
141-
LOG.warn("duplicate value for {} -> {} {}", name, parts[1],
142-
nameToUnicode.get(name));
143-
}
144-
145139
int[] codePoints = new int[unicodeList.length];
146140
int index = 0;
147141
for (String hex : unicodeList)
@@ -151,8 +145,11 @@ private void loadList(InputStream input) throws IOException
151145
String string = new String(codePoints, 0 , codePoints.length);
152146

153147
// forward mapping
154-
nameToUnicode.put(name, string);
155-
148+
String oldMapping = nameToUnicode.put(name, string);
149+
if (oldMapping != null)
150+
{
151+
LOG.warn("duplicate value for {} -> {} {}", name, parts[1], oldMapping);
152+
}
156153
// reverse mapping
157154
// PDFBOX-3884: take the various standard encodings as canonical,
158155
// e.g. tilde over ilde
@@ -162,10 +159,14 @@ private void loadList(InputStream input) throws IOException
162159
MacExpertEncoding.INSTANCE.contains(name) ||
163160
SymbolEncoding.INSTANCE.contains(name) ||
164161
ZapfDingbatsEncoding.INSTANCE.contains(name);
165-
if (!unicodeToName.containsKey(string) || forceOverride)
162+
if (forceOverride)
166163
{
167164
unicodeToName.put(string, name);
168165
}
166+
else
167+
{
168+
unicodeToName.putIfAbsent(string, name);
169+
}
169170
}
170171
}
171172
}

pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/state/PDExtendedGraphicsState.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -586,11 +586,8 @@ public void setBlendMode(BlendMode bm)
586586
*/
587587
public PDSoftMask getSoftMask()
588588
{
589-
if (!dict.containsKey(COSName.SMASK))
590-
{
591-
return null;
592-
}
593-
return PDSoftMask.create(dict.getDictionaryObject(COSName.SMASK));
589+
COSBase smask = dict.getDictionaryObject(COSName.SMASK);
590+
return smask == null ? null : PDSoftMask.create(smask);
594591
}
595592

596593
/**

0 commit comments

Comments
 (0)