Skip to content

Commit

Permalink
Merge pull request #2865 from 1c-syntax/fix/memory-consumption
Browse files Browse the repository at this point in the history
Снижение потребления памяти
  • Loading branch information
nixel2007 authored Nov 22, 2022
2 parents 8ce6141 + 62fece8 commit 4c18caf
Show file tree
Hide file tree
Showing 18 changed files with 682 additions and 91 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ plugins {
id("ru.vyarus.pom") version "2.2.2"
id("com.gorylenko.gradle-git-properties") version "2.4.1"
id("io.codearte.nexus-staging") version "0.30.0"
id("me.champeau.jmh") version "0.6.6"
}

repositories {
Expand Down Expand Up @@ -80,7 +81,7 @@ dependencies {
exclude("org.antlr", "antlr-runtime")
exclude("org.glassfish", "javax.json")
}
api("com.github.1c-syntax", "utils", "0.4.0")
api("com.github.1c-syntax", "utils", "f1694d9c")
api("com.github.1c-syntax", "mdclasses", "0.10.3")
api("io.github.1c-syntax", "bsl-common-library", "0.3.0")
api("io.github.1c-syntax", "supportconf", "0.1.1")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* This file is a part of BSL Language Server.
*
* Copyright (c) 2018-2022
* Alexey Sosnoviy <[email protected]>, Nikita Fedkin <[email protected]> and contributors
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* BSL Language Server is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* BSL Language Server is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with BSL Language Server.
*/
package com.github._1c_syntax.bsl.languageserver.context.symbol;

import com.github._1c_syntax.bsl.languageserver.context.symbol.variable.VariableKind;
import com.github._1c_syntax.bsl.languageserver.utils.Ranges;
import org.eclipse.lsp4j.Range;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;

import java.util.Optional;

@State(Scope.Benchmark)
public class VariableSymbolCreate {

private Range range;

@Param({"false", "true"})
boolean shortBased;

@Setup(Level.Trial)
public void setup() {
int line = shortBased ? 100 : 60_000;
range = Ranges.create(line, 0, line, 1);
}

@Benchmark
@Fork(value = 2, warmups = 2)
@Warmup(time = 5, iterations = 3)
public void createVariableSymbols(Blackhole bh) {
var test = VariableSymbol.builder()
.name("test")
.owner(null)
.range(range)
.variableNameRange(range)
.export(true)
.kind(VariableKind.MODULE)
.description(Optional.empty())
.scope(null).build();

bh.consume(test);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public SymbolTree compute() {
currentParent = placeSymbol(topLevelSymbols, currentParent, symbol);
}

collapseChildrenCollection(moduleSymbol);

return new SymbolTree(moduleSymbol);
}

Expand All @@ -89,4 +91,12 @@ private static SourceDefinedSymbol placeSymbol(
return placeSymbol(topLevelSymbols, maybeParent.get(), symbol);
}

private static void collapseChildrenCollection(SourceDefinedSymbol symbol) {
var children = symbol.getChildren();
if (children instanceof ArrayList) {
((ArrayList<SourceDefinedSymbol>) children).trimToSize();
}

children.forEach(SymbolTreeComputer::collapseChildrenCollection);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
/*
* This file is a part of BSL Language Server.
*
* Copyright (c) 2018-2022
* Alexey Sosnoviy <[email protected]>, Nikita Fedkin <[email protected]> and contributors
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* BSL Language Server is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* BSL Language Server is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with BSL Language Server.
*/
package com.github._1c_syntax.bsl.languageserver.context.symbol;

import com.github._1c_syntax.bsl.languageserver.context.DocumentContext;
import com.github._1c_syntax.bsl.languageserver.context.symbol.variable.VariableDescription;
import com.github._1c_syntax.bsl.languageserver.context.symbol.variable.VariableKind;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.Value;
import lombok.experimental.Accessors;
import lombok.experimental.NonFinal;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.SymbolKind;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
* Общая реализация символа переменной.
*/
@Value
@NonFinal
@Builder(builderClassName = "Builder")
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@ToString(exclude = {"children", "parent"})
public abstract class AbstractVariableSymbol implements VariableSymbol {

/**
* Имя переменной.
*/
@EqualsAndHashCode.Include
String name;

/**
* Область доступности символа. Метод или модуль.
*/
SourceDefinedSymbol scope;

/**
* Файл в котором располагается переменная.
*/
@EqualsAndHashCode.Include
DocumentContext owner;

/**
* Символ, внутри которого располагается данный символ.
*/
@Getter
@Setter
@NonFinal
Optional<SourceDefinedSymbol> parent;

/**
* Список "детей" символа - символов, которые располагаются внутри данного символа.
*/
@Getter
List<SourceDefinedSymbol> children;

/**
* Тип переменной.
*/
byte kind;

/**
* Признак экспортной переменной.
*/
boolean export;

/**
* Описание переменной.
*/
Optional<VariableDescription> description;

@Override
public SymbolKind getSymbolKind() {
return SymbolKind.Variable;
}

@Override
public VariableKind getKind() {
return VariableKind.values()[kind];
}

@Override
@EqualsAndHashCode.Include
public abstract Range getVariableNameRange();

@Override
public void accept(SymbolTreeVisitor visitor) {
visitor.visitVariable(this);
}

@Override
public Range getSelectionRange() {
return getVariableNameRange();
}

public static class Builder {

@Setter
@Accessors(fluent = true, chain = true)
private VariableKind kind;

@Setter
@Accessors(fluent = true, chain = true)
Optional<SourceDefinedSymbol> parent = Optional.empty();

@Setter
@Accessors(fluent = true, chain = true)
List<SourceDefinedSymbol> children = Collections.emptyList();

private int startLine;
private int startCharacter;
private int endLine;
private int endCharacter;
private int variableNameLine;
private int variableNameStartCharacter;
private int variableNameEndCharacter;

public Builder range(Range range) {
var start = range.getStart();
var end = range.getEnd();
startLine = start.getLine();
startCharacter = start.getCharacter();
endLine = end.getLine();
endCharacter = end.getCharacter();

return this;
}

public Builder variableNameRange(Range range) {
var start = range.getStart();
var end = range.getEnd();
variableNameLine = start.getLine();
variableNameStartCharacter = start.getCharacter();
variableNameEndCharacter = end.getCharacter();

return this;
}

public VariableSymbol build() {

// Ленивое булево вычисление диапазона переменной
var shortBased = startLine <= Short.MAX_VALUE
&& endLine <= Short.MAX_VALUE
&& startCharacter <= Short.MAX_VALUE
&& endCharacter <= Short.MAX_VALUE
&& variableNameLine <= Short.MAX_VALUE
&& variableNameStartCharacter <= Short.MAX_VALUE
&& variableNameEndCharacter <= Short.MAX_VALUE;

if (shortBased) {
return new ShortBasedVariableSymbol(
name,
scope,
owner,
parent,
children,
(byte) kind.ordinal(),
export,
description,
(short) startLine,
(short) startCharacter,
(short) endLine,
(short) endCharacter,
(short) variableNameLine,
(short) variableNameStartCharacter,
(short) variableNameEndCharacter
);
} else {
return new IntBasedVariableSymbol(
name,
scope,
owner,
parent,
children,
(byte) kind.ordinal(),
export,
description,
startLine,
startCharacter,
endLine,
endCharacter,
variableNameLine,
variableNameStartCharacter,
variableNameEndCharacter
);
}
}
}

}
Loading

0 comments on commit 4c18caf

Please sign in to comment.