[#1632] Improve the performance of various DefaultRenderContext methods,

by locally caching Settings values locally
This commit is contained in:
Lukas Eder 2012-07-27 13:33:15 +02:00
parent a120437579
commit 9caed2164b
2 changed files with 49 additions and 32 deletions

View File

@ -83,45 +83,53 @@ extends BaseTest<A, AP, B, S, B2S, BS, L, X, DATE, BOOL, D, T, U, I, IPK, T658,
// This benchmark is contributed by "jjYBdx4IL" on GitHub:
// https://github.com/jOOQ/jOOQ/issues/1625
Factory create = create();
create.getSettings().setExecuteLogging(false);
create.getSettings().setExecuteListeners(Collections.<String>emptyList());
// Dry-run to avoid side-effects
testBenchmarkFullExecution(create, 1);
testBenchmarkReuseSelect(create, 1);
testBenchmarkReuseSQLString(create, 1);
System.in.read();
StopWatch watch = new StopWatch();
watch.splitInfo("Benchmark start");
testBenchmarkFullExecution(create);
testBenchmarkFullExecution(create, REPETITIONS);
watch.splitInfo("Full re-execution");
testBenchmarkReuseSelect(create);
testBenchmarkReuseSelect(create, REPETITIONS);
watch.splitInfo("Reuse select");
testBenchmarkReuseSQLString(create);
testBenchmarkReuseSQLString(create, REPETITIONS);
watch.splitInfo("Reuse SQL String");
}
private void testBenchmarkReuseSQLString(Factory create) throws Exception {
private void testBenchmarkReuseSQLString(Factory create, int repetitions) throws Exception {
String sql = createSelect(create).getSQL(false);
PreparedStatement pst = getConnection().prepareStatement(sql);
pst.setLong(1, 1);
pst.setString(2, RANDOM);
for (int i = 0; i < REPETITIONS; i++) {
for (int i = 0; i < repetitions; i++) {
pst.executeQuery();
}
pst.close();
}
private void testBenchmarkReuseSelect(Factory create) {
private void testBenchmarkReuseSelect(Factory create, int repetitions) {
Select<?> scs = createSelect(create);
for (int i = 0; i < REPETITIONS; i++) {
for (int i = 0; i < repetitions; i++) {
scs.execute();
}
}
private void testBenchmarkFullExecution(Factory create) {
for (int i = 0; i < REPETITIONS; i++) {
private void testBenchmarkFullExecution(Factory create, int repetitions) {
for (int i = 0; i < repetitions; i++) {
createSelect(create).execute();
}
}

View File

@ -46,6 +46,7 @@ import org.jooq.RenderContext;
import org.jooq.SQLDialect;
import org.jooq.conf.RenderKeywordStyle;
import org.jooq.conf.RenderNameStyle;
import org.jooq.conf.Settings;
import org.jooq.tools.StringUtils;
/**
@ -56,22 +57,32 @@ class DefaultRenderContext extends AbstractContext<RenderContext> implements Ren
/**
* Generated UID
*/
private static final long serialVersionUID = -8358225526567622252L;
private static final long serialVersionUID = -8358225526567622252L;
private final StringBuilder sql;
private boolean inline;
private boolean renderNamedParams;
private boolean qualify = true;
private int alias;
private CastMode castMode = CastMode.DEFAULT;
private SQLDialect[] castDialects;
private int indent;
private Stack<Integer> indentLock = new Stack<Integer>();
private final StringBuilder sql;
private boolean inline;
private boolean renderNamedParams;
private boolean qualify = true;
private int alias;
private CastMode castMode = CastMode.DEFAULT;
private SQLDialect[] castDialects;
private int indent;
private Stack<Integer> indentLock = new Stack<Integer>();
// [#1632] Cached values from Settings
private final RenderKeywordStyle renderKeywordStyle;
private final RenderNameStyle renderNameStyle;
private final boolean renderFormatted;
DefaultRenderContext(Configuration configuration) {
super(configuration);
Settings settings = configuration.getSettings();
this.sql = new StringBuilder();
this.renderKeywordStyle = settings.getRenderKeywordStyle();
this.renderFormatted = Boolean.TRUE.equals(settings.isRenderFormatted());
this.renderNameStyle = settings.getRenderNameStyle();
}
DefaultRenderContext(RenderContext context) {
@ -109,7 +120,7 @@ class DefaultRenderContext extends AbstractContext<RenderContext> implements Ren
@Override
public final RenderContext keyword(String keyword) {
if (RenderKeywordStyle.UPPER == getSettings().getRenderKeywordStyle()) {
if (RenderKeywordStyle.UPPER == renderKeywordStyle) {
return sql(keyword.toUpperCase());
}
else {
@ -119,7 +130,7 @@ class DefaultRenderContext extends AbstractContext<RenderContext> implements Ren
@Override
public final RenderContext sql(String s) {
if (s != null && Boolean.TRUE.equals(getSettings().isRenderFormatted())) {
if (s != null && renderFormatted) {
sql.append(s.replaceAll("[\\n\\r]", "$0" + indentation()));
}
else {
@ -143,7 +154,7 @@ class DefaultRenderContext extends AbstractContext<RenderContext> implements Ren
@Override
public final RenderContext formatNewLine() {
if (Boolean.TRUE.equals(getSettings().isRenderFormatted())) {
if (renderFormatted) {
sql.append("\n");
sql.append(indentation());
}
@ -157,7 +168,7 @@ class DefaultRenderContext extends AbstractContext<RenderContext> implements Ren
@Override
public final RenderContext formatSeparator() {
if (Boolean.TRUE.equals(getSettings().isRenderFormatted())) {
if (renderFormatted) {
formatNewLine();
}
else {
@ -179,7 +190,7 @@ class DefaultRenderContext extends AbstractContext<RenderContext> implements Ren
@Override
public final RenderContext formatIndentStart(int i) {
if (Boolean.TRUE.equals(getSettings().isRenderFormatted())) {
if (renderFormatted) {
indent += i;
}
@ -188,7 +199,7 @@ class DefaultRenderContext extends AbstractContext<RenderContext> implements Ren
@Override
public final RenderContext formatIndentEnd(int i) {
if (Boolean.TRUE.equals(getSettings().isRenderFormatted())) {
if (renderFormatted) {
indent -= i;
}
@ -197,7 +208,7 @@ class DefaultRenderContext extends AbstractContext<RenderContext> implements Ren
@Override
public final RenderContext formatIndentLockStart() {
if (Boolean.TRUE.equals(getSettings().isRenderFormatted())) {
if (renderFormatted) {
indentLock.push(indent);
String[] lines = sql.toString().split("[\\n\\r]");
indent = lines[lines.length - 1].length();
@ -208,7 +219,7 @@ class DefaultRenderContext extends AbstractContext<RenderContext> implements Ren
@Override
public final RenderContext formatIndentLockEnd() {
if (Boolean.TRUE.equals(getSettings().isRenderFormatted())) {
if (renderFormatted) {
indent = indentLock.pop();
}
@ -223,15 +234,13 @@ class DefaultRenderContext extends AbstractContext<RenderContext> implements Ren
return this;
}
RenderNameStyle style = configuration.getSettings().getRenderNameStyle();
if (RenderNameStyle.LOWER == style) {
if (RenderNameStyle.LOWER == renderNameStyle) {
sql(literal.toLowerCase());
}
else if (RenderNameStyle.UPPER == style) {
else if (RenderNameStyle.UPPER == renderNameStyle) {
sql(literal.toUpperCase());
}
else if (RenderNameStyle.AS_IS == style) {
else if (RenderNameStyle.AS_IS == renderNameStyle) {
sql(literal);
}
else {