aboutsummaryrefslogtreecommitdiff
path: root/src/server/documents.odin
diff options
context:
space:
mode:
authorDanielGavin <danielgavin5@hotmail.com>2020-12-05 00:39:50 +0100
committerDanielGavin <danielgavin5@hotmail.com>2020-12-05 00:39:50 +0100
commit498e8a3895cd5b1db756b7f61eb48d1fd4211460 (patch)
tree71f35915bc9c449f762c1b7e25014fc6f7685e39 /src/server/documents.odin
parentefd2930b74943a4dbe463810f0c7b3e9ede0ab84 (diff)
added allocator - no more parsing the file every request
Diffstat (limited to 'src/server/documents.odin')
-rw-r--r--src/server/documents.odin55
1 files changed, 30 insertions, 25 deletions
diff --git a/src/server/documents.odin b/src/server/documents.odin
index 7f14252..5a0261c 100644
--- a/src/server/documents.odin
+++ b/src/server/documents.odin
@@ -35,10 +35,12 @@ Document :: struct {
ast: ast.File,
imports: [] Package,
package_name: string,
+ allocator: ^common.Scratch_Allocator, //because does not support freeing I use arena allocators for each document
};
DocumentStorage :: struct {
documents: map [string] Document,
+ free_allocators: [dynamic] ^common.Scratch_Allocator,
};
document_storage: DocumentStorage;
@@ -47,6 +49,24 @@ document_storage_shutdown :: proc() {
delete(document_storage.documents);
}
+document_get_allocator :: proc() -> ^common.Scratch_Allocator {
+
+ if len(document_storage.free_allocators) > 0 {
+ return pop(&document_storage.free_allocators);
+ }
+
+ else {
+ allocator := new(common.Scratch_Allocator);
+ common.scratch_allocator_init(allocator, mem.megabytes(1));
+ return allocator;
+ }
+
+}
+
+document_free_allocator :: proc(allocator: ^common.Scratch_Allocator) {
+ append(&document_storage.free_allocators, allocator);
+}
+
document_get :: proc(uri_string: string) -> ^Document {
uri, parsed_ok := common.parse_uri(uri_string, context.temp_allocator);
@@ -84,6 +104,7 @@ document_open :: proc(uri_string: string, text: string, config: ^common.Config,
document.client_owned = true;
document.text = transmute([] u8)text;
document.used_text = len(document.text);
+ document.allocator = document_get_allocator();
if err := document_refresh(document, config, writer); err != .None {
return err;
@@ -98,6 +119,7 @@ document_open :: proc(uri_string: string, text: string, config: ^common.Config,
text = transmute([] u8)text,
client_owned = true,
used_text = len(text),
+ allocator = document_get_allocator(),
};
if err := document_refresh(&document, config, writer); err != .None {
@@ -221,9 +243,9 @@ document_close :: proc(uri_string: string) -> common.Error {
return .InvalidRequest;
}
- //free_imports(document);
-
- //common.free_ast_file(document.ast);
+ free_all(common.scratch_allocator(document.allocator));
+ document_free_allocator(document.allocator);
+ document.allocator = nil;
document.client_owned = false;
@@ -246,7 +268,6 @@ document_refresh :: proc(document: ^Document, config: ^common.Config, writer: ^W
return .ParseError;
}
- //right now we don't allow to writer errors out from files read from the file directory, core files, etc.
if writer != nil && len(errors) > 0 {
document.diagnosed_errors = true;
@@ -322,24 +343,8 @@ parser_warning_handler :: proc(pos: tokenizer.Pos, msg: string, args: ..any) {
}
-free_imports :: proc(document: ^Document) {
- if document.imports != nil {
-
- for imp in document.imports {
- delete(imp.name);
- }
-
- delete(document.imports);
- delete(document.package_name);
-
- document.imports = nil;
- }
-}
-
parse_document :: proc(document: ^Document, config: ^common.Config) -> ([] ParserError, bool) {
- context.allocator = context.temp_allocator;
-
p := parser.Parser {
err = parser_error_handler,
warn = parser_warning_handler,
@@ -347,7 +352,9 @@ parse_document :: proc(document: ^Document, config: ^common.Config) -> ([] Parse
current_errors = make([dynamic] ParserError, context.temp_allocator);
- //common.free_ast_file(document.ast);
+ free_all(common.scratch_allocator(document.allocator));
+
+ context.allocator = common.scratch_allocator(document.allocator);
document.ast = ast.File {
fullpath = document.uri.path,
@@ -356,10 +363,8 @@ parse_document :: proc(document: ^Document, config: ^common.Config) -> ([] Parse
parser.parse_file(&p, &document.ast);
- //free_imports(document);
-
- document.imports = make([]Package, len(document.ast.imports), context.temp_allocator);
- document.package_name = strings.to_lower(path.dir(document.uri.path, context.temp_allocator), context.temp_allocator);
+ document.imports = make([]Package, len(document.ast.imports));
+ document.package_name = strings.to_lower(path.dir(document.uri.path, context.temp_allocator));
for imp, index in document.ast.imports {