Skip to content

Commit

Permalink
Lazy-allocate the Row list to speedup drop or create use cases
Browse files Browse the repository at this point in the history
  • Loading branch information
franz1981 committed Mar 14, 2023
1 parent 1d21f2e commit 8a17940
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions h2/src/main/org/h2/mvstore/db/MVTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -419,11 +421,14 @@ private void rebuildIndexBlockMerge(SessionLocal session, MVIndex<?,?> index) {
Store store = session.getDatabase().getStore();

int bufferSize = database.getMaxMemoryRows() / 2;
ArrayList<Row> buffer = new ArrayList<>(bufferSize);
List<Row> buffer = null;
String n = getName() + ':' + index.getName();
ArrayList<String> bufferNames = Utils.newSmallArrayList();
while (cursor.next()) {
Row row = cursor.get();
if (buffer == null) {
buffer = new ArrayList<>(bufferSize);
}
buffer.add(row);
database.setProgress(DatabaseEventListener.STATE_CREATE_INDEX, n, i++, total);
if (buffer.size() >= bufferSize) {
Expand All @@ -435,6 +440,9 @@ private void rebuildIndexBlockMerge(SessionLocal session, MVIndex<?,?> index) {
}
remaining--;
}
if (buffer == null) {
buffer = Collections.emptyList();
}
sortRows(buffer, index);
if (!bufferNames.isEmpty()) {
String mapName = store.nextTemporaryMapName();
Expand All @@ -457,17 +465,23 @@ private void rebuildIndexBuffered(SessionLocal session, Index index) {
Cursor cursor = scan.find(session, null, null);
long i = 0;
int bufferSize = (int) Math.min(total, database.getMaxMemoryRows());
ArrayList<Row> buffer = new ArrayList<>(bufferSize);
List<Row> buffer = null;
String n = getName() + ':' + index.getName();
while (cursor.next()) {
Row row = cursor.get();
if (buffer == null) {
buffer = new ArrayList<>(bufferSize);
}
buffer.add(row);
database.setProgress(DatabaseEventListener.STATE_CREATE_INDEX, n, i++, total);
if (buffer.size() >= bufferSize) {
addRowsToIndex(session, buffer, index);
}
remaining--;
}
if (buffer == null) {
buffer = Collections.emptyList();
}
addRowsToIndex(session, buffer, index);
if (remaining != 0) {
throw DbException.getInternalError("rowcount remaining=" + remaining + ' ' + getName());
Expand Down Expand Up @@ -703,7 +717,7 @@ public int getMainIndexColumn() {
* @param index
* the index to append to
*/
private static void addRowsToIndex(SessionLocal session, ArrayList<Row> list, Index index) {
private static void addRowsToIndex(SessionLocal session, List<Row> list, Index index) {
sortRows(list, index);
for (Row row : list) {
index.add(session, row);
Expand Down Expand Up @@ -763,7 +777,7 @@ private static String lockTypeToString(int lockType) {
* @param index
* the index to sort for
*/
private static void sortRows(ArrayList<? extends SearchRow> list, final Index index) {
private static void sortRows(List<? extends SearchRow> list, final Index index) {
list.sort(index::compareRows);
}

Expand Down

0 comments on commit 8a17940

Please sign in to comment.