compact compiler ids and spans to reduce memory usage

This commit is contained in:
2026-06-12 16:55:16 +02:00
parent 99ba907f59
commit 4112b79c6b
16 changed files with 972 additions and 573 deletions
+27 -16
View File
@@ -39,20 +39,20 @@ is_identifier :: proc(value: string) -> bool {
return true
}
find_package :: proc(state: ^State, path: string) -> int {
find_package :: proc(state: ^State, path: string) -> ast.Package_Id {
for pkg, id in state.module.packages {
if pkg.path == path {
return id
return ast.package_id(id)
}
}
return -1
return ast.INVALID_PACKAGE
}
add_placeholder :: proc(state: ^State, path: string) -> int {
if existing := find_package(state, path); existing >= 0 {
add_placeholder :: proc(state: ^State, path: string) -> ast.Package_Id {
if existing := find_package(state, path); existing != ast.INVALID_PACKAGE {
return existing
}
id := len(state.module.packages)
id := ast.package_id(len(state.module.packages))
append(&state.module.packages, ast.Package{
path=strings.clone(path, state.allocator),
name=symbol.intern(state.symbols, filepath.base(path)),
@@ -103,7 +103,7 @@ resolve_import_path :: proc(state: ^State, importing_path, import_path: string)
return joined, false
}
load_package :: proc(state: ^State, path: string, import_span: source.Span, is_root := false) -> int {
load_package :: proc(state: ^State, path: string, import_span: source.Span, is_root := false) -> ast.Package_Id {
canonical, ok := filepath.abs(path, state.allocator)
if !ok || !os.is_dir(path) {
if is_root {
@@ -111,7 +111,7 @@ load_package :: proc(state: ^State, path: string, import_span: source.Span, is_r
if len(canonical) > 0 {
delete(canonical, state.allocator)
}
return -1
return ast.INVALID_PACKAGE
}
placeholder := path
if len(canonical) > 0 {
@@ -124,12 +124,12 @@ load_package :: proc(state: ^State, path: string, import_span: source.Span, is_r
}
return id
}
if existing := find_package(state, canonical); existing >= 0 {
if existing := find_package(state, canonical); existing != ast.INVALID_PACKAGE {
delete(canonical, state.allocator)
return existing
}
pkg_id := len(state.module.packages)
pkg_id := ast.package_id(len(state.module.packages))
append(&state.module.packages, ast.Package{
path=canonical,
name=symbol.intern(state.symbols, filepath.base(canonical)),
@@ -152,13 +152,24 @@ load_package :: proc(state: ^State, path: string, import_span: source.Span, is_r
}
for file_info in files {
if file_info.size < 0 || !source.fits_source_length(u64(file_info.size)) {
source.addf(state.diagnostics, import_span, "source file '%s' exceeds the 4 GiB source limit", file_info.fullpath)
state.root_failed = true
continue
}
bytes, read_ok := os.read_entire_file(file_info.fullpath, state.sources.allocator)
if !read_ok {
state.root_failed = true
continue
}
if !source.fits_source_length(u64(len(bytes))) {
source.addf(state.diagnostics, import_span, "source file '%s' exceeds the 4 GiB source limit", file_info.fullpath)
delete(bytes, state.sources.allocator)
state.root_failed = true
continue
}
source_id := source.add_source_owned(state.sources, file_info.fullpath, bytes)
file_id := len(state.module.files)
file_id := ast.file_id(len(state.module.files))
append(&state.module.files, ast.File{source=source_id, pkg=pkg_id})
stream := lexer.lex(&state.sources.items[source_id], state.diagnostics, state.symbols, state.token_allocator)
parser.parse_into(&stream, &state.sources.items[source_id], state.diagnostics, state.module, pkg_id, file_id)
@@ -169,7 +180,7 @@ load_package :: proc(state: ^State, path: string, import_span: source.Span, is_r
import_count := len(state.module.imports)
for import_id in 0..<import_count {
import_item := state.module.imports[import_id]
if import_item.pkg != pkg_id || import_item.target >= 0 {
if import_item.pkg != pkg_id || import_item.target != ast.INVALID_PACKAGE {
continue
}
if filepath.is_abs(import_item.path) {
@@ -181,7 +192,7 @@ load_package :: proc(state: ^State, path: string, import_span: source.Span, is_r
target_path, target_ok := resolve_import_path(state, canonical, import_item.path)
target := load_package(state, target_path, import_item.span)
state.module.imports[import_id].target = target
if !target_ok || target < 0 || !state.module.packages[target].available {
if !target_ok || target == ast.INVALID_PACKAGE || !state.module.packages[target].available {
state.module.imports[import_id].valid = false
}
delete(target_path, state.allocator)
@@ -189,7 +200,7 @@ load_package :: proc(state: ^State, path: string, import_span: source.Span, is_r
return pkg_id
}
declaration_conflicts :: proc(module: ^ast.Module, pkg: int, name: symbol.Id) -> bool {
declaration_conflicts :: proc(module: ^ast.Module, pkg: ast.Package_Id, name: symbol.Id) -> bool {
for function in module.functions {
if function.pkg == pkg && function.name == name {
return true
@@ -205,7 +216,7 @@ declaration_conflicts :: proc(module: ^ast.Module, pkg: int, name: symbol.Id) ->
validate_imports :: proc(state: ^State) {
for import_item, import_id in state.module.imports {
if !symbol.is_valid(import_item.alias) && import_item.target >= 0 {
if !symbol.is_valid(import_item.alias) && import_item.target != ast.INVALID_PACKAGE {
state.module.imports[import_id].alias = state.module.packages[import_item.target].name
}
alias := state.module.imports[import_id].alias
@@ -260,7 +271,7 @@ load :: proc(
allocator=allocator,
}
root := load_package(&state, root_path, source.Span{}, true)
if root != 0 && root >= 0 {
if root != ast.Package_Id(0) && root != ast.INVALID_PACKAGE {
state.root_failed = true
}
validate_imports(&state)