Skip to content

perf: optimize parsing in Connection API #3800

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions google-cloud-spanner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,19 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.37</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.cloud.spanner.connection.UnitOfWork.CallType;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheStats;
Expand All @@ -41,6 +42,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -614,16 +616,20 @@ public boolean isUpdateStatement(String sql) {

private boolean statementStartsWith(String sql, Iterable<String> checkStatements) {
Preconditions.checkNotNull(sql);
String[] tokens = sql.split("\\s+", 2);
int checkIndex = 0;
if (supportsExplain() && tokens[0].equalsIgnoreCase("EXPLAIN")) {
checkIndex = 1;
}
if (tokens.length > checkIndex) {
for (String check : checkStatements) {
if (tokens[checkIndex].equalsIgnoreCase(check)) {
return true;
}
Iterator<String> tokens = Splitter.onPattern("\\s+").split(sql).iterator();
if (!tokens.hasNext()) {
return false;
}
String token = tokens.next();
if (supportsExplain() && token.equalsIgnoreCase("EXPLAIN")) {
if (!tokens.hasNext()) {
return false;
}
token = tokens.next();
}
for (String check : checkStatements) {
if (token.equalsIgnoreCase(check)) {
return true;
}
}
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.spanner.connection;

import com.google.cloud.spanner.Dialect;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Warmup;

@Fork(value = 1, warmups = 0)
@Warmup(iterations = 1, time = 5)
@Measurement(iterations = 5, time = 5)
public class StatementParserBenchmark {
private static final Dialect dialect = Dialect.POSTGRESQL;
private static final AbstractStatementParser parser =
AbstractStatementParser.getInstance(dialect);

private static String longQueryText = generateLongQuery(100 * 1024); // 100kb

/** Generates a long SQL-looking string. */
private static String generateLongQuery(int length) {
StringBuilder sb = new StringBuilder(length + 50);
sb.append("SELECT * FROM foo WHERE 1");
while (sb.length() < length) {
sb.append(" OR abcdefghijklmnopqrstuvwxyz");
}
return sb.toString();
}

@Benchmark
public boolean isQueryTest() {
return parser.isQuery("CREATE TABLE FOO (ID INT64, NAME STRING(100)) PRIMARY KEY (ID)");
}

@Benchmark
public boolean longQueryTest() {
return parser.isQuery(longQueryText);
}
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy