Skip to content

Commit b214eb3

Browse files
committed
HHH-16884 Improve efficiency of UpdateCoordinatorStandard in tracking tables to be updated
1 parent 314f2d7 commit b214eb3

File tree

3 files changed

+94
-11
lines changed

3 files changed

+94
-11
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html.
6+
*/
7+
package org.hibernate.persister.entity.mutation;
8+
9+
import java.util.Arrays;
10+
import java.util.BitSet;
11+
12+
import org.hibernate.sql.model.MutationTarget;
13+
import org.hibernate.sql.model.TableMapping;
14+
15+
/**
16+
* Represents a Set of TableMapping(s); table mappings are
17+
* identified by an ordered unique id: the order in which
18+
* they are updated within the scope of a particular persister.
19+
* This makes it possible to store a set of them as a bitset,
20+
* which is typically more efficient than using a {@link java.util.Set}.
21+
* These table ids come from {@link org.hibernate.sql.model.TableMapping#getRelativePosition}.
22+
* <p>N.B. Make sure to not store TableMappings from different
23+
* persisters, as their unique identifiers will overlap:
24+
* we'll only verify a mismatch if assertions are enabled.</p>
25+
*/
26+
public final class TableSet {
27+
28+
private BitSet bits;
29+
private Object[] checks; //Meant for assertions only
30+
31+
public void add(final TableMapping tableMapping) {
32+
if ( bits == null ) {
33+
bits = new BitSet();
34+
}
35+
assert addForChecks( tableMapping );
36+
bits.set( tableMapping.getRelativePosition() );
37+
}
38+
39+
public boolean isEmpty() {
40+
return bits == null;
41+
}
42+
43+
public boolean contains(final TableMapping tableMapping) {
44+
assert matchRead( tableMapping );
45+
return bits != null && bits.get( tableMapping.getRelativePosition() );
46+
}
47+
48+
//Meant for assertions only
49+
private boolean matchRead(final TableMapping tableMapping) {
50+
if ( bits != null ) {
51+
final int index = tableMapping.getRelativePosition();
52+
if ( bits.get( index ) ) {
53+
return checks[index] == tableMapping;
54+
}
55+
}
56+
return true; //to make the assertion happy
57+
}
58+
59+
//Meant for assertions only
60+
private boolean addForChecks(final TableMapping tableMapping) {
61+
final int position = tableMapping.getRelativePosition();
62+
ensureCapacity( position );
63+
if ( checks[position] != null ) {
64+
//pre-existing in the set: verify it's the same one.
65+
if ( checks[position] != tableMapping ) {
66+
return false;//fail the assertion
67+
}
68+
}
69+
checks[position] = tableMapping;
70+
return true; //to make the assertion happy
71+
}
72+
73+
//Meant for assertions only
74+
private void ensureCapacity(final int position) {
75+
final int increments = 3; //Needs to be at least 1.
76+
if ( checks == null ) {
77+
checks = new Object[position + increments];
78+
}
79+
else if ( checks.length <= position ) {
80+
checks = Arrays.copyOf( checks, position + increments );
81+
}
82+
}
83+
84+
}

hibernate-core/src/main/java/org/hibernate/persister/entity/mutation/UpdateCoordinatorStandard.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,10 +1242,10 @@ protected class UpdateValuesAnalysisImpl implements UpdateValuesAnalysis {
12421242
private final int[] dirtyAttributeIndexes;
12431243
private final InclusionChecker dirtinessChecker;
12441244

1245-
private final Set<EntityTableMapping> tablesNeedingUpdate = new HashSet<>();
1246-
private final Set<EntityTableMapping> tablesNeedingDynamicUpdate = new HashSet<>();
1247-
private final Set<EntityTableMapping> tablesWithNonNullValues = new HashSet<>();
1248-
private final Set<EntityTableMapping> tablesWithPreviousNonNullValues = new HashSet<>();
1245+
private final TableSet tablesNeedingUpdate = new TableSet();
1246+
private final TableSet tablesNeedingDynamicUpdate = new TableSet();
1247+
private final TableSet tablesWithNonNullValues = new TableSet();
1248+
private final TableSet tablesWithPreviousNonNullValues = new TableSet();
12491249

12501250
private final List<AttributeAnalysis> attributeAnalyses = new ArrayList<>();
12511251

@@ -1316,17 +1316,17 @@ public Object[] getValues() {
13161316
}
13171317

13181318
@Override
1319-
public Set<EntityTableMapping> getTablesNeedingUpdate() {
1319+
public TableSet getTablesNeedingUpdate() {
13201320
return tablesNeedingUpdate;
13211321
}
13221322

13231323
@Override
1324-
public Set<EntityTableMapping> getTablesWithNonNullValues() {
1324+
public TableSet getTablesWithNonNullValues() {
13251325
return tablesWithNonNullValues;
13261326
}
13271327

13281328
@Override
1329-
public Set<EntityTableMapping> getTablesWithPreviousNonNullValues() {
1329+
public TableSet getTablesWithPreviousNonNullValues() {
13301330
return tablesWithPreviousNonNullValues;
13311331
}
13321332

hibernate-core/src/main/java/org/hibernate/persister/entity/mutation/UpdateValuesAnalysis.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package org.hibernate.persister.entity.mutation;
88

99
import java.util.List;
10-
import java.util.Set;
1110

1211
import org.hibernate.Incubating;
1312
import org.hibernate.sql.model.TableMapping;
@@ -29,17 +28,17 @@ public interface UpdateValuesAnalysis extends ValuesAnalysis {
2928
*
3029
* @apiNote {@linkplain TableMapping#isInverse() Inverse tables} are not included in the result
3130
*/
32-
Set<EntityTableMapping> getTablesNeedingUpdate();
31+
TableSet getTablesNeedingUpdate();
3332

3433
/**
3534
* Descriptor of the tables which had any non-null value bindings
3635
*/
37-
Set<EntityTableMapping> getTablesWithNonNullValues();
36+
TableSet getTablesWithNonNullValues();
3837

3938
/**
4039
* Descriptor of the tables which had any non-null value bindings
4140
*/
42-
Set<EntityTableMapping> getTablesWithPreviousNonNullValues();
41+
TableSet getTablesWithPreviousNonNullValues();
4342

4443
/**
4544
* Descriptors for the analysis of each attribute

0 commit comments

Comments
 (0)