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

[Performance] Export Model constructor throws exceptions on very hot paths #127

Merged
merged 10 commits into from
Sep 6, 2017
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
28 changes: 25 additions & 3 deletions core/src/main/java/org/kohsuke/stapler/export/ModelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
Expand All @@ -36,18 +37,39 @@
public class ModelBuilder {
/**
* Instantiated {@link Model}s.
* Registration happens in {@link Model#Model(ModelBuilder,Class)} so that cyclic references
* Registration happens in {@link Model#Model(ModelBuilder, Class, Class, String)} so that cyclic references
* are handled correctly.
*/
/*package*/ final Map<Class, Model> models = new ConcurrentHashMap<Class, Model>();

@Nonnull
public <T> Model<T> get(Class<T> type) throws NotExportableException {
return get(type, null, null);
}

/**
* @throws NotExportableException if type is not exportable
* @return model
*/
@Nonnull
public <T> Model<T> get(Class<T> type, @CheckForNull Class<?> propertyOwner, @Nullable String property) throws NotExportableException {
Model m = models.get(type);
if(m==null) {
Model<T> model = getOrNull(type, propertyOwner, property);
if (model == null) {
throw new NotExportableException(type);
}
return model;
}

/**
* Instead of throwing {@link NotExportableException} this method will return null
* This should be used on hot paths where throwing the exception and catching it would incur a performance hit
* @return model
* @since 1.253
*/
@CheckForNull
public <T> Model<T> getOrNull(Class<T> type, @CheckForNull Class<?> propertyOwner, @Nullable String property) {
Model<T> m = models.get(type);
if(m==null && type.getAnnotation(ExportedBean.class) != null) {
m = new Model<T>(this, type, propertyOwner, property);
}
return m;
Expand Down
36 changes: 16 additions & 20 deletions core/src/main/java/org/kohsuke/stapler/export/Property.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public int compareTo(Property that) {
* @param pruner
* Determines how to prune the object graph tree.
*/
@SuppressWarnings("unchecked")
public void writeTo(Object object, TreePruner pruner, DataWriter writer) throws IOException {
TreePruner child = pruner.accept(object, this);
if (child==null) return;
Expand All @@ -139,16 +140,13 @@ public void writeTo(Object object, TreePruner pruner, DataWriter writer) throws
if (merge) {
// merged property will get all its properties written here
if (d != null) {
Model model;
try {
model = owner.get(d.getClass(), parent.type, name);
} catch (NotExportableException e) {
if(writer.getExportConfig().isSkipIfFail()){
return;
}
throw e;
Class<?> objectType = d.getClass();
Model model = owner.getOrNull(objectType, parent.type, name);
if (model == null && !writer.getExportConfig().isSkipIfFail()) {
throw new NotExportableException(objectType);
} else if (model != null) {
model.writeNestedObjectTo(d, new FilteringTreePruner(parent.HAS_PROPERTY_NAME_IN_ANCESTRY,child), writer);
}
model.writeNestedObjectTo(d, new FilteringTreePruner(parent.HAS_PROPERTY_NAME_IN_ANCESTRY,child), writer);
}
} else {
writer.name(name);
Expand Down Expand Up @@ -186,6 +184,7 @@ private void writeBuffered(Type expected, Object value, TreePruner pruner, DataW
/**
* Writes one value of the property to {@link DataWriter}.
*/
@SuppressWarnings("unchecked")
private void writeValue(Type expected, Object value, TreePruner pruner, DataWriter writer, boolean skipIfFail) throws IOException {
if(value==null) {
writer.valueNull();
Expand All @@ -199,10 +198,8 @@ private void writeValue(Type expected, Object value, TreePruner pruner, DataWrit

Class c = value.getClass();

Model model;
try {
model = owner.get(c, parent.type, name);
} catch (NotExportableException ex) {
Model model = owner.getOrNull(c, parent.type, name);
if (model == null) {
if(STRING_TYPES.contains(c)) {
writer.value(value.toString());
return;
Expand Down Expand Up @@ -298,14 +295,13 @@ private void writeValue(Type expected, Object value, TreePruner pruner, DataWrit
return;
}

throw ex;
throw new NotExportableException(c);
} else {
writer.type(expected, value.getClass());
writer.startObject();
model.writeNestedObjectTo(value, pruner, writer);
writer.endObject();
}

writer.type(expected, value.getClass());

writer.startObject();
model.writeNestedObjectTo(value, pruner, writer);
writer.endObject();
}

private static class BufferedDataWriter implements DataWriter {
Expand Down