Objects: getFromSource just ever so slightly faster

one map access
This commit is contained in:
2025-04-23 14:50:28 +02:00
parent 04e932ed62
commit e2e756e7c5
3 changed files with 24 additions and 31 deletions

View File

@@ -42,13 +42,15 @@ public class CachingObjectPersistentStore {
if (old != null) if (old != null)
newSize -= old.size(); newSize -= old.size();
TreePMap<JObjectKey, CacheEntry> newCache = map().plus(key, entry); TreePMap<JObjectKey, CacheEntry> newCache = map();
while (newSize > sizeLimit) { while (newSize > sizeLimit) {
var del = newCache.firstEntry(); var del = newCache.firstEntry();
newCache = newCache.minusFirstEntry(); newCache = newCache.minusFirstEntry();
newSize -= del.getValue().size(); newSize -= del.getValue().size();
} }
newCache = newCache.plus(key, entry);
return new Cache( return new Cache(
newCache, newCache,
newSize, newSize,

View File

@@ -94,15 +94,12 @@ public class TransactionFactoryImpl implements TransactionFactory {
@Override @Override
public <T extends JData> Optional<T> getFromSource(Class<T> type, JObjectKey key) { public <T extends JData> Optional<T> getFromSource(Class<T> type, JObjectKey key) {
var got = _readSet.get(key); return _readSet.computeIfAbsent(key, k -> {
var read = _snapshot.readObject(k);
if (got == null) { return new TransactionObjectNoLock<>(read);
var read = _snapshot.readObject(key); })
_readSet.put(key, new TransactionObjectNoLock<>(read)); .data()
return read.map(JDataVersionedWrapper::data).map(type::cast); .map(w -> type.cast(w.data()));
}
return got.data().map(JDataVersionedWrapper::data).map(type::cast);
} }
public <T extends JData> Optional<T> getWriteLockedFromSource(Class<T> type, JObjectKey key) { public <T extends JData> Optional<T> getWriteLockedFromSource(Class<T> type, JObjectKey key) {

View File

@@ -6,8 +6,7 @@ import org.pcollections.*;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
public record RemoteObjectMeta(PCollection<JDataRef> refsFrom, boolean frozen, public record RemoteObjectMeta(JObjectKey key, PCollection<JDataRef> refsFrom, boolean frozen,
JObjectKey key,
PMap<PeerId, Long> knownRemoteVersions, PMap<PeerId, Long> knownRemoteVersions,
Class<? extends JDataRemote> knownType, Class<? extends JDataRemote> knownType,
PSet<PeerId> confirmedDeletes, PSet<PeerId> confirmedDeletes,
@@ -16,22 +15,22 @@ public record RemoteObjectMeta(PCollection<JDataRef> refsFrom, boolean frozen,
boolean hasLocalData) implements JDataRefcounted { boolean hasLocalData) implements JDataRefcounted {
// Self put // Self put
public RemoteObjectMeta(JDataRemote data, PeerId initialPeer) { public RemoteObjectMeta(JDataRemote data, PeerId initialPeer) {
this(HashTreePSet.empty(), false, this(data.key(), HashTreePSet.empty(), false,
data.key(), HashTreePMap.empty(), data.getClass(), HashTreePSet.empty(), false, HashTreePMap.empty(), data.getClass(), HashTreePSet.empty(), false,
HashTreePMap.<PeerId, Long>empty().plus(initialPeer, 1L), HashTreePMap.<PeerId, Long>empty().plus(initialPeer, 1L),
true); true);
} }
public RemoteObjectMeta(JObjectKey key, PMap<PeerId, Long> remoteChangelog) { public RemoteObjectMeta(JObjectKey key, PMap<PeerId, Long> remoteChangelog) {
this(HashTreePSet.empty(), false, this(key, HashTreePSet.empty(), false,
key, HashTreePMap.empty(), JDataRemote.class, HashTreePSet.empty(), true, HashTreePMap.empty(), JDataRemote.class, HashTreePSet.empty(), true,
remoteChangelog, remoteChangelog,
false); false);
} }
public RemoteObjectMeta(JObjectKey key) { public RemoteObjectMeta(JObjectKey key) {
this(HashTreePSet.empty(), false, this(key, HashTreePSet.empty(), false,
key, HashTreePMap.empty(), JDataRemote.class, HashTreePSet.empty(), true, HashTreePMap.empty(), JDataRemote.class, HashTreePSet.empty(), true,
TreePMap.empty(), TreePMap.empty(),
false); false);
} }
@@ -44,47 +43,42 @@ public record RemoteObjectMeta(PCollection<JDataRef> refsFrom, boolean frozen,
return JObjectKey.of(key.value() + "_data"); return JObjectKey.of(key.value() + "_data");
} }
@Override
public JObjectKey key() {
return ofMetaKey(key);
}
public JObjectKey dataKey() { public JObjectKey dataKey() {
return ofDataKey(key); return ofDataKey(key);
} }
@Override @Override
public RemoteObjectMeta withRefsFrom(PCollection<JDataRef> refs) { public RemoteObjectMeta withRefsFrom(PCollection<JDataRef> refs) {
return new RemoteObjectMeta(refs, frozen, key, knownRemoteVersions, knownType, confirmedDeletes, seen, changelog, hasLocalData); return new RemoteObjectMeta(key, refs, frozen, knownRemoteVersions, knownType, confirmedDeletes, seen, changelog, hasLocalData);
} }
@Override @Override
public RemoteObjectMeta withFrozen(boolean frozen) { public RemoteObjectMeta withFrozen(boolean frozen) {
return new RemoteObjectMeta(refsFrom, frozen, key, knownRemoteVersions, knownType, confirmedDeletes, seen, changelog, hasLocalData); return new RemoteObjectMeta(key, refsFrom, frozen, knownRemoteVersions, knownType, confirmedDeletes, seen, changelog, hasLocalData);
} }
public RemoteObjectMeta withKnownRemoteVersions(PMap<PeerId, Long> knownRemoteVersions) { public RemoteObjectMeta withKnownRemoteVersions(PMap<PeerId, Long> knownRemoteVersions) {
return new RemoteObjectMeta(refsFrom, frozen, key, knownRemoteVersions, knownType, confirmedDeletes, seen, changelog, hasLocalData); return new RemoteObjectMeta(key, refsFrom, frozen, knownRemoteVersions, knownType, confirmedDeletes, seen, changelog, hasLocalData);
} }
public RemoteObjectMeta withKnownType(Class<? extends JDataRemote> knownType) { public RemoteObjectMeta withKnownType(Class<? extends JDataRemote> knownType) {
return new RemoteObjectMeta(refsFrom, frozen, key, knownRemoteVersions, knownType, confirmedDeletes, seen, changelog, hasLocalData); return new RemoteObjectMeta(key, refsFrom, frozen, knownRemoteVersions, knownType, confirmedDeletes, seen, changelog, hasLocalData);
} }
public RemoteObjectMeta withConfirmedDeletes(PSet<PeerId> confirmedDeletes) { public RemoteObjectMeta withConfirmedDeletes(PSet<PeerId> confirmedDeletes) {
return new RemoteObjectMeta(refsFrom, frozen, key, knownRemoteVersions, knownType, confirmedDeletes, seen, changelog, hasLocalData); return new RemoteObjectMeta(key, refsFrom, frozen, knownRemoteVersions, knownType, confirmedDeletes, seen, changelog, hasLocalData);
} }
public RemoteObjectMeta withSeen(boolean seen) { public RemoteObjectMeta withSeen(boolean seen) {
return new RemoteObjectMeta(refsFrom, frozen, key, knownRemoteVersions, knownType, confirmedDeletes, seen, changelog, hasLocalData); return new RemoteObjectMeta(key, refsFrom, frozen, knownRemoteVersions, knownType, confirmedDeletes, seen, changelog, hasLocalData);
} }
public RemoteObjectMeta withChangelog(PMap<PeerId, Long> changelog) { public RemoteObjectMeta withChangelog(PMap<PeerId, Long> changelog) {
return new RemoteObjectMeta(refsFrom, frozen, key, knownRemoteVersions, knownType, confirmedDeletes, seen, changelog, hasLocalData); return new RemoteObjectMeta(key, refsFrom, frozen, knownRemoteVersions, knownType, confirmedDeletes, seen, changelog, hasLocalData);
} }
public RemoteObjectMeta withHaveLocal(boolean haveLocal) { public RemoteObjectMeta withHaveLocal(boolean haveLocal) {
return new RemoteObjectMeta(refsFrom, frozen, key, knownRemoteVersions, knownType, confirmedDeletes, seen, changelog, haveLocal); return new RemoteObjectMeta(key, refsFrom, frozen, knownRemoteVersions, knownType, confirmedDeletes, seen, changelog, haveLocal);
} }
public long versionSum() { public long versionSum() {