Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use StringIntMap from Core, refactoring #772

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -438,16 +438,12 @@ void initialize(Deserializer.DecoderContext decoderContext) throws SerdeExceptio

private void initializeInternal(Deserializer.DecoderContext decoderContext) throws SerdeException {
if (injectProperties != null) {
List<Map.Entry<String, DerProperty<T, Object>>> properties = injectProperties.getProperties();
for (Map.Entry<String, DerProperty<T, Object>> e : properties) {
DerProperty<T, Object> property = e.getValue();
for (DerProperty<T, Object> property : injectProperties.getProperties()) {
initProperty(property, decoderContext);
}
}
if (creatorParams != null) {
List<Map.Entry<String, DerProperty<T, Object>>> properties = creatorParams.getProperties();
for (Map.Entry<String, DerProperty<T, Object>> e : properties) {
DerProperty<T, Object> property = e.getValue();
for (DerProperty<T, Object> property : creatorParams.getProperties()) {
initProperty(property, decoderContext);
}
}
Expand All @@ -466,8 +462,7 @@ private boolean isSimpleBean() {
return false;
}
if (injectProperties != null) {
for (Map.Entry<String, DerProperty<T, Object>> e : injectProperties.getProperties()) {
DerProperty<T, Object> property = e.getValue();
for (DerProperty<T, Object> property : injectProperties.getProperties()) {
if (property.isAnySetter || property.views != null || property.managedRef != null || introspection != property.instrospection || property.backRef != null || property.beanProperty == null) {
return false;
}
Expand All @@ -481,8 +476,7 @@ private boolean isRecordLikeBean() {
return false;
}
if (creatorParams != null) {
for (Map.Entry<String, DerProperty<T, Object>> e : creatorParams.getProperties()) {
DerProperty<T, Object> property = e.getValue();
for (DerProperty<T, Object> property : creatorParams.getProperties()) {
if (property.beanProperty != null && !property.beanProperty.isReadOnly() || property.isAnySetter || property.views != null || property.managedRef != null || introspection != property.instrospection || property.backRef != null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.beans.BeanIntrospection;
import io.micronaut.core.naming.Named;
import io.micronaut.core.util.StringIntMap;

import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
Expand Down Expand Up @@ -50,11 +50,7 @@ final class PropertiesBag<T> {
@Nullable
private final Map<String, Integer> nameToPropertiesMapping;
private final long propertiesMask;
// hash table with open addressing and linear probing that associates the key with the index
// similar to JDK maps but with int values
private final String[] keyTable;
private final int[] indexTable;
private final int tableMask;
private final StringIntMap nameToPosition;

private PropertiesBag(BeanIntrospection<T> beanIntrospection,
int[] originalNameToPropertiesMapping,
Expand All @@ -74,74 +70,49 @@ private PropertiesBag(BeanIntrospection<T> beanIntrospection,
propStream = Stream.concat(propStream, nameToPropertiesMapping.keySet().stream());
}
Set<String> props = propStream.collect(Collectors.toSet());
int tableSize = (props.size() * 2) + 1;
tableSize = Integer.highestOneBit(tableSize) * 2; // round to next power of two
tableMask = tableSize - 1;
this.keyTable = new String[tableSize];
this.indexTable = new int[keyTable.length];
nameToPosition = new StringIntMap(props.size());
for (String prop : props) {
int tableIndex = ~probe(prop);
keyTable[tableIndex] = prop;
indexTable[tableIndex] = propertyIndexOfSlow(prop);
}
}

private int probe(String key) {
int n = keyTable.length;
int i = key.hashCode() & tableMask;
while (true) {
String candidate = keyTable[i];
if (candidate == null) {
return ~i;
} else if (candidate.equals(key)) {
return i;
} else {
i++;
if (i == n) {
i = 0;
}
}
nameToPosition.put(prop, propertyIndexOfSlow(prop));
}
}

/**
* Get the properties in this bag with their property names.
* Get the properties in this bag.
*
* @return All properties in this bag
*/
public List<Map.Entry<String, DeserBean.DerProperty<T, Object>>> getProperties() {
Stream<AbstractMap.SimpleEntry<String, DeserBean.DerProperty<T, Object>>> originalProperties = Arrays.stream(originalNameToPropertiesMapping)
List<DeserBean.DerProperty<T, Object>> getProperties() {
Stream<DeserBean.DerProperty<T, Object>> originalProperties = Arrays.stream(originalNameToPropertiesMapping)
.filter(index -> index != -1)
.mapToObj(index -> {
DeserBean.DerProperty<T, Object> prop = properties[index];
if (prop.beanProperty == null) {
return null;
}
return new AbstractMap.SimpleEntry<>(prop.beanProperty.getName(), prop);
return prop;
});
Stream<AbstractMap.SimpleEntry<String, DeserBean.DerProperty<T, Object>>> mappedByName = nameToPropertiesMapping == null ? Stream.empty() : nameToPropertiesMapping.entrySet()
Stream<DeserBean.DerProperty<T, Object>> mappedByName = nameToPropertiesMapping == null ? Stream.empty() : nameToPropertiesMapping.values()
.stream()
.map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), properties[e.getValue()]));
.map(index -> properties[index]);
return Stream.concat(originalProperties, mappedByName)
.collect(Collectors.toList());
.toList();
}

/**
* Get the properties in this bag.
*
* @return All properties in this bag
*/
public List<DeserBean.DerProperty<T, Object>> getDerProperties() {
List<DeserBean.DerProperty<T, Object>> getDerProperties() {
return Collections.unmodifiableList(Arrays.asList(properties));
}

public DeserBean.DerProperty<T, Object>[] getPropertiesArray() {
DeserBean.DerProperty<T, Object>[] getPropertiesArray() {
return properties;
}

public int propertyIndexOf(@NonNull String name) {
int i = probe(name);
return i < 0 ? -1 : indexTable[i];
int propertyIndexOf(@NonNull String name) {
return nameToPosition.get(name, -1);
}

private int propertyIndexOfSlow(@NonNull String name) {
Expand All @@ -156,45 +127,31 @@ private int propertyIndexOfSlow(@NonNull String name) {
return nameToPropertiesMapping == null ? -1 : nameToPropertiesMapping.getOrDefault(name, -1);
}

public Consumer newConsumer() {
Consumer newConsumer() {
return propertiesMask == 0 ? new ConsumerBig() : new ConsumerSmall();
}

/**
* Properties consumer.
*/
public abstract sealed class Consumer {
abstract sealed class Consumer {
private Consumer() {
}

public boolean isNotConsumed(String name) {
int propertyIndex = propertyIndexOf(name);
return propertyIndex != -1 && !isConsumed(propertyIndex);
}

public DeserBean.DerProperty<T, Object> findNotConsumed(String name) {
int propertyIndex = propertyIndexOf(name);
if (propertyIndex == -1 || isConsumed(propertyIndex)) {
return null;
}
return properties[propertyIndex];
}

public DeserBean.DerProperty<T, Object> consume(String name) {
int propertyIndex = propertyIndexOf(name);
int propertyIndex = nameToPosition.get(name, -1);
if (propertyIndex == -1 || isConsumed(propertyIndex)) {
return null;
}
setConsumed(propertyIndex);
return properties[propertyIndex];
}

public DeserBean.DerProperty<T, Object> consume(int propertyIndex) {
public void consume(int propertyIndex) {
if (propertyIndex == -1 || isConsumed(propertyIndex)) {
return null;
return;
}
setConsumed(propertyIndex);
return properties[propertyIndex];
}

public List<DeserBean.DerProperty<T, Object>> getNotConsumed() {
Expand Down Expand Up @@ -255,7 +212,7 @@ public boolean isAllConsumed() {
}
}

public static class Builder<T> {
static class Builder<T> {

private final BeanIntrospection<T> beanIntrospection;
private final int[] originalNameToPropertiesMapping;
Expand All @@ -264,19 +221,19 @@ public static class Builder<T> {

private final List<DeserBean.DerProperty<T, Object>> mutableProperties;

public Builder(BeanIntrospection<T> beanIntrospection) {
Builder(BeanIntrospection<T> beanIntrospection) {
this(beanIntrospection, beanIntrospection.getBeanProperties().size());
}

public Builder(BeanIntrospection<T> beanIntrospection, int expectedPropertiesSize) {
Builder(BeanIntrospection<T> beanIntrospection, int expectedPropertiesSize) {
this.beanIntrospection = beanIntrospection;
int beanPropertiesSize = beanIntrospection.getBeanProperties().size();
this.originalNameToPropertiesMapping = new int[beanPropertiesSize];
Arrays.fill(originalNameToPropertiesMapping, -1);
this.mutableProperties = new ArrayList<>(expectedPropertiesSize);
}

public void register(String name, DeserBean.DerProperty<T, Object> derProperty, boolean addAliases) {
void register(String name, DeserBean.DerProperty<T, Object> derProperty, boolean addAliases) {
int newPropertyIndex = mutableProperties.size();
if (derProperty.beanProperty != null && derProperty.beanProperty.getDeclaringBean() == beanIntrospection && name.equals(derProperty.beanProperty.getName())) {
originalNameToPropertiesMapping[beanIntrospection.propertyIndexOf(name)] = newPropertyIndex;
Expand All @@ -295,11 +252,10 @@ public void register(String name, DeserBean.DerProperty<T, Object> derProperty,
}
}
mutableProperties.add(derProperty);

}

@Nullable
public PropertiesBag<T> build() {
PropertiesBag<T> build() {
if (mutableProperties.isEmpty()) {
return null;
}
Expand Down
Loading