Skip to content

Commit

Permalink
use GC nearly everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
WebFreak001 committed Oct 13, 2022
1 parent e7c7f86 commit 5c529f3
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 117 deletions.
2 changes: 1 addition & 1 deletion dsymbol/src/dsymbol/builtin/symbols.d
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import dsymbol.string_interning;
import dsymbol.symbol;
import std.experimental.allocator.mallocator : Mallocator;

alias SymbolsAllocator = Mallocator;
private alias SymbolsAllocator = Mallocator;

/**
* Symbols for the built in types
Expand Down
48 changes: 13 additions & 35 deletions dsymbol/src/dsymbol/conversion/first.d
Original file line number Diff line number Diff line change
Expand Up @@ -53,32 +53,22 @@ import std.typecons : Rebindable;
*/
final class FirstPass : ASTVisitor
{
alias SymbolAllocator = GCAllocator; // NOTE using First`Pass.symbolAllocator` instead fails when analyzing Phobos master
alias ScopeAllocator = GCAllocator; // NOTE using `Mallocator` instead fails when analyzing Phobos master

/**
* Params:
* mod = the module to visit
* symbolFile = path to the file being converted
* symbolAllocator = allocator used for the auto-complete symbols
* semanticAllocator = allocator used for semantic symbols
*/
this(const Module mod, istring symbolFile, RCIAllocator symbolAllocator,
RCIAllocator semanticAllocator,
this(const Module mod, istring symbolFile,
ModuleCache* cache, CacheEntry* entry = null)
in
{
assert(mod);
assert(!symbolAllocator.isNull);
assert(!semanticAllocator.isNull);
assert(cache);
}
do
{
this.mod = mod;
this.symbolFile = symbolFile;
this.symbolAllocator = symbolAllocator;
this.semanticAllocator = semanticAllocator;
this.entry = entry;
this.cache = cache;
}
Expand Down Expand Up @@ -146,7 +136,7 @@ final class FirstPass : ASTVisitor

if (dec.functionBody !is null)
{
pushFunctionScope(dec.functionBody, semanticAllocator,
pushFunctionScope(dec.functionBody,
dec.name.index + dec.name.text.length);
scope (exit) popScope();
processParameters(currentSymbol, dec.returnType,
Expand Down Expand Up @@ -374,7 +364,7 @@ final class FirstPass : ASTVisitor
rootSymbol = allocateSemanticSymbol(null, CompletionKind.moduleName,
symbolFile);
currentSymbol = rootSymbol;
moduleScope = GCAllocator.instance.make!Scope(0, uint.max); // NOTE using `semanticAllocator` here fails as `Segmentation fault (core dumped)`
moduleScope = GCAllocator.instance.make!Scope(0, uint.max);
currentScope = moduleScope;
auto objectLocation = cache.resolveImportLocation("object");
if (objectLocation is null)
Expand Down Expand Up @@ -444,7 +434,7 @@ final class FirstPass : ASTVisitor
scope(exit) structFieldNames = move(savedStructFieldNames);
scope(exit) structFieldTypes = move(savedStructFieldTypes);

DSymbol* thisSymbol = SymbolAllocator.instance.make!DSymbol(THIS_SYMBOL_NAME,
DSymbol* thisSymbol = GCAllocator.instance.make!DSymbol(THIS_SYMBOL_NAME,
CompletionKind.variableName, currentSymbol.acSymbol);
thisSymbol.location = currentScope.startLocation;
thisSymbol.symbolFile = symbolFile;
Expand Down Expand Up @@ -497,7 +487,7 @@ final class FirstPass : ASTVisitor
auto s = currentScope.getSymbolsByName(ip);
if (s.length == 0)
{
currentImportSymbol = SymbolAllocator.instance.make!DSymbol(ip, kind);
currentImportSymbol = GCAllocator.instance.make!DSymbol(ip, kind);
currentScope.addSymbol(currentImportSymbol, true);
if (last)
{
Expand All @@ -514,7 +504,7 @@ final class FirstPass : ASTVisitor
auto s = currentImportSymbol.getPartsByName(ip);
if (s.length == 0)
{
auto sym = SymbolAllocator.instance.make!DSymbol(ip, kind);
auto sym = GCAllocator.instance.make!DSymbol(ip, kind);
currentImportSymbol.addChild(sym, true);
currentImportSymbol = sym;
if (last)
Expand Down Expand Up @@ -781,9 +771,6 @@ final class FirstPass : ASTVisitor
/// The module
SemanticSymbol* rootSymbol;

/// Allocator used for symbol allocation
RCIAllocator symbolAllocator;

/// Number of symbols allocated
uint symbolsAllocated;

Expand Down Expand Up @@ -823,7 +810,7 @@ private:
{
assert (startLocation < uint.max);
assert (endLocation < uint.max || endLocation == size_t.max);
Scope* s = ScopeAllocator.instance.make!Scope(cast(uint) startLocation, cast(uint) endLocation);
Scope* s = GCAllocator.instance.make!Scope(cast(uint) startLocation, cast(uint) endLocation);
s.parent = currentScope;
currentScope.children.insert(s);
currentScope = s;
Expand All @@ -834,10 +821,9 @@ private:
currentScope = currentScope.parent;
}

void pushFunctionScope(const FunctionBody functionBody,
RCIAllocator semanticAllocator, size_t scopeBegin)
void pushFunctionScope(const FunctionBody functionBody, size_t scopeBegin)
{
Scope* s = ScopeAllocator.instance.make!Scope(cast(uint) scopeBegin,
Scope* s = GCAllocator.instance.make!Scope(cast(uint) scopeBegin,
cast(uint) functionBody.endLocation);
s.parent = currentScope;
currentScope.children.insert(s);
Expand Down Expand Up @@ -926,8 +912,7 @@ private:

if (functionBody !is null)
{
pushFunctionScope(functionBody, semanticAllocator,
location + 4); // 4 == "this".length
pushFunctionScope(functionBody, location + 4); // 4 == "this".length
scope(exit) popScope();
currentSymbol = symbol;
functionBody.accept(this);
Expand All @@ -951,7 +936,7 @@ private:

if (functionBody !is null)
{
pushFunctionScope(functionBody, semanticAllocator, location + 4); // 4 == "this".length
pushFunctionScope(functionBody, location + 4); // 4 == "this".length
scope(exit) popScope();
currentSymbol = symbol;
functionBody.accept(this);
Expand Down Expand Up @@ -1108,17 +1093,12 @@ private:

SemanticSymbol* allocateSemanticSymbol(string name, CompletionKind kind,
istring symbolFile, size_t location = 0)
in
{
assert (!symbolAllocator.isNull);
}
do
{
DSymbol* acSymbol = SymbolAllocator.instance.make!DSymbol(istring(name), kind);
DSymbol* acSymbol = GCAllocator.instance.make!DSymbol(istring(name), kind);
acSymbol.location = location;
acSymbol.symbolFile = symbolFile;
symbolsAllocated++;
return SymbolAllocator.instance.make!SemanticSymbol(acSymbol); // NOTE using semanticAllocator here breaks when analysing phobos as: `Segmentation fault (core dumped)‘’
return GCAllocator.instance.make!SemanticSymbol(acSymbol);
}

void addTypeToLookups(ref TypeLookups lookups,
Expand Down Expand Up @@ -1207,8 +1187,6 @@ private:

const Module mod;

RCIAllocator semanticAllocator;

Rebindable!(const ExpressionNode) feExpression;

CacheEntry* entry;
Expand Down
20 changes: 10 additions & 10 deletions dsymbol/src/dsymbol/conversion/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,38 @@

module dsymbol.conversion;

import dparse.ast;
import dparse.lexer;
import dparse.parser;
import dparse.rollback_allocator;
import dsymbol.cache_entry;
import dsymbol.conversion.first;
import dsymbol.conversion.second;
import dsymbol.modulecache;
import dsymbol.scope_;
import dsymbol.semantic;
import dsymbol.string_interning;
import dsymbol.symbol;
import dsymbol.semantic;
import dparse.ast;
import dparse.lexer;
import dparse.parser;
import dparse.rollback_allocator;
import std.algorithm;
import std.experimental.allocator;

/**
* Used by autocompletion.
*/
ScopeSymbolPair generateAutocompleteTrees(const(Token)[] tokens,
RCIAllocator symbolAllocator, RollbackAllocator* parseAllocator,
RollbackAllocator* parseAllocator,
size_t cursorPosition, ref ModuleCache cache)
{
Module m = parseModuleForAutocomplete(tokens, internString("stdin"),
parseAllocator, cursorPosition);

scope first = new FirstPass(m, internString("stdin"), symbolAllocator,
symbolAllocator, &cache);
scope first = new FirstPass(m, internString("stdin"), &cache);
first.run();

secondPass(first.rootSymbol, first.moduleScope, cache);
auto r = first.rootSymbol.acSymbol;
auto r = move(first.rootSymbol.acSymbol);
typeid(SemanticSymbol).destroy(first.rootSymbol);
return ScopeSymbolPair(r, first.moduleScope);
return ScopeSymbolPair(r, move(first.moduleScope));
}

struct ScopeSymbolPair
Expand Down
16 changes: 7 additions & 9 deletions dsymbol/src/dsymbol/conversion/second.d
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ import std.experimental.logger;
import dparse.ast;
import dparse.lexer;

alias SymbolAllocator = GCAllocator; // NOTE using cache.symbolAllocator instead fails when analyzing Phobos master

void secondPass(SemanticSymbol* currentSymbol, Scope* moduleScope, ref ModuleCache cache)
{
with (CompletionKind) final switch (currentSymbol.acSymbol.kind)
Expand Down Expand Up @@ -115,7 +113,7 @@ do
if (moduleSymbol is null)
{
tryAgain:
DeferredSymbol* deferred = TypeLookupsAllocator.instance.make!DeferredSymbol(acSymbol);
DeferredSymbol* deferred = DeferredSymbolsAllocator.instance.make!DeferredSymbol(acSymbol);
deferred.typeLookups.insert(typeLookups[]);
// Get rid of the old references to the lookups, this new deferred
// symbol owns them now
Expand Down Expand Up @@ -191,7 +189,7 @@ do
break;
immutable qualifier = isAssoc ? SymbolQualifier.assocArray :
(isFunction ? SymbolQualifier.func : SymbolQualifier.array);
lastSuffix = SymbolAllocator.instance.make!DSymbol(back, CompletionKind.dummy, lastSuffix);
lastSuffix = GCAllocator.instance.make!DSymbol(back, CompletionKind.dummy, lastSuffix);
lastSuffix.qualifier = qualifier;
lastSuffix.ownType = true;
if (isFunction)
Expand Down Expand Up @@ -335,13 +333,13 @@ void resolveInheritance(DSymbol* symbol, ref TypeLookups typeLookups,
baseClass = symbols[0];
}

DSymbol* imp = SymbolAllocator.instance.make!DSymbol(IMPORT_SYMBOL_NAME,
DSymbol* imp = GCAllocator.instance.make!DSymbol(IMPORT_SYMBOL_NAME,
CompletionKind.importSymbol, baseClass);
symbol.addChild(imp, true);
symbolScope.addSymbol(imp, false);
if (baseClass.kind == CompletionKind.className)
{
auto s = SymbolAllocator.instance.make!DSymbol(SUPER_SYMBOL_NAME,
auto s = GCAllocator.instance.make!DSymbol(SUPER_SYMBOL_NAME,
CompletionKind.variableName, baseClass);
symbolScope.addSymbol(s, true);
}
Expand All @@ -359,7 +357,7 @@ void resolveAliasThis(DSymbol* symbol,
auto parts = symbol.getPartsByName(aliasThis.breadcrumbs.front);
if (parts.length == 0 || parts[0].type is null)
continue;
DSymbol* s = SymbolAllocator.instance.make!DSymbol(IMPORT_SYMBOL_NAME,
DSymbol* s = GCAllocator.instance.make!DSymbol(IMPORT_SYMBOL_NAME,
CompletionKind.importSymbol, parts[0].type);
symbol.addChild(s, true);
auto symbolScope = moduleScope.getScopeByCursor(s.location);
Expand Down Expand Up @@ -395,7 +393,7 @@ void resolveMixinTemplates(DSymbol* symbol,
}
if (currentSymbol !is null)
{
auto i = SymbolAllocator.instance.make!DSymbol(IMPORT_SYMBOL_NAME,
auto i = GCAllocator.instance.make!DSymbol(IMPORT_SYMBOL_NAME,
CompletionKind.importSymbol, currentSymbol);
i.ownType = false;
symbol.addChild(i, true);
Expand Down Expand Up @@ -447,7 +445,7 @@ void resolveTypeFromInitializer(DSymbol* symbol, TypeLookup* lookup,
else
if (crumb == ARRAY_LITERAL_SYMBOL_NAME)
{
auto arr = SymbolAllocator.instance.make!(DSymbol)(ARRAY_LITERAL_SYMBOL_NAME, CompletionKind.dummy, currentSymbol);
auto arr = GCAllocator.instance.make!(DSymbol)(ARRAY_LITERAL_SYMBOL_NAME, CompletionKind.dummy, currentSymbol);
arr.qualifier = SymbolQualifier.array;
currentSymbol = arr;
}
Expand Down
4 changes: 2 additions & 2 deletions dsymbol/src/dsymbol/deferred.d
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import dsymbol.import_;
import dsymbol.symbol;
import dsymbol.type_lookup;
import std.experimental.allocator : dispose;
import std.experimental.allocator.mallocator : Mallocator;
import std.experimental.allocator.gc_allocator : GCAllocator;
import dsymbol.semantic : TypeLookups, TypeLookupsAllocator;

alias ImportsAllocator = Mallocator;
alias ImportsAllocator = GCAllocator;
alias Imports = UnrolledList!(DSymbol*, ImportsAllocator);

/**
Expand Down
19 changes: 1 addition & 18 deletions dsymbol/src/dsymbol/modulecache.d
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ import std.file;
import std.experimental.lexer;
import std.path;

alias ASTAllocator = AllocatorList!(n => Region!Mallocator(1024 * 128), Mallocator);

/**
* Returns: true if a file exists at the given path.
*/
Expand All @@ -71,13 +69,6 @@ struct ModuleCache
/// No copying.
@disable this(this);

@disable this();

this(RCIAllocator symbolAllocator)
{
this.symbolAllocator = symbolAllocator;
}

~this()
{
clear();
Expand Down Expand Up @@ -147,9 +138,6 @@ struct ModuleCache
foreach (symbol; deferredSymbols[])
DeferredSymbolsAllocator.instance.dispose(symbol);

// TODO: This call to deallocateAll is a workaround for issues of
// CAllocatorImpl and GCAllocator not interacting well.
symbolAllocator.deallocateAll();
cache.clear();
deferredSymbols.clear();
importPaths.clear();
Expand Down Expand Up @@ -198,14 +186,11 @@ struct ModuleCache

CacheEntry* newEntry = CacheAllocator.instance.make!CacheEntry();

scope semanticAllocator = new ASTAllocator();
import dparse.rollback_allocator:RollbackAllocator;
RollbackAllocator parseAllocator;
Module m = parseModuleSimple(tokens[], cachedLocation, &parseAllocator);

assert (!symbolAllocator.isNull);
scope first = new FirstPass(m, cachedLocation, symbolAllocator,
semanticAllocator.allocatorObject, &this, newEntry);
scope first = new FirstPass(m, cachedLocation, &this, newEntry);
first.run();

secondPass(first.rootSymbol, first.moduleScope, this);
Expand Down Expand Up @@ -355,8 +340,6 @@ struct ModuleCache
return cache[];
}

RCIAllocator symbolAllocator;

alias DeferredSymbols = UnrolledList!(DeferredSymbol*, DeferredSymbolsAllocator);
DeferredSymbols deferredSymbols;

Expand Down
Loading

0 comments on commit 5c529f3

Please sign in to comment.