performance boost list --> set

This commit is contained in:
Noemi Szemenyei
2022-03-15 10:46:22 +01:00
parent 52ca271a13
commit 2478aded70
6 changed files with 24 additions and 22 deletions
@@ -4,7 +4,7 @@ import io.ebean.bean.EntityBeanIntercept;
import io.ebean.bean.PersistenceContext;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import java.util.List;
import java.util.Set;
/**
* A buffer of beans for batch lazy loading and secondary query loading.
@@ -13,7 +13,7 @@ public interface LoadBeanBuffer {
int batchSize();
List<EntityBeanIntercept> batch();
Set<EntityBeanIntercept> batch();
BeanDescriptor<?> descriptor();
@@ -16,7 +16,7 @@ import java.util.Set;
*/
public final class LoadBeanRequest extends LoadRequest {
private final List<EntityBeanIntercept> batch;
private final Set<EntityBeanIntercept> batch;
private final LoadBeanBuffer loadBuffer;
private final String lazyLoadProperty;
private final boolean loadCache;
@@ -58,7 +58,7 @@ public final class LoadBeanRequest extends LoadRequest {
/**
* Return the batch of beans to actually load.
*/
public List<EntityBeanIntercept> batch() {
public Set<EntityBeanIntercept> batch() {
return batch;
}
@@ -17,6 +17,7 @@ import org.slf4j.Logger;
import javax.persistence.EntityNotFoundException;
import java.util.List;
import java.util.Set;
/**
* Helper to handle lazy loading and refreshing of beans.
@@ -121,7 +122,7 @@ final class DefaultBeanLoader {
* Load a batch of beans for +query or +lazy loading.
*/
void loadBean(LoadBeanRequest loadRequest) {
List<EntityBeanIntercept> batch = loadRequest.batch();
Set<EntityBeanIntercept> batch = loadRequest.batch();
if (batch.isEmpty()) {
throw new RuntimeException("Nothing in batch?");
}
@@ -1312,8 +1312,8 @@ public class BeanDescriptor<T> implements BeanType<T>, STreeType, SpiBeanType {
* Hit the bean cache trying to load a list/batch of entities.
* Return the set of entities that were successfully loaded from L2 cache.
*/
public Set<EntityBeanIntercept> cacheBeanLoadAll(List<EntityBeanIntercept> list, PersistenceContext persistenceContext, int lazyLoadProperty, String propertyName) {
return cacheHelp.beanCacheLoadAll(list, persistenceContext, lazyLoadProperty, propertyName);
public Set<EntityBeanIntercept> cacheBeanLoadAll(Set<EntityBeanIntercept> batch, PersistenceContext persistenceContext, int lazyLoadProperty, String propertyName) {
return cacheHelp.beanCacheLoadAll(batch, persistenceContext, lazyLoadProperty, propertyName);
}
/**
@@ -693,9 +693,9 @@ final class BeanDescriptorCacheHelp<T> {
/**
* Load a batch of entities from L2 bean cache checking the lazy loaded property is loaded.
*/
Set<EntityBeanIntercept> beanCacheLoadAll(List<EntityBeanIntercept> list, PersistenceContext context, int lazyLoadProperty, String propertyName) {
Set<EntityBeanIntercept> beanCacheLoadAll(Set<EntityBeanIntercept> batch, PersistenceContext context, int lazyLoadProperty, String propertyName) {
Map<Object, EntityBeanIntercept> ebis = new HashMap<>();
for (EntityBeanIntercept ebi : list) {
for (EntityBeanIntercept ebi : batch) {
ebis.put(desc.cacheKeyForBean(ebi.getOwner()), ebi);
}
@@ -15,6 +15,7 @@ import io.ebeaninternal.server.deploy.BeanPropertyAssocMany;
import io.ebeaninternal.server.querydefn.OrmQueryProperties;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.locks.Lock;
@@ -91,7 +92,7 @@ final class DLoadBeanContext extends DLoadBaseContext implements LoadBeanContext
try {
if (bufferList != null) {
for (LoadBuffer loadBuffer : bufferList) {
if (!loadBuffer.list.isEmpty()) {
if (!loadBuffer.batch.isEmpty()) {
parent.getEbeanServer().loadBean(new LoadBeanRequest(loadBuffer, parentRequest));
}
if (forEach) {
@@ -115,13 +116,13 @@ final class DLoadBeanContext extends DLoadBaseContext implements LoadBeanContext
private final ReentrantLock bufferLock = new ReentrantLock();
private final DLoadBeanContext context;
private final int batchSize;
private final List<EntityBeanIntercept> list;
private final Set<EntityBeanIntercept> batch;
private PersistenceContext persistenceContext;
LoadBuffer(DLoadBeanContext context, int batchSize) {
this.context = context;
this.batchSize = batchSize;
this.list = new ArrayList<>(batchSize);
this.batch = new HashSet<>(Math.max((int) (batchSize/.75f) + 1, 16));
}
@Override
@@ -139,7 +140,7 @@ final class DLoadBeanContext extends DLoadBaseContext implements LoadBeanContext
* Return true if the buffer is full.
*/
public boolean isFull() {
return batchSize == list.size();
return batchSize == batch.size();
}
/**
@@ -150,12 +151,12 @@ final class DLoadBeanContext extends DLoadBaseContext implements LoadBeanContext
// get persistenceContext from first loaded bean into the buffer
persistenceContext = ebi.getPersistenceContext();
}
list.add(ebi);
batch.add(ebi);
}
@Override
public List<EntityBeanIntercept> batch() {
return list;
public Set<EntityBeanIntercept> batch() {
return batch;
}
@Override
@@ -190,13 +191,13 @@ final class DLoadBeanContext extends DLoadBaseContext implements LoadBeanContext
// lazy load property was a Many
return;
}
if (!list.contains(ebi)) {
if (!batch.contains(ebi)) {
// re-add to the batch and lazy load from DB skipping l2 cache
list.add(ebi);
batch.add(ebi);
} else if (context.hitCache) {
Set<EntityBeanIntercept> hits = context.desc.cacheBeanLoadAll(list, persistenceContext, ebi.getLazyLoadPropertyIndex(), ebi.getLazyLoadProperty());
list.removeAll(hits);
if (list.isEmpty() || hits.contains(ebi)) {
Set<EntityBeanIntercept> hits = context.desc.cacheBeanLoadAll(batch, persistenceContext, ebi.getLazyLoadPropertyIndex(), ebi.getLazyLoadProperty());
batch.removeAll(hits);
if (batch.isEmpty() || hits.contains(ebi)) {
// successfully hit the L2 cache so don't invoke DB lazy loading
return;
}
@@ -204,7 +205,7 @@ final class DLoadBeanContext extends DLoadBaseContext implements LoadBeanContext
LoadBeanRequest req = new LoadBeanRequest(this, ebi, context.hitCache);
context.desc.ebeanServer().loadBean(req);
list.clear();
batch.clear();
}
}