From 0d1f7caf5ec7f24528a7a38661b598c6abd520f1 Mon Sep 17 00:00:00 2001 From: michaeloffner Date: Wed, 13 Mar 2024 19:16:28 +0100 Subject: [PATCH] LDEV-4817 - improve BytecodeFactory cache --- .../main/java/lucee/transformer/Factory.java | 3 --- .../transformer/bytecode/BytecodeFactory.java | 26 +++++++++++-------- .../java/lucee/transformer/cfml/Data.java | 6 ++--- .../transformer/cfml/evaluator/impl/Loop.java | 2 +- .../transformer/cfml/tag/CFMLTransformer.java | 5 ++-- .../interpreter/InterpreterFactory.java | 7 ----- loader/build.xml | 2 +- loader/pom.xml | 2 +- 8 files changed, 23 insertions(+), 30 deletions(-) diff --git a/core/src/main/java/lucee/transformer/Factory.java b/core/src/main/java/lucee/transformer/Factory.java index 3fe1db02d3..6329b1eee7 100644 --- a/core/src/main/java/lucee/transformer/Factory.java +++ b/core/src/main/java/lucee/transformer/Factory.java @@ -19,7 +19,6 @@ import java.math.BigDecimal; -import lucee.runtime.config.Config; import lucee.runtime.exp.PageException; import lucee.transformer.expression.ExprBoolean; import lucee.transformer.expression.ExprInt; @@ -182,8 +181,6 @@ public abstract class Factory { public abstract void registerKey(Context bc, Expression name, boolean doUpperCase) throws TransformerException; - public abstract Config getConfig(); - public static boolean canRegisterKey(Expression name) { return name instanceof LitString; } diff --git a/core/src/main/java/lucee/transformer/bytecode/BytecodeFactory.java b/core/src/main/java/lucee/transformer/bytecode/BytecodeFactory.java index 68af0e0328..ae4bb98594 100644 --- a/core/src/main/java/lucee/transformer/bytecode/BytecodeFactory.java +++ b/core/src/main/java/lucee/transformer/bytecode/BytecodeFactory.java @@ -18,6 +18,8 @@ package lucee.transformer.bytecode; import java.math.BigDecimal; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; @@ -79,10 +81,20 @@ public class BytecodeFactory extends FactoryBase { private static final Type KEY_CONSTANTS = Type.getType(KeyConstants.class); - private static BytecodeFactory instance; + private static Map instances = new ConcurrentHashMap<>(); public static Factory getInstance(Config config) { - if (instance == null) instance = new BytecodeFactory(config == null ? ThreadLocalPageContext.getConfig() : config); + if (config == null) config = ThreadLocalPageContext.getConfig(); + String key = config.hashCode() + ":" + config.getIdentification().getId(); + BytecodeFactory instance = instances.get(key); + if (instance == null) { + synchronized (instances) { + instance = instances.get(key); + if (instance == null) { + instances.put(key, instance = new BytecodeFactory()); + } + } + } return instance; } @@ -93,16 +105,13 @@ public static Factory getInstance(Config config) { private final LitNumber NUMBER_ZERO; private final LitNumber NUMBER_ONE; - private final Config config; - - public BytecodeFactory(Config config) { + public BytecodeFactory() { TRUE = createLitBoolean(true); FALSE = createLitBoolean(false); EMPTY = createLitString(""); NULL = Null.getSingleInstance(this); NUMBER_ZERO = createLitNumber(0); NUMBER_ONE = createLitNumber(1); - this.config = config; } @Override @@ -340,11 +349,6 @@ public void registerKey(Context c, Expression name, boolean doUpperCase) throws return; } - @Override - public Config getConfig() { - return config; - } - @Override public Expression createStruct() { return new EmptyStruct(this); diff --git a/core/src/main/java/lucee/transformer/cfml/Data.java b/core/src/main/java/lucee/transformer/cfml/Data.java index 22a1b1e662..826083a936 100644 --- a/core/src/main/java/lucee/transformer/cfml/Data.java +++ b/core/src/main/java/lucee/transformer/cfml/Data.java @@ -57,8 +57,8 @@ public class Data { private Body parent; public ExprTransformer transformer; - public Data(Factory factory, Page page, SourceCode srcCode, EvaluatorPool ep, TransfomerSettings settings, TagLib[][] tlibs, FunctionLib[] flibs, TagLibTag[] scriptTags, - boolean allowLowerThan) { + public Data(Factory factory, Config config, Page page, SourceCode srcCode, EvaluatorPool ep, TransfomerSettings settings, TagLib[][] tlibs, FunctionLib[] flibs, + TagLibTag[] scriptTags, boolean allowLowerThan) { this.page = page; this.srcCode = srcCode; this.settings = settings; @@ -67,7 +67,7 @@ public Data(Factory factory, Page page, SourceCode srcCode, EvaluatorPool ep, Tr this.scriptTags = scriptTags; this.ep = ep; this.factory = factory; - this.config = factory.getConfig(); + this.config = config; this.allowLowerThan = allowLowerThan; } diff --git a/core/src/main/java/lucee/transformer/cfml/evaluator/impl/Loop.java b/core/src/main/java/lucee/transformer/cfml/evaluator/impl/Loop.java index a3ef895413..d3e12459f9 100755 --- a/core/src/main/java/lucee/transformer/cfml/evaluator/impl/Loop.java +++ b/core/src/main/java/lucee/transformer/cfml/evaluator/impl/Loop.java @@ -191,7 +191,7 @@ public void evaluate(Tag tag, TagLibTag tagLibTag, FunctionLib[] flibs) throws E transformer = tagLib.getExprTransfomer(); Page page = ASMUtil.getAncestorPage(null, tag); ConfigPro config = (ConfigPro) page.getConfig(); - Data data = new Data(BytecodeFactory.getInstance(config), page, new SourceCode(null, text, false, page.getSourceCode().getDialect()), new EvaluatorPool(), + Data data = new Data(BytecodeFactory.getInstance(config), config, page, new SourceCode(null, text, false, page.getSourceCode().getDialect()), new EvaluatorPool(), new TransfomerSettings(page.getSourceCode().getDialect() == CFMLEngine.DIALECT_CFML && config.getDotNotationUpperCase(), page.getSourceCode().getDialect() == CFMLEngine.DIALECT_CFML && config.getHandleUnQuotedAttrValueAsString(), page.ignoreScopes), null, flibs, config.getCoreTagLib(page.getSourceCode().getDialect()).getScriptTags(), false); diff --git a/core/src/main/java/lucee/transformer/cfml/tag/CFMLTransformer.java b/core/src/main/java/lucee/transformer/cfml/tag/CFMLTransformer.java index 88e3f224c0..e068976244 100755 --- a/core/src/main/java/lucee/transformer/cfml/tag/CFMLTransformer.java +++ b/core/src/main/java/lucee/transformer/cfml/tag/CFMLTransformer.java @@ -314,9 +314,8 @@ public Page transform(Factory factory, ConfigPro config, SourceCode sc, TagLib[] Page page = new Page(factory, config, sc, null, ConfigWebUtil.getEngine(config).getInfo().getFullVersionInfo(), sourceLastModified, sc.getWriteLog(), sc.getDialect() == CFMLEngine.DIALECT_LUCEE || config.getSuppressWSBeforeArg(), config.getDefaultFunctionOutput(), returnValue, ignoreScope); - TransfomerSettings settings = new TransfomerSettings(dnuc, sc.getDialect() == CFMLEngine.DIALECT_CFML && factory.getConfig().getHandleUnQuotedAttrValueAsString(), - ignoreScope); - Data data = new Data(factory, page, sc, new EvaluatorPool(), settings, _tlibs, flibs, config.getCoreTagLib(sc.getDialect()).getScriptTags(), false); + TransfomerSettings settings = new TransfomerSettings(dnuc, sc.getDialect() == CFMLEngine.DIALECT_CFML && config.getHandleUnQuotedAttrValueAsString(), ignoreScope); + Data data = new Data(factory, config, page, sc, new EvaluatorPool(), settings, _tlibs, flibs, config.getCoreTagLib(sc.getDialect()).getScriptTags(), false); transform(data, page); return page; diff --git a/core/src/main/java/lucee/transformer/interpreter/InterpreterFactory.java b/core/src/main/java/lucee/transformer/interpreter/InterpreterFactory.java index db521f1765..fe4a8e974f 100644 --- a/core/src/main/java/lucee/transformer/interpreter/InterpreterFactory.java +++ b/core/src/main/java/lucee/transformer/interpreter/InterpreterFactory.java @@ -318,11 +318,4 @@ public void registerKey(Context bc, Expression name, boolean doUpperCase) throws // TODO Auto-generated method stub } - - @Override - public Config getConfig() { - // TODO Auto-generated method stub - return null; - } - } diff --git a/loader/build.xml b/loader/build.xml index 253712d1e2..1e08845ae5 100644 --- a/loader/build.xml +++ b/loader/build.xml @@ -2,7 +2,7 @@ - + diff --git a/loader/pom.xml b/loader/pom.xml index c66ed70d92..231a796739 100644 --- a/loader/pom.xml +++ b/loader/pom.xml @@ -3,7 +3,7 @@ org.lucee lucee - 6.0.1.80-SNAPSHOT + 6.0.1.81-SNAPSHOT jar Lucee Loader Build