v.util: add a small cache for util.read_file, so reading individual source files is done just once

pull/11187/head
Delyan Angelov 2021-08-14 21:59:28 +03:00
parent 8a8a0932f7
commit 1743ad05c0
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 34 additions and 2 deletions

View File

@ -83,9 +83,12 @@ fn (mut b Builder) myfree() {
// for file in b.parsed_files {
// }
unsafe { b.parsed_files.free() }
unsafe { util.cached_read_source_file('') or {} }
}
fn (b &Builder) exit_on_invalid_syntax() {
// clear the source file cache, since it will not be needed anymore
unsafe { util.cached_read_source_file('') or {} }
// V should exit with an exit code of 1, when there are errors,
// even when -silent is passed in combination to -check-syntax:
if b.pref.only_check_syntax {

View File

@ -267,9 +267,38 @@ pub fn path_of_executable(path string) string {
return path
}
[heap]
struct SourceCache {
mut:
sources map[string]string
}
[unsafe]
pub fn cached_read_source_file(path string) ?string {
mut static cache := &SourceCache(0)
if isnil(cache) {
cache = &SourceCache{}
}
if path.len == 0 {
unsafe { cache.sources.free() }
unsafe { free(cache) }
cache = &SourceCache(0)
return error('memory source file cache cleared')
}
// eprintln('>> cached_read_source_file path: $path')
if res := cache.sources[path] {
// eprintln('>> cached')
return res
}
// eprintln('>> not cached | cache.sources.len: $cache.sources.len')
raw_text := os.read_file(path) or { return error('failed to open $path') }
res := skip_bom(raw_text)
cache.sources[path] = res
return res
}
pub fn read_file(file_path string) ?string {
raw_text := os.read_file(file_path) or { return error('failed to open $file_path') }
return skip_bom(raw_text)
return unsafe { cached_read_source_file(file_path) }
}
pub fn skip_bom(file_content string) string {