Skip to content

Commit 6fc93af

Browse files
authored
Merge pull request wildfly#18112 from pferraro/WFLY-19613
WFLY-19613 Immutability performance optimizations
2 parents 9441016 + fc8a193 commit 6fc93af

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

clustering/web/cache/src/main/java/org/wildfly/clustering/web/cache/session/attributes/coarse/CoarseSessionAttributes.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public Object setAttribute(String name, Object value) {
6767
@Override
6868
public Object getAttribute(String name) {
6969
Object value = this.attributes.get(name);
70-
if (!this.immutability.test(value)) {
70+
// Bypass immutability check if session is already dirty
71+
if (!this.dirty.get() && !this.immutability.test(value)) {
7172
this.dirty.set(true);
7273
}
7374
return value;

clustering/web/cache/src/main/java/org/wildfly/clustering/web/cache/session/attributes/fine/FineSessionAttributes.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ public Object getAttribute(String name) {
5555

5656
if (value != null) {
5757
// If the object is mutable, we need to mutate this value on close
58-
if (!this.immutability.test(value)) {
59-
synchronized (this.updates) {
58+
synchronized (this.updates) {
59+
// Bypass immutability check if we are already updating this attribute
60+
if (!this.updates.containsKey(name) && !this.immutability.test(value)) {
6061
this.updates.put(name, value);
6162
}
6263
}

ejb3/src/main/java/org/jboss/as/ejb3/component/stateful/StatefulSessionBeanImmutability.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,43 @@
55

66
package org.jboss.as.ejb3.component.stateful;
77

8+
import java.lang.reflect.Field;
89
import java.lang.reflect.InvocationHandler;
910

1011
import org.jboss.as.ee.component.ProxyInvocationHandler;
1112
import org.jboss.invocation.proxy.ProxyFactory;
1213
import org.kohsuke.MetaInfServices;
1314
import org.wildfly.clustering.ee.Immutability;
15+
import org.wildfly.security.ParametricPrivilegedAction;
16+
import org.wildfly.security.manager.WildFlySecurityManager;
1417

1518
/**
1619
* Immutability test for EJB proxies, whose serializable placeholders are immutable.
1720
* @author Paul Ferraro
1821
*/
1922
@MetaInfServices(Immutability.class)
2023
public class StatefulSessionBeanImmutability implements Immutability {
24+
private static final ParametricPrivilegedAction<InvocationHandler, Object> GET_INVOCATION_HANDLER = new ParametricPrivilegedAction<>() {
25+
@Override
26+
public InvocationHandler run(Object object) {
27+
// Since there is a low probability that this object is actually a SFSB proxy, avoid Class.getDeclaredField(...) which will typically throw/catch a NoSuchFieldException
28+
for (Field field : object.getClass().getDeclaredFields()) {
29+
if (field.getName().equals(ProxyFactory.INVOCATION_HANDLER_FIELD)) {
30+
field.setAccessible(true);
31+
try {
32+
return (InvocationHandler) field.get(object);
33+
} catch (IllegalAccessException e) {
34+
throw new IllegalStateException(e);
35+
}
36+
}
37+
}
38+
return null;
39+
}
40+
};
2141

2242
@Override
2343
public boolean test(Object object) {
24-
try {
25-
InvocationHandler handler = ProxyFactory.getInvocationHandlerStatic(object);
26-
return handler instanceof ProxyInvocationHandler;
27-
} catch (RuntimeException e) {
28-
return false;
29-
}
44+
InvocationHandler handler = WildFlySecurityManager.doUnchecked(object, GET_INVOCATION_HANDLER);
45+
return handler instanceof ProxyInvocationHandler;
3046
}
3147
}

0 commit comments

Comments
 (0)