parser: parallel parser, part 1

pull/4987/head
Alexander Medvednikov 2020-05-22 02:22:56 +02:00
parent 2f171bf5d9
commit 1e853072dc
3 changed files with 52 additions and 30 deletions

View File

@ -11,6 +11,9 @@ import v.pref
import v.util import v.util
import v.errors import v.errors
import os import os
import runtime
import sync
import time
pub struct Parser { pub struct Parser {
scanner &scanner.Scanner scanner &scanner.Scanner
@ -134,48 +137,59 @@ pub fn parse_file(path string, b_table &table.Table, comments_mode scanner.Comme
} }
} }
/*
struct Queue { struct Queue {
mut: mut:
idx int idx int
mu sync.Mutex mu &sync.Mutex
mu2 &sync.Mutex
paths []string paths []string
table &table.Table table &table.Table
parsed_ast_files []ast.File parsed_ast_files []ast.File
pref &pref.Preferences
global_scope &ast.Scope
} }
fn (q mut Queue) run() { fn (mut q Queue) run() {
q.mu.lock() for {
idx := q.idx q.mu.lock()
if idx >= q.paths.len { idx := q.idx
if idx >= q.paths.len {
q.mu.unlock()
return
}
q.idx++
q.mu.unlock() q.mu.unlock()
return println('run(idx=$idx)')
path := q.paths[idx]
file := parse_file(path, q.table, .skip_comments, q.pref, q.global_scope)
q.mu2.lock()
q.parsed_ast_files << file
q.mu2.unlock()
println('run done(idx=$idx)')
} }
q.idx++
q.mu.unlock()
path := q.paths[idx]
file := parse_file(path, q.table, .skip_comments)
q.mu.lock()
q.parsed_ast_files << file
q.mu.unlock()
} }
*/
pub fn parse_files(paths []string, table &table.Table, pref &pref.Preferences, global_scope &ast.Scope) []ast.File { pub fn parse_files(paths []string, table &table.Table, pref &pref.Preferences, global_scope &ast.Scope) []ast.File {
/* // println('nr_cpus= $nr_cpus')
println('\n\n\nparse_files()') if pref.is_parallel && paths[0].contains('/array.v') {
println(paths) println('\n\n\nparse_files() nr_files=$paths.len')
nr_cpus := runtime.nr_cpus() println(paths)
println('nr_cpus= $nr_cpus') nr_cpus := runtime.nr_cpus()
mut q := &Queue{ mut q := &Queue{
paths: paths paths: paths
table: table table: table
pref: pref
global_scope: global_scope
mu: sync.new_mutex()
mu2: sync.new_mutex()
}
for _ in 0 .. nr_cpus - 1 {
go q.run()
}
time.sleep_ms(1000)
println('all done')
return q.parsed_ast_files
} }
for i in 0 .. nr_cpus {
go q.run()
}
time.sleep_ms(100)
return q.parsed_ast_files
*/
// /////////////// // ///////////////
mut files := []ast.File{} mut files := []ast.File{}
for path in paths { for path in paths {

View File

@ -24,7 +24,7 @@ pub enum ColorOutput {
auto auto
always always
never never
} }
pub enum Backend { pub enum Backend {
c // The (default) C backend c // The (default) C backend
@ -108,6 +108,7 @@ pub mut:
skip_running bool // when true, do no try to run the produced file (set by b.cc(), when -o x.c or -o x.js) skip_running bool // when true, do no try to run the produced file (set by b.cc(), when -o x.c or -o x.js)
skip_warnings bool // like C's "-w" skip_warnings bool // like C's "-w"
use_color ColorOutput // whether the warnings/errors should use ANSI color escapes. use_color ColorOutput // whether the warnings/errors should use ANSI color escapes.
is_parallel bool
} }
pub fn parse_args(args []string) (&Preferences, string) { pub fn parse_args(args []string) (&Preferences, string) {
@ -188,6 +189,9 @@ pub fn parse_args(args []string) (&Preferences, string) {
'-keepc' { '-keepc' {
res.keep_c = true res.keep_c = true
} }
'-parallel' {
res.is_parallel = true
}
'-x64' { '-x64' {
res.backend = .x64 res.backend = .x64
} }

View File

@ -6,6 +6,8 @@ fn simple<T>(p T) T {
} }
fn plus<T>(a, b T) T { fn plus<T>(a, b T) T {
// x := a
// y := b
// ww := ww // ww := ww
// q := xx + 1 // q := xx + 1
return a + b return a + b
@ -17,6 +19,8 @@ fn test_generic_fn() {
assert simple<string>('g') == 'g' assert simple<string>('g') == 'g'
assert simple<string>('g') + 'h' == 'gh' assert simple<string>('g') + 'h' == 'gh'
a := plus<int>(2, 3) a := plus<int>(2, 3)
assert a == 5
assert plus<int>(10, 1) == 11
// plus<string>('a', 'b') // plus<string>('a', 'b')
println(a) println(a)
} }