Build and freeze transaction events in bulk

This commit is contained in:
eyck committed 2026-11-14 17:38:02 +01:00
1 parent e78c65a36c
commit 75fa1a4f31
10 files changed
+146 -22

No files matched your search

@@ -36,6 +36,7 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
/** The events. */
protected IEventList events = new EventList();
private EventList.Builder eventBuilder = new EventList.Builder();
/** The max concurrency. */
private int rowCount = -1;
@@ -69,9 +70,16 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
* @param evt the evt
*/
public void addEvent(ITxEvent evt) {
events.put(evt.getTime(), evt);
eventBuilder.add(evt.getTime(), evt);
}
void finishEvents() {
if (eventBuilder != null) {
events = eventBuilder.build();
eventBuilder = null;
}
}
/**
* Gets the events at time.
*
@@ -82,6 +82,9 @@ class TxStream extends AbstractTxStream {
try {
int blockId = 0;
for (byte[] chunk : getChunks()) loader.parseTx(this, blockId++, chunk);
finishEvents();
for (com.minres.scviewer.database.IHierNode child : getChildNodes())
((TxGenerator) child).finishEvents();
return Boolean.TRUE;
} catch (InputFormatException | IOException e) {
throw new IllegalStateException("Cannot parse stream " + getFullName(), e);
@@ -13,7 +13,6 @@ package com.minres.scviewer.database.sqlite;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.minres.scviewer.database.EventEntry;
import com.minres.scviewer.database.EventKind;
@@ -77,20 +76,24 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
}
@Override
public IEventList getEvents(){
if(events==null){
events=new EventList();
for(Entry<Integer, ITx> entry:getTransactions().entrySet()){
putEvent(new TxEvent(EventKind.BEGIN, entry.getValue()));
putEvent(new TxEvent(EventKind.END, entry.getValue()));
}
}
return events;
}
private void putEvent(TxEvent ev){
events.put(ev.getTime(), ev);
}
public synchronized IEventList getEvents() {
if (events == null) {
EventList.Builder builder = new EventList.Builder();
for (ITx tx : getTransactions().values()) {
if (tx.getBeginTime() == tx.getEndTime()) {
TxEvent single = new TxEvent(EventKind.SINGLE, tx);
builder.add(single.getTime(), single);
} else {
TxEvent begin = new TxEvent(EventKind.BEGIN, tx);
TxEvent end = new TxEvent(EventKind.END, tx);
builder.add(begin.getTime(), begin);
builder.add(end.getTime(), end);
}
}
events = builder.build();
}
return events;
}
protected abstract Map<Integer, ITx> getTransactions();
@@ -37,6 +37,7 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
/** The events. */
IEventList events = new EventList();
private EventList.Builder eventBuilder = new EventList.Builder();
/** The max concurrency. */
private int rowCount = -1;
@@ -70,9 +71,16 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
* @param evt the evt
*/
public void addEvent(ITxEvent evt) {
events.put(evt.getTime(), evt);
eventBuilder.add(evt.getTime(), evt);
}
void finishEvents() {
if (eventBuilder != null) {
events = eventBuilder.build();
eventBuilder = null;
}
}
/**
* Gets the events.
*
@@ -252,7 +252,9 @@ public class TextDbLoader implements IWaveformDbLoader {
} finally {
transactions = parser.txSink;
}
txStreams.values().parallelStream().forEach(TxStream::calculateConcurrency);
txStreams.values().forEach(TxStream::finishEvents);
txGenerators.values().forEach(TxGenerator::finishEvents);
txStreams.values().parallelStream().forEach(TxStream::calculateConcurrency);
}
/**
@@ -106,7 +106,7 @@ public class SignalPainter extends TrackPainter {
EventEntry first = signal.getEvents().floorEntry(beginTime);
if (first == null)
first = signal.getEvents().firstEntry();
if (first == null) return;
if (first == null || Long.compareUnsigned(first.timestamp, endTime) > 0) return;
beginTime = first.timestamp;
proj.setForeground(this.waveCanvas.styleProvider.getColor(WaveformColors.LINE));
proj.setLineStyle(SWT.LINE_SOLID);
@@ -7,6 +7,36 @@ import java.util.NoSuchElementException;
/** Sorted timestamp buckets. Range views are read-only and bounded by their parent. */
public class EventList implements IEventList {
/** Accumulates unsorted events and freezes timestamp buckets with one sort and one allocation per bucket. */
public static final class Builder {
private record TimedEvent(long time, IEvent event) { }
private final ArrayList<TimedEvent> pending = new ArrayList<>();
private boolean built;
public void add(long time, IEvent event) {
if (built) throw new IllegalStateException("Event list has already been built");
pending.add(new TimedEvent(time, event));
}
public EventList build() {
if (built) throw new IllegalStateException("Event list has already been built");
pending.sort((a, b) -> Long.compareUnsigned(a.time(), b.time()));
ArrayList<EventEntry> entries = new ArrayList<>();
for (int first = 0; first < pending.size();) {
long time = pending.get(first).time();
int next = first + 1;
while (next < pending.size() && pending.get(next).time() == time) next++;
IEvent[] events = new IEvent[next - first];
for (int i = first; i < next; ++i) events[i - first] = pending.get(i).event();
entries.add(new EventEntry(time, events));
first = next;
}
built = true;
pending.clear();
return new EventList(entries, 0, entries.size(), true);
}
}
private final ArrayList<EventEntry> store;
private final int start;
private int end;
@@ -70,9 +70,9 @@ public interface IEventList extends Iterable<EventEntry> {
* {@code fromKey} to {@code toKey}. If {@code fromKey} and
* {@code toKey} are equal, the returned map is empty unless
* {@code fromInclusive} is true. The
* returned map is backed by this map, so changes in the returned map are
* reflected in this map, and vice-versa. The returned map supports all
* optional map operations that this map supports.
* returned view is read-only. Do not structurally modify a mutable source
* while a view is in use. EventList.Builder produces frozen sources suitable
* for sharing range views.
*
* @param fromKey low endpoint of the keys in the returned map
* @param fromInclusive {@code true} if the low endpoint