Skip to content

Commit

Permalink
LDEV-3854 - remove sync and reorganize the code
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Sep 5, 2023
1 parent 9632fe5 commit 4e2fb21
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 72 deletions.
63 changes: 17 additions & 46 deletions core/src/main/java/lucee/runtime/MappingImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.Serializable;
import java.lang.instrument.UnmodifiableClassException;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -248,9 +247,7 @@ private PhysicalClassLoader touchPhysicalClassLoader(boolean forComponent) throw
}
else if ((forComponent ? pclCFC : pclCFM).getSize(true) > (forComponent ? MAX_SIZE_CFC : MAX_SIZE_CFM)) {
PhysicalClassLoader pcl = forComponent ? pclCFC : pclCFM;
synchronized (pageSourcePool) {
pageSourcePool.clearPages(pcl);
}
pageSourcePool.clearPages(pcl);
pcl.clear();
if (forComponent) pclCFC = new PhysicalClassLoader(config, getClassRootDirectory());
else pclCFM = new PhysicalClassLoader(config, getClassRootDirectory());
Expand Down Expand Up @@ -294,21 +291,15 @@ public Class<?> getPhysicalClass(String className, byte[] code) throws IOExcepti
* @param cl
*/
public void clearPages(ClassLoader cl) {
synchronized (pageSourcePool) {
pageSourcePool.clearPages(cl);
}
pageSourcePool.clearPages(cl);
}

public void clearUnused(Config config) {
synchronized (pageSourcePool) {
pageSourcePool.clearUnused(config);
}
pageSourcePool.clearUnused(config);
}

public void resetPages(ClassLoader cl) {
synchronized (pageSourcePool) {
pageSourcePool.resetPages(cl);
}
pageSourcePool.resetPages(cl);
}

@Override
Expand Down Expand Up @@ -400,49 +391,31 @@ else if (realPath.startsWith("./")) {

@Override
public PageSource getPageSource(String path, boolean isOut) {
synchronized (pageSourcePool) {
PageSource source = pageSourcePool.getPageSource(path, true);
if (source != null) return source;
PageSource source = pageSourcePool.getPageSource(path, true);
if (source != null) return source;

PageSourceImpl newSource = new PageSourceImpl(this, path, isOut);
pageSourcePool.setPage(path, newSource);
PageSourceImpl newSource = new PageSourceImpl(this, path, isOut);
pageSourcePool.setPage(path, newSource);

return newSource;// new PageSource(this,path);
}
return newSource;// new PageSource(this,path);
}

// to not delete,used for argus monitor!
public PageSourcePool getPageSourcePool() {
synchronized (pageSourcePool) {
return pageSourcePool;
}
return pageSourcePool;
}

public Array getDisplayPathes(Array arr) throws PageException {
synchronized (pageSourcePool) {
String[] keys = pageSourcePool.keys();
PageSourceImpl ps;
for (int y = 0; y < keys.length; y++) {
ps = (PageSourceImpl) pageSourcePool.getPageSource(keys[y], false);
if (ps != null && ps.isLoad()) arr.append(ps.getDisplayPath());
}
return arr;
List<PageSource> values = pageSourcePool.values(true);
for (PageSource ps: values) {
if (ps != null) arr.append(ps.getDisplayPath());
}
return arr;

}

public List<PageSource> getPageSources(boolean loaded) {
List<PageSource> list = new ArrayList<>();
synchronized (pageSourcePool) {
String[] keys = pageSourcePool.keys();
PageSourceImpl ps;
for (int y = 0; y < keys.length; y++) {
ps = (PageSourceImpl) pageSourcePool.getPageSource(keys[y], false);
if (ps != null) {
if (!loaded || ps.isLoad()) list.add(ps);
}
}
}
return list;
return pageSourcePool.values(loaded);
}

@Override
Expand Down Expand Up @@ -599,9 +572,7 @@ public int getListenerType() {
}

public void flush() {
synchronized (pageSourcePool) {
pageSourcePool.clear();
}
pageSourcePool.clear();
}

public SerMapping toSerMapping() {
Expand Down
77 changes: 53 additions & 24 deletions core/src/main/java/lucee/runtime/PageSourcePool.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
package lucee.runtime;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -66,9 +69,12 @@ public PageSourcePool() {
*/
public PageSource getPageSource(String key, boolean updateAccesTime) { // DO NOT CHANGE INTERFACE (used by Argus Monitor)
SoftReference<PageSource> tmp = pageSources.get(key.toLowerCase());
PageSource ps = tmp == null ? null : tmp.get();
if (ps == null) return null;
if (updateAccesTime) ps.setLastAccessTime();
if (tmp == null) return null;
PageSource ps = tmp.get();
if (ps == null) {
pageSources.remove(key.toLowerCase());
return null;
}
return ps;
}

Expand Down Expand Up @@ -103,6 +109,19 @@ public String[] keys() {
return set.toArray(new String[set.size()]);
}

public List<PageSource> values(boolean loaded) {
List<PageSource> vals = new ArrayList<>();
if (pageSources == null) return vals;

PageSource ps;
for (SoftReference<PageSource> sr: pageSources.values()) {
ps = sr.get();
if (ps != null && (!loaded || ((PageSourceImpl) ps).isLoad())) vals.add(ps);

}
return vals;
}

/**
* removes a page from the page pool
*
Expand Down Expand Up @@ -144,20 +163,30 @@ public boolean flushPage(String key) {
* @return returns the size of the pool
*/
public int size() {
return pageSources.size();
int size = 0;

for (Entry<String, SoftReference<PageSource>> entry: pageSources.entrySet()) {
if (entry.getValue().get() != null) size++;
else {
pageSources.remove(entry.getKey());
}
}
return size;
}

/**
* @return returns if pool is empty or not
*/
public boolean isEmpty() {
return pageSources.isEmpty();
return size() > 0;
}

/**
* clear unused pages from page pool
*/

public void clearUnused(Config config) {

if (size() > maxSize) {
LogUtil.log(config, Log.LEVEL_INFO, PageSourcePool.class.getName(), "PagePool size [" + size() + "] has exceeded max size [" + maxSize + "]. Clearing unused...");
String[] keys = keys();
Expand All @@ -184,6 +213,8 @@ public void clearUnused(Config config) {
@Override
public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) {
maxlevel--;

size(); // calling size because it get rid of all the blanks
Iterator<SoftReference<PageSource>> it = pageSources.values().iterator();

DumpTable table = new DumpTable("#FFCC00", "#FFFF00", "#000000");
Expand All @@ -207,36 +238,34 @@ public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties
* @param cl
*/
public void clearPages(ClassLoader cl) {
Iterator<SoftReference<PageSource>> it = this.pageSources.values().iterator();
PageSourceImpl psi;
SoftReference<PageSource> sr;
while (it.hasNext()) {
sr = it.next();
psi = sr == null ? null : (PageSourceImpl) sr.get();
if (psi == null) continue;
if (cl != null) psi.clear(cl);
else psi.clear();
}
clearResetPages(cl, true);
}

public void resetPages(ClassLoader cl) {
Iterator<SoftReference<PageSource>> it = this.pageSources.values().iterator();
clearResetPages(cl, false);
}

public void clear() {
clearResetPages(null, true);
}

private void clearResetPages(ClassLoader cl, boolean clear) {
PageSourceImpl psi;
SoftReference<PageSource> sr;
while (it.hasNext()) {
sr = it.next();
for (Entry<String, SoftReference<PageSource>> entry: pageSources.entrySet()) {
sr = entry.getValue();
psi = sr == null ? null : (PageSourceImpl) sr.get();
if (psi == null) continue;
if (psi == null) {
if (sr != null) pageSources.remove(entry.getKey());
continue;
}

if (cl != null) psi.clear(cl);
else if (clear) psi.clear();
else psi.resetLoaded();
}
}

public void clear() {
clearPages(null);
// pageSources.clear();
}

public int getMaxSize() {
return maxSize;
}
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.0.0.534-SNAPSHOT"/>
<property name="version" value="6.0.0.535-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.0.0.534-SNAPSHOT</version>
<version>6.0.0.535-SNAPSHOT</version>
<packaging>jar</packaging>

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

0 comments on commit 4e2fb21

Please sign in to comment.