Skip to content

Commit

Permalink
LDEV-5218 - remove Object serialisation cache
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Dec 20, 2024
1 parent 1c3235f commit 0c1ef20
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.apache.felix.framework.BundleWiringImpl.BundleClassLoader;
import org.objectweb.asm.ClassReader;
Expand Down Expand Up @@ -80,66 +83,39 @@ public static ClazzDynamic getInstance(Class clazz, Resource dir, Log log) throw
synchronized (clazz) {
sr = classes.get(clazz);
if (sr == null || (cd = sr.get()) == null) {
String id = generateClassLoderId(clazz);
// String id = generateClassLoderId(clazz);
// generateClassLoderId += (SystemUtil.millis() - start);
// start = SystemUtil.millis();
StringBuilder sbClassPath = new StringBuilder();
sbClassPath.append(clazz.getName().replace('.', '/')).append('-').append(id).append(".ser");
Resource ser = dir.getRealResource(getPackagePrefix() + sbClassPath.toString());

// path += (SystemUtil.millis() - start);
// start = SystemUtil.millis();
if (id != null && ser.isFile()) {
// isFile += (SystemUtil.millis() - start);
// start = SystemUtil.millis();
if (log != null) log.info("dynamic", "found metadata for [" + clazz.getName() + "]in from serialized file:" + ser);
try {
cd = (ClazzDynamic) deserialize(getClassLoader(clazz), ser.getInputStream());
cd.clazz = clazz;
// deserialize += (SystemUtil.millis() - start);
// start = SystemUtil.millis();
if (log != null) log.info("dynamic", "loaded metadata for [" + clazz.getName() + "] from serialized file:" + ser);
classes.put(clazz, new SoftReference<ClazzDynamic>(cd));
// put += (SystemUtil.millis() - start);
// start = SystemUtil.millis();
}
catch (Exception e) {
if (log != null) log.error("dynamic", e);
}
}
if (cd == null) {
try {
if (log != null) log.info("dynamic", "extract metadata from [" + clazz.getName() + "]");
cd = new ClazzDynamic(clazz, id, log);
// neww += (SystemUtil.millis() - start);
// start = SystemUtil.millis();
if (id != null) {
final ClazzDynamic _cd = cd;
ThreadUtil.getThread(() -> {
// Thread.ofVirtual().start(() -> {
try {
ser.getParentResource().mkdirs();
serialize(_cd, ser.getOutputStream());
if (log != null) {
log.info("dynamic", "stored metadata for [" + clazz.getName() + "] to serialized file:" + ser);
}
}
catch (IOException e) {
if (log != null) {
log.error("dynamic", "Failed to serialize metadata for [" + clazz.getName() + "] to file: " + ser, e);
}
}
}, true).start();

// serialize += (SystemUtil.millis() - start);
// start = SystemUtil.millis();
}
}
catch (IOException ioe) {
// print.e(ioe);
throw ioe;
}
}
// StringBuilder sbClassPath = new StringBuilder();
// sbClassPath.append(clazz.getName().replace('.', '/')).append('-').append(id).append(".ser");
// Resource ser = dir.getRealResource(getPackagePrefix() + sbClassPath.toString());

/*
* if (id != null && ser.isFile()) { if (log != null) log.info("dynamic", "found metadata for [" +
* clazz.getName() + "]in from serialized file:" + ser); try { cd = (ClazzDynamic)
* deserialize(getClassLoader(clazz), ser.getInputStream(), id + "->" + clazz.getName() + ":" +
* ser); cd.clazz = clazz; if (log != null) log.info("dynamic", "loaded metadata for [" +
* clazz.getName() + "] from serialized file:" + ser); classes.put(clazz, new
* SoftReference<ClazzDynamic>(cd)); } catch (Exception e) { if (log != null) log.error("dynamic",
* e); } }
*/
// if (cd == null) {
if (log != null) log.info("dynamic", "extract metadata from [" + clazz.getName() + "]");
cd = new ClazzDynamic(clazz, log);

// if (id != null) {
// final ClazzDynamic _cd = cd;
/*
* ThreadUtil.getThread(() -> { try { ser.getParentResource().mkdirs(); print.e("=>" +
* _cd.getClass().getName()); serialize(_cd, ser.getOutputStream()); if (log != null) {
* log.info("dynamic", "stored metadata for [" + clazz.getName() + "] to serialized file:" + ser); }
* } catch (IOException e) { if (log != null) { log.error("dynamic",
* "Failed to serialize metadata for [" + clazz.getName() + "] to file: " + ser, e); } } },
* true).start();
*/
// }

// }
}
}
}
Expand Down Expand Up @@ -182,10 +158,9 @@ public static String generateClassLoderId(Class<?> clazz) {
return null;
}

