From 1e853072dce9c1f9cf121489ab24f07d6c371020 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 22 May 2020 02:22:56 +0200 Subject: [PATCH] parser: parallel parser, part 1 --- vlib/v/parser/parser.v | 72 ++++++++++++++++++++++--------------- vlib/v/pref/pref.v | 6 +++- vlib/v/tests/generic_test.v | 4 +++ 3 files changed, 52 insertions(+), 30 deletions(-) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index e749ccc4b3..81633cdd3c 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -11,6 +11,9 @@ import v.pref import v.util import v.errors import os +import runtime +import sync +import time pub struct Parser { scanner &scanner.Scanner @@ -134,48 +137,59 @@ pub fn parse_file(path string, b_table &table.Table, comments_mode scanner.Comme } } -/* struct Queue { mut: idx int - mu sync.Mutex + mu &sync.Mutex + mu2 &sync.Mutex paths []string table &table.Table parsed_ast_files []ast.File + pref &pref.Preferences + global_scope &ast.Scope } -fn (q mut Queue) run() { - q.mu.lock() - idx := q.idx - if idx >= q.paths.len { +fn (mut q Queue) run() { + for { + q.mu.lock() + idx := q.idx + if idx >= q.paths.len { + q.mu.unlock() + return + } + q.idx++ 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 { - /* - println('\n\n\nparse_files()') - println(paths) - nr_cpus := runtime.nr_cpus() - println('nr_cpus= $nr_cpus') - mut q := &Queue{ - paths: paths - table: table + // println('nr_cpus= $nr_cpus') + if pref.is_parallel && paths[0].contains('/array.v') { + println('\n\n\nparse_files() nr_files=$paths.len') + println(paths) + nr_cpus := runtime.nr_cpus() + mut q := &Queue{ + paths: paths + 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{} for path in paths { diff --git a/vlib/v/pref/pref.v b/vlib/v/pref/pref.v index 07bade1053..db427e9390 100644 --- a/vlib/v/pref/pref.v +++ b/vlib/v/pref/pref.v @@ -24,7 +24,7 @@ pub enum ColorOutput { auto always never -} +} pub enum 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_warnings bool // like C's "-w" use_color ColorOutput // whether the warnings/errors should use ANSI color escapes. + is_parallel bool } pub fn parse_args(args []string) (&Preferences, string) { @@ -188,6 +189,9 @@ pub fn parse_args(args []string) (&Preferences, string) { '-keepc' { res.keep_c = true } + '-parallel' { + res.is_parallel = true + } '-x64' { res.backend = .x64 } diff --git a/vlib/v/tests/generic_test.v b/vlib/v/tests/generic_test.v index b7e26cbd8d..290f76aac8 100644 --- a/vlib/v/tests/generic_test.v +++ b/vlib/v/tests/generic_test.v @@ -6,6 +6,8 @@ fn simple(p T) T { } fn plus(a, b T) T { + // x := a + // y := b // ww := ww // q := xx + 1 return a + b @@ -17,6 +19,8 @@ fn test_generic_fn() { assert simple('g') == 'g' assert simple('g') + 'h' == 'gh' a := plus(2, 3) + assert a == 5 + assert plus(10, 1) == 11 // plus('a', 'b') println(a) }