parser: parallel parser, part 1
							parent
							
								
									2f171bf5d9
								
							
						
					
					
						commit
						1e853072dc
					
				| 
						 | 
					@ -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,17 +137,20 @@ 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() {
 | 
				
			||||||
 | 
						for {
 | 
				
			||||||
		q.mu.lock()
 | 
							q.mu.lock()
 | 
				
			||||||
		idx := q.idx
 | 
							idx := q.idx
 | 
				
			||||||
		if idx >= q.paths.len {
 | 
							if idx >= q.paths.len {
 | 
				
			||||||
| 
						 | 
					@ -153,29 +159,37 @@ fn (q mut Queue) run() {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		q.idx++
 | 
							q.idx++
 | 
				
			||||||
		q.mu.unlock()
 | 
							q.mu.unlock()
 | 
				
			||||||
 | 
							println('run(idx=$idx)')
 | 
				
			||||||
		path := q.paths[idx]
 | 
							path := q.paths[idx]
 | 
				
			||||||
	file := parse_file(path, q.table, .skip_comments)
 | 
							file := parse_file(path, q.table, .skip_comments, q.pref, q.global_scope)
 | 
				
			||||||
	q.mu.lock()
 | 
							q.mu2.lock()
 | 
				
			||||||
		q.parsed_ast_files << file
 | 
							q.parsed_ast_files << file
 | 
				
			||||||
	q.mu.unlock()
 | 
							q.mu2.unlock()
 | 
				
			||||||
 | 
							println('run done(idx=$idx)')
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
*/
 | 
					
 | 
				
			||||||
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('\n\n\nparse_files() nr_files=$paths.len')
 | 
				
			||||||
		println(paths)
 | 
							println(paths)
 | 
				
			||||||
		nr_cpus := runtime.nr_cpus()
 | 
							nr_cpus := runtime.nr_cpus()
 | 
				
			||||||
	println('nr_cpus= $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 i in 0 .. nr_cpus {
 | 
							for _ in 0 .. nr_cpus - 1 {
 | 
				
			||||||
			go q.run()
 | 
								go q.run()
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	time.sleep_ms(100)
 | 
							time.sleep_ms(1000)
 | 
				
			||||||
 | 
							println('all done')
 | 
				
			||||||
		return q.parsed_ast_files
 | 
							return q.parsed_ast_files
 | 
				
			||||||
	*/
 | 
						}
 | 
				
			||||||
	// ///////////////
 | 
						// ///////////////
 | 
				
			||||||
	mut files := []ast.File{}
 | 
						mut files := []ast.File{}
 | 
				
			||||||
	for path in paths {
 | 
						for path in paths {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue