Skip to content

Commit

Permalink
Apply polish to StaticResourcesRecorder
Browse files Browse the repository at this point in the history
* Use setRelease/getAcquire semantics for knownPaths (as the value is only written once by the main thread). Furthermore, this variable is really hot, so the optimization does make sense.
* Remove lambdas
  • Loading branch information
geoand committed Oct 28, 2021
1 parent 42e2886 commit d610981
Showing 1 changed file with 38 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.vertx.http.runtime;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -20,12 +22,22 @@ public class StaticResourcesRecorder {
private static volatile Set<String> knownPaths;
private static volatile List<Path> hotDeploymentResourcePaths;

private static final VarHandle KNOWN_PATHS_MH;

static {
try {
KNOWN_PATHS_MH = MethodHandles.lookup().findStaticVarHandle(StaticResourcesRecorder.class, "knownPaths", Set.class);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new Error(e);
}
}

public static void setHotDeploymentResources(List<Path> resources) {
hotDeploymentResourcePaths = resources;
}

public void staticInit(Set<String> knownPaths) {
StaticResourcesRecorder.knownPaths = knownPaths;
KNOWN_PATHS_MH.setRelease(knownPaths);
}

public Consumer<Route> start() {
Expand All @@ -40,36 +52,42 @@ public Consumer<Route> start() {
staticHandler.setAllowRootFileSystemAccess(true);
staticHandler.setWebRoot(root);
staticHandler.setDefaultContentEncoding("UTF-8");
handlers.add(event -> {
try {
staticHandler.handle(event);
} catch (Exception e) {
// on Windows, the drive in file path screws up cache lookup
// so just punt to next handler
event.next();
handlers.add(new Handler<>() {
@Override
public void handle(RoutingContext event) {
try {
staticHandler.handle(event);
} catch (Exception e) {
// on Windows, the drive in file path screws up cache lookup
// so just punt to next handler
event.next();
}
}
});
}
}
if (!knownPaths.isEmpty()) {
ClassLoader currentCl = Thread.currentThread().getContextClassLoader();
StaticHandler staticHandler = StaticHandler.create(META_INF_RESOURCES).setDefaultContentEncoding("UTF-8");
handlers.add(ctx -> {
String rel = ctx.mountPoint() == null ? ctx.normalisedPath()
: ctx.normalisedPath().substring(
// let's be extra careful here in case Vert.x normalizes the mount points at some point
ctx.mountPoint().endsWith("/") ? ctx.mountPoint().length() - 1 : ctx.mountPoint().length());
if (knownPaths.contains(rel)) {
staticHandler.handle(ctx);
} else {
// make sure we don't lose the correct TCCL to Vert.x...
Thread.currentThread().setContextClassLoader(currentCl);
ctx.next();
handlers.add(new Handler<>() {
@Override
public void handle(RoutingContext ctx) {
String rel = ctx.mountPoint() == null ? ctx.normalizedPath()
: ctx.normalizedPath().substring(
// let's be extra careful here in case Vert.x normalizes the mount points at some point
ctx.mountPoint().endsWith("/") ? ctx.mountPoint().length() - 1 : ctx.mountPoint().length());
if (((Set<?>) KNOWN_PATHS_MH.getAcquire()).contains(rel)) {
staticHandler.handle(ctx);
} else {
// make sure we don't lose the correct TCCL to Vert.x...
Thread.currentThread().setContextClassLoader(currentCl);
ctx.next();
}
}
});
}

return new Consumer<Route>() {
return new Consumer<>() {

@Override
public void accept(Route route) {
Expand Down

0 comments on commit d610981

Please sign in to comment.