private ClazzDynamic(Class clazz, String clid, Log log) throws IOException {
private ClazzDynamic(Class clazz, Log log) throws IOException {
this.clazz = clazz;
this.clid = clid;
Map<String, FunctionMember> members = getFunctionMembers(this.clid, clazz, log);
Map<String, FunctionMember> members = getFunctionMembers(clazz, log);

LinkedList<Method> tmpMethods = new LinkedList<>();
LinkedList<Method> tmpDeclaredMethods = new LinkedList<>();
Expand Down Expand Up @@ -525,11 +500,11 @@ public List<Constructor> getDeclaredConstructors(int argumentLength) throws IOEx
return list;
}

private static Map<String, FunctionMember> getFunctionMembers(String clid, final Class clazz, Log log) throws IOException {
return _getFunctionMembers(clid, clazz, log);
private static Map<String, FunctionMember> getFunctionMembers(final Class clazz, Log log) throws IOException {
return _getFunctionMembers(clazz, log);
}

private static Map<String, FunctionMember> _getFunctionMembers(String clid, final Class clazz, Log log) throws IOException {
private static Map<String, FunctionMember> _getFunctionMembers(final Class clazz, Log log) throws IOException {

final Map<String, FunctionMember> members = new LinkedHashMap<>();
Map<String, FunctionMember> existing = membersCollection.get(clazz);
Expand Down Expand Up @@ -578,7 +553,7 @@ public void visit(int version, int access, String name, String signature, String
try {
// add(members, _getFunctionMembers(clid,
// cl.loadClass(ASMUtil.getClassName(Type.getObjectType(superName))), log));
add(members, _getFunctionMembers(clid, cl.loadClass(ASMUtil.getClassName(Type.getObjectType(superName))), log));
add(members, _getFunctionMembers(cl.loadClass(ASMUtil.getClassName(Type.getObjectType(superName))), log));
}
catch (IllegalArgumentException iae) {
String v = ASMUtil.getJavaVersionFromException(iae, null);
Expand All @@ -602,7 +577,7 @@ public void visit(int version, int access, String name, String signature, String
try {
// add(members, _getFunctionMembers(clid,
// cl.loadClass(ASMUtil.getClassName(Type.getObjectType(interf))), log));
add(members, _getFunctionMembers(clid, cl.loadClass(ASMUtil.getClassName(Type.getObjectType(interf))), log));
add(members, _getFunctionMembers(cl.loadClass(ASMUtil.getClassName(Type.getObjectType(interf))), log));
}
catch (Exception e) {
if (log != null) log.error("dynamic", e);
Expand Down Expand Up @@ -847,7 +822,23 @@ public static void serialize(Serializable o, OutputStream os) throws IOException
}
}

public static Object deserialize(ClassLoader cl, InputStream is) throws IOException, ClassNotFoundException {
private static Object deserializeAsync(ClassLoader cl, InputStream is, String path) throws TimeoutException, InterruptedException, ExecutionException {
return ThreadUtil.createExecutorService().submit(() -> {
ObjectInputStream ois = null;
Object o;
try {
ois = new ObjectInputStreamImpl(cl, is);
o = ois.readObject();
}
finally {
IOUtil.close(ois);
}
return o;
}).get(10, TimeUnit.MILLISECONDS);
}

private static Object deserialize(ClassLoader cl, InputStream is) throws IOException, ClassNotFoundException {

ObjectInputStream ois = null;
Object o = null;
try {
Expand Down
2 changes: 1 addition & 1 deletion loader/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project default="core" basedir="." name="Lucee"
xmlns:resolver="antlib:org.apache.maven.resolver.ant">

<property name="version" value="6.2.0.238-SNAPSHOT"/>
<property name="version" value="6.2.0.239-SNAPSHOT"/>

<taskdef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml">
<classpath>
Expand Down
2 changes: 1 addition & 1 deletion loader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.lucee</groupId>
<artifactId>lucee</artifactId>
<version>6.2.0.238-SNAPSHOT</version>
<version>6.2.0.239-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lucee Loader Build</name>
Expand Down

0 comments on commit 0c1ef20

Please sign in to comment.