Skip to content

Commit

Permalink
fix: performance improvements by removing calls to positionAddOffset …
Browse files Browse the repository at this point in the history
…and removing redundant parsing
  • Loading branch information
Mangern committed Aug 8, 2024
1 parent 2ccfd0a commit d79c181
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,9 @@ INJECT SchemaParser:
* @param linguistics What to use for tokenizing.
*/
private SubLanguageData newIndexingOperation(boolean multiline, Linguistics linguistics, Map<String, Embedder> embedders) throws ParseException {
// Find the CharStream to and set input to this stream, by converting it to a SimpleCharStream - Theodor
// SimpleCharStream input = new SimpleCharStream("summary | index"); //lastConsumedToken.getTokenSource();

TokenSource tokS = lastConsumedToken.getTokenSource();
String inputString = tokS.toString();
int startSplit = lastConsumedToken.getEndOffset();
int endSplit = startSplit;
int leadingStripped = 0;

if (multiline) {
Expand Down Expand Up @@ -136,7 +132,7 @@ INJECT SchemaParser:
}
}

endSplit = lastConsumedToken.getEndOffset();
int endSplit = lastConsumedToken.getEndOffset();

inputString = inputString.substring(startSplit, endSplit);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,12 @@ public List<Symbol> findSymbols(Symbol scope, SymbolType type, String shortIdent
* Will not search inheritance graphs.
*/
private Optional<Symbol> findSymbolInConcreteScope(Symbol scope, SymbolType type, String shortIdentifier) {
List<Symbol> match = symbolDefinitions.get(type)
.stream()
.filter(symbolDefinition -> scope.equals(symbolDefinition.getScope()) && symbolDefinition.getShortIdentifier().equals(shortIdentifier))
.toList();
if (match.isEmpty()) return Optional.empty();
return Optional.of(match.get(0));
for (Symbol symbolDefinition : symbolDefinitions.get(type)) {
if (!symbolDefinition.getShortIdentifier().equals(shortIdentifier)) continue;
if (!scope.equals(symbolDefinition.getScope())) continue;
return Optional.of(symbolDefinition);
}
return Optional.empty();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import ai.vespa.schemals.common.FileUtils;
import ai.vespa.schemals.common.SchemaDiagnostic;
import ai.vespa.schemals.common.SchemaDiagnostic.DiagnosticCode;
import ai.vespa.schemals.common.StringUtils;
import ai.vespa.schemals.index.SchemaIndex;
import ai.vespa.schemals.index.Symbol;
import ai.vespa.schemals.index.Symbol.SymbolType;
Expand Down Expand Up @@ -193,30 +192,15 @@ public SchemaNode getRootNode() {
public static ParseResult parseContent(ParseContext context) {
CharSequence sequence = context.content();

SchemaParser parserStrict = new SchemaParser(context.logger(), context.fileURI(), sequence);
parserStrict.setParserTolerant(false);

ArrayList<Diagnostic> diagnostics = new ArrayList<Diagnostic>();
SchemaParser parserFaultTolerant = new SchemaParser(context.fileURI(), sequence);

try {
parserStrict.Root();
parserFaultTolerant.Root();
} catch (ParseException e) {

Node.TerminalNode node = e.getToken();

Range range = CSTUtils.getNodeRange(node);
String message = e.getMessage();

diagnostics.add(new SchemaDiagnostic.Builder()
.setRange(range)
.setMessage(message)
.setSeverity(DiagnosticSeverity.Error)
.build());


// Ignore, marked by "Dirty Node"
} catch (IllegalArgumentException e) {
// Complex error, invalidate the whole document

// Some internal error from config-model Parsed*, invalidate whole document.
diagnostics.add(new SchemaDiagnostic.Builder()
.setRange(
new Range(
Expand All @@ -226,26 +210,13 @@ public static ParseResult parseContent(ParseContext context) {
)
.setMessage(e.getMessage())
.setSeverity(DiagnosticSeverity.Error)
.build()
);
return ParseResult.parsingFailed(diagnostics);
}

SchemaParser parserFaultTolerant = new SchemaParser(context.fileURI(), sequence);
try {
parserFaultTolerant.Root();
} catch (ParseException e) {
// Ignore
} catch (IllegalArgumentException e) {
// Ignore
.build());
}

Node node = parserFaultTolerant.rootNode();

var tolerantResult = parseCST(node, context);

context.logger().println("After parseCST, present: " + tolerantResult.CST().isPresent());

diagnostics.addAll(InheritanceResolver.resolveInheritances(context));

for (SchemaNode typeNode : context.unresolvedTypeNodes()) {
Expand All @@ -269,13 +240,9 @@ public static ParseResult parseContent(ParseContext context) {
return new ParseResult(diagnostics, tolerantResult.CST());
}

private static ArrayList<Diagnostic> traverseCST(SchemaNode node, ParseContext context) {


ArrayList<Diagnostic> ret = new ArrayList<>();

private static void traverseCST(SchemaNode node, ParseContext context, List<Diagnostic> diagnostics) {
if (node.containsOtherLanguageData(LanguageType.INDEXING)) {
SchemaNode indexingNode = parseIndexingScript(node, context, ret);
SchemaNode indexingNode = parseIndexingScript(node, context, diagnostics);
if (indexingNode != null) {
node.addChild(indexingNode);
}
Expand All @@ -286,18 +253,16 @@ private static ArrayList<Diagnostic> traverseCST(SchemaNode node, ParseContext c
// String nodeString = node.get(0).get(0).getText();
// Position rankExpressionStart = CSTUtils.addPositions(nodeRange.getStart(), new Position(0, nodeString.length()));

SchemaRankExpressionParser.embedCST(context, node, ret);
SchemaRankExpressionParser.embedCST(context, node, diagnostics);
}

for (Identifier identifier : context.identifiers()) {
ret.addAll(identifier.identify(node));
diagnostics.addAll(identifier.identify(node));
}

for (int i = 0; i < node.size(); ++i) {
ret.addAll(traverseCST(node.get(i), context));
traverseCST(node.get(i), context, diagnostics);
}

return ret;
}

public static ParseResult parseCST(Node node, ParseContext context) {
Expand All @@ -307,14 +272,14 @@ public static ParseResult parseCST(Node node, ParseContext context) {
SchemaNode CST = new SchemaNode(node);
ArrayList<Diagnostic> errors = new ArrayList<>();
try {
errors = traverseCST(CST, context);
traverseCST(CST, context, errors);
} catch(Exception ex) {
ex.printStackTrace(context.logger());
}
return new ParseResult(errors, Optional.of(CST));
}

private static SchemaNode parseIndexingScript(SchemaNode indexingElmNode, ParseContext context, ArrayList<Diagnostic> diagnostics) {
private static SchemaNode parseIndexingScript(SchemaNode indexingElmNode, ParseContext context, List<Diagnostic> diagnostics) {
SubLanguageData script = indexingElmNode.getILScript();
if (script == null) return null;

Expand All @@ -323,7 +288,7 @@ private static SchemaNode parseIndexingScript(SchemaNode indexingElmNode, ParseC
IndexingParser parser = new IndexingParser(context.logger(), context.fileURI(), sequence);
parser.setParserTolerant(false);

Position scriptStart = StringUtils.positionAddOffset(context.content(), indexingStart, script.leadingStripped());
Position scriptStart = new Position(indexingStart.getLine(), indexingStart.getCharacter() + script.leadingStripped());

try {
var expression = parser.root();
Expand Down
Loading

0 comments on commit d79c181

Please sign in to comment.