From 45a8030aeb627e42d04f9fa3f095522b45b5f5e1 Mon Sep 17 00:00:00 2001 From: NotMyFault Date: Mon, 2 Aug 2021 22:10:21 +0200 Subject: [PATCH] Update Upstream b9f0416 Use a Guava Cache instead of a ThreadLocal (1862) --- .../worldedit/session/request/Request.java | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/session/request/Request.java b/worldedit-core/src/main/java/com/sk89q/worldedit/session/request/Request.java index 554f0cfa8..c933b2e95 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/session/request/Request.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/session/request/Request.java @@ -19,6 +19,9 @@ package com.sk89q.worldedit.session.request; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.extension.platform.Actor; @@ -27,19 +30,15 @@ import com.sk89q.worldedit.world.World; import javax.annotation.Nullable; import java.util.Collection; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; /** - * Describes the current request using a {@link ThreadLocal}. + * Describes the current request */ public final class Request { - private static final ThreadLocal threadLocal = ThreadLocal.withInitial(Request::new); - //FAWE start - // TODO any better way to deal with this? - private static final Map requests = new ConcurrentHashMap<>(); - //FAWE end + private static final LoadingCache THREAD_TO_REQUEST = CacheBuilder.newBuilder() + .weakKeys() + .build(CacheLoader.from(Request::new)); @Nullable private World world; @@ -55,13 +54,12 @@ public final class Request { private Extent extent; //FAWE end - //FAWE start private Request() { - requests.put(Thread.currentThread(), this); } + //FAWE start public static Collection getAll() { - return requests.values(); + return THREAD_TO_REQUEST.asMap().values(); } //FAWE end @@ -160,7 +158,7 @@ public final class Request { * @return the current request */ public static Request request() { - return threadLocal.get(); + return THREAD_TO_REQUEST.getUnchecked(Thread.currentThread()); } /** @@ -168,10 +166,7 @@ public final class Request { */ public static void reset() { request().invalidate(); - threadLocal.remove(); - //FAWE start - requests.remove(Thread.currentThread()); - //FAWE end + THREAD_TO_REQUEST.invalidate(Thread.currentThread()); } /**