Skip to content

Commit ebf023f

Browse files
[MOD] Types: unify sequence type caches
1 parent b832f6b commit ebf023f

9 files changed

Lines changed: 54 additions & 41 deletions

File tree

‎basex-core/src/main/java/org/basex/query/value/type/BasicType.java‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -943,8 +943,8 @@ public QNm read(final DataInput in, final QueryContext qc) throws IOException {
943943

944944
/** Pre/post values (pre, post << 8). */
945945
private short prePost;
946-
/** Sequence types (can be {@code null}; lazy instantiation). */
947-
private EnumMap<Occ, SeqType> seqTypes;
946+
/** Sequence types. */
947+
private final SeqType[] seqTypes = SeqType.cache(this);
948948
/** QName (can be {@code null}; lazy instantiation). */
949949
private QNm qnm;
950950

@@ -1020,9 +1020,7 @@ public Item read(final DataInput in, final QueryContext qc) throws IOException,
10201020

10211021
@Override
10221022
public final SeqType seqType(final Occ occ) {
1023-
// cannot be instantiated statically due to circular dependencies
1024-
if(seqTypes == null) seqTypes = new EnumMap<>(Occ.class);
1025-
return seqTypes.computeIfAbsent(occ, o -> new SeqType(this, o));
1023+
return SeqType.get(seqTypes, this, occ);
10261024
}
10271025

10281026
/**

‎basex-core/src/main/java/org/basex/query/value/type/ChoiceItemType.java‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public final class ChoiceItemType implements Type {
2222
/** Common ancestor type. */
2323
private final Type union;
2424

25-
/** Sequence types (can be {@code null}; lazy instantiation). */
26-
private EnumMap<Occ, SeqType> seqTypes;
25+
/** Sequence types. */
26+
private final SeqType[] seqTypes = SeqType.cache(this);
2727

2828
/**
2929
* Constructor.
@@ -79,9 +79,7 @@ public Item read(final DataInput in, final QueryContext qc) {
7979

8080
@Override
8181
public SeqType seqType(final Occ occ) {
82-
// cannot be instantiated statically due to circular dependencies
83-
if(seqTypes == null) seqTypes = new EnumMap<>(Occ.class);
84-
return seqTypes.computeIfAbsent(occ, o -> new SeqType(this, o));
82+
return SeqType.get(seqTypes, this, occ);
8583
}
8684

8785
@Override

‎basex-core/src/main/java/org/basex/query/value/type/EnumType.java‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import static org.basex.query.QueryError.*;
44

5-
import java.util.*;
6-
75
import org.basex.io.in.*;
86
import org.basex.query.*;
97
import org.basex.query.value.*;
@@ -22,8 +20,8 @@ public final class EnumType implements Type {
2220
/** The enumeration values (at least one). */
2321
private final TokenSet values;
2422

25-
/** Sequence types (can be {@code null}; lazy instantiation). */
26-
private EnumMap<Occ, SeqType> seqTypes;
23+
/** Sequence types. */
24+
private final SeqType[] seqTypes = SeqType.cache(this);
2725

2826
/**
2927
* Constructor.
@@ -80,9 +78,7 @@ public Item read(final DataInput in, final QueryContext qc) {
8078

8179
@Override
8280
public SeqType seqType(final Occ occ) {
83-
// cannot be instantiated statically due to circular dependencies
84-
if(seqTypes == null) seqTypes = new EnumMap<>(Occ.class);
85-
return seqTypes.computeIfAbsent(occ, o -> new SeqType(this, o));
81+
return SeqType.get(seqTypes, this, occ);
8682
}
8783

8884
@Override

‎basex-core/src/main/java/org/basex/query/value/type/FType.java‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import static org.basex.query.QueryError.*;
44

5-
import java.util.*;
6-
75
import org.basex.query.*;
86
import org.basex.query.value.item.*;
97
import org.basex.util.*;
@@ -19,8 +17,8 @@ public abstract class FType implements Type {
1917
/** Any function placeholder string. */
2018
static final String[] WILDCARD = { "*" };
2119

22-
/** Sequence types (can be {@code null}; lazy instantiation). */
23-
private EnumMap<Occ, SeqType> seqTypes;
20+
/** Sequence types. */
21+
private final SeqType[] seqTypes = SeqType.cache(this);
2422

2523
@Override
2624
public final boolean isNumber() {
@@ -49,9 +47,7 @@ public final boolean isSortable() {
4947

5048
@Override
5149
public final SeqType seqType(final Occ occ) {
52-
// cannot be instantiated statically due to circular dependencies
53-
if(seqTypes == null) seqTypes = new EnumMap<>(Occ.class);
54-
return seqTypes.computeIfAbsent(occ, o -> new SeqType(this, o));
50+
return SeqType.get(seqTypes, this, occ);
5551
}
5652

5753
@Override

‎basex-core/src/main/java/org/basex/query/value/type/ListType.java‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import static org.basex.util.Token.*;
66
import static org.basex.util.Token.normalize;
77

8-
import java.util.*;
9-
108
import org.basex.io.in.*;
119
import org.basex.query.*;
1210
import org.basex.query.value.*;
@@ -32,8 +30,8 @@ public enum ListType implements Type {
3230
/** Atom Type. */
3331
private final BasicType type;
3432

35-
/** Sequence types (can be {@code null}; lazy instantiation). */
36-
private EnumMap<Occ, SeqType> seqTypes;
33+
/** Sequence types. */
34+
private final SeqType[] seqTypes = SeqType.cache(this);
3735

3836
/**
3937
* Constructor.
@@ -118,9 +116,7 @@ public Item read(final DataInput in, final QueryContext qc) {
118116

119117
@Override
120118
public SeqType seqType(final Occ occ) {
121-
// cannot be instantiated statically due to circular dependencies
122-
if(seqTypes == null) seqTypes = new EnumMap<>(Occ.class);
123-
return seqTypes.computeIfAbsent(occ, o -> new SeqType(this, o));
119+
return SeqType.get(seqTypes, this, occ);
124120
}
125121

126122
@Override

‎basex-core/src/main/java/org/basex/query/value/type/NodeType.java‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public final class NodeType implements Type {
5353
/** Node test (can be {@code null}). */
5454
public final Test test;
5555

56-
/** Sequence types (can be {@code null}; lazy instantiation). */
57-
private EnumMap<Occ, SeqType> seqTypes;
56+
/** Sequence types. */
57+
private final SeqType[] seqTypes = SeqType.cache(this);
5858

5959
/**
6060
* Constructor.
@@ -145,9 +145,7 @@ public GNode read(final DataInput in, final QueryContext qc) throws IOException,
145145

146146
@Override
147147
public SeqType seqType(final Occ occ) {
148-
// cannot be instantiated statically due to circular dependencies
149-
if(seqTypes == null) seqTypes = new EnumMap<>(Occ.class);
150-
return seqTypes.computeIfAbsent(occ, o -> new SeqType(this, o));
148+
return SeqType.get(seqTypes, this, occ);
151149
}
152150

153151
@Override

‎basex-core/src/main/java/org/basex/query/value/type/SeqType.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
* @author Christian Gruen
2525
*/
2626
public final class SeqType {
27+
/** Number of cached sequence types (all occurrence indicators but {@link Occ#ZERO}). */
28+
private static final int OCCS = Occ.values().length - 1;
29+
2730
/** Item type. */
2831
public final Type type;
2932
/** Occurrence indicator. */
@@ -53,6 +56,35 @@ public static SeqType get(final Type type, final Occ occ) {
5356
return occ == ZERO ? Types.EMPTY_SEQUENCE_Z : type.seqType(occ);
5457
}
5558

59+
/**
60+
* Creates a sequence type cache for the specified type.
61+
* @param type item type
62+
* @return cache
63+
*/
64+
static SeqType[] cache(final Type type) {
65+
final SeqType[] cache = new SeqType[OCCS];
66+
cache[EXACTLY_ONE.ordinal() - 1] = new SeqType(type, EXACTLY_ONE);
67+
return cache;
68+
}
69+
70+
/**
71+
* Returns a cached sequence type.
72+
* @param cache sequence type cache
73+
* @param type item type
74+
* @param occ occurrence indicator (no {@link Occ#ZERO})
75+
* @return sequence type
76+
*/
77+
static SeqType get(final SeqType[] cache, final Type type, final Occ occ) {
78+
assert occ != ZERO;
79+
final int o = occ.ordinal() - 1;
80+
SeqType st = cache[o];
81+
if(st == null) {
82+
st = new SeqType(type, occ);
83+
cache[o] = st;
84+
}
85+
return st;
86+
}
87+
5688
/**
5789
* Returns an array type for this sequence type.
5890
* @return array type

‎basex-core/src/main/java/org/basex/query/value/type/TypeRef.java‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ public final class TypeRef implements Type {
2222
private InputInfo info;
2323
/** Referenced type (initially {@code item()}, may itself be a {@code TypeRef}). */
2424
private Type type = BasicType.ITEM;
25-
/** Sequence types (can be {@code null}; lazy instantiation). */
26-
private EnumMap<Occ, SeqType> seqTypes;
25+
/** Sequence types. */
26+
private final SeqType[] seqTypes = SeqType.cache(this);
2727

2828
/**
2929
* Constructor.
@@ -123,8 +123,7 @@ public Item read(final DataInput in, final QueryContext qc) throws IOException,
123123
@Override
124124
public SeqType seqType(final Occ occ) {
125125
// wrap this placeholder, so that in-place resolution propagates to all use sites
126-
if(seqTypes == null) seqTypes = new EnumMap<>(Occ.class);
127-
return seqTypes.computeIfAbsent(occ, o -> new SeqType(this, o));
126+
return SeqType.get(seqTypes, this, occ);
128127
}
129128

130129
@Override

‎basex-core/src/main/java/org/basex/query/value/type/Types.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
public final class Types {
2121
/** Zero items (single instance). */
22-
public static final SeqType EMPTY_SEQUENCE_Z = ITEM.seqType(ZERO);
22+
public static final SeqType EMPTY_SEQUENCE_Z = new SeqType(ITEM, ZERO);
2323

2424
/** Single item. */
2525
public static final SeqType ITEM_O = ITEM.seqType();

0 commit comments

Comments
 (0)