diff options
| author | Daniel Gavin <danielgavin5@hotmail.com> | 2022-07-31 15:25:07 +0200 |
|---|---|---|
| committer | Daniel Gavin <danielgavin5@hotmail.com> | 2022-07-31 15:25:07 +0200 |
| commit | 963bdd60b8f85eabd3775cec9ce2b92609524db5 (patch) | |
| tree | 5caba8b7b05ead720f86671a9127e76253653b15 /src | |
| parent | a9625ed006585b742895166ddc5a77f80c1e2960 (diff) | |
Memory optimizations
Diffstat (limited to 'src')
| -rw-r--r-- | src/odin/printer/document.odin | 25 | ||||
| -rw-r--r-- | src/odin/printer/printer.odin | 2 |
2 files changed, 13 insertions, 14 deletions
diff --git a/src/odin/printer/document.odin b/src/odin/printer/document.odin index 8a3f858..c2e59ea 100644 --- a/src/odin/printer/document.odin +++ b/src/odin/printer/document.odin @@ -215,17 +215,12 @@ group :: proc(grouped_document: ^Document, options := Document_Group_Options{}, cons :: proc(elems: ..^Document, allocator := context.allocator) -> ^Document { document := new(Document, allocator) - elements := make([dynamic]^Document, allocator) + elements := make([dynamic]^Document, 0, len(elems), allocator) + for elem in elems { - #partial switch e in elem { - case Document_Nil: - continue - case Document_Cons: - append(&elements, ..e.elements) - case: - append(&elements, elem) - } + append(&elements, elem) } + c := Document_Cons { elements = elements[:], } @@ -264,6 +259,8 @@ Tuple :: struct { document: ^Document, } +list_fits: [dynamic]Tuple + fits :: proc(width: int, list: ^[dynamic]Tuple) -> bool { assert(list != nil) @@ -357,6 +354,8 @@ format :: proc(width: int, list: ^[dynamic]Tuple, builder: ^strings.Builder, p: suffix_builder := strings.builder_make() + list_fits = make([dynamic]Tuple, 0, 100, p.allocator) + for len(list) != 0 { data: Tuple = pop(list) @@ -425,20 +424,20 @@ format :: proc(width: int, list: ^[dynamic]Tuple, builder: ^strings.Builder, p: break } - l := make([dynamic]Tuple, 0, len(list)) + clear(&list_fits) for element in list { - append(&l, element) + append(&list_fits, element) } - append(&l, Tuple {indentation = data.indentation, mode = .Fit, document = v.document, alignment = data.alignment}) + append(&list_fits, Tuple {indentation = data.indentation, mode = .Fit, document = v.document, alignment = data.alignment}) recalculate = false if data.mode == .Fit { append(list, Tuple {indentation = data.indentation, mode = .Fit, document = v.document, alignment = data.alignment}) } - else if fits(width-consumed, &l) && v.mode != .Break { + else if fits(width-consumed, &list_fits) && v.mode != .Break { append(list, Tuple {indentation = data.indentation, mode = .Flat, document = v.document, alignment = data.alignment}) } else { if v.mode == .Fit { diff --git a/src/odin/printer/printer.odin b/src/odin/printer/printer.odin index 183ed75..c37e0fa 100644 --- a/src/odin/printer/printer.odin +++ b/src/odin/printer/printer.odin @@ -174,7 +174,7 @@ print_expr :: proc(p: ^Printer, expr: ^ast.Expr) -> string { print_file :: proc(p: ^Printer, file: ^ast.File) -> string { p.comments = file.comments - p.string_builder = strings.builder_make(p.allocator) + p.string_builder = strings.builder_make(0, len(file.src)*2, p.allocator) p.src = file.src context.allocator = p.allocator |