Skip to content

Commit 031629f

Browse files
committed
tools: add cmd/tools/measure/scanner_speed.v and cmd/tools/measure/parser_speed.v
1 parent 9a0ec7f commit 031629f

File tree

3 files changed

+115
-5
lines changed

3 files changed

+115
-5
lines changed

cmd/tools/measure/parser_speed.v

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import os
2+
import time
3+
import v.ast
4+
import v.pref
5+
import v.parser
6+
import v.errors
7+
import v.scanner
8+
9+
fn main() {
10+
files := os.args#[1..]
11+
if files.len > 0 && files[0].starts_with('@') {
12+
lst_path := files[0].all_after('@')
13+
listed_files := os.read_file(lst_path)?.split('\n')
14+
process_files(listed_files)?
15+
return
16+
}
17+
process_files(files)?
18+
}
19+
20+
fn process_files(files []string) ? {
21+
mut table := ast.new_table()
22+
mut pref := pref.new_preferences()
23+
pref.is_fmt = true
24+
pref.skip_warnings = true
25+
pref.output_mode = .silent
26+
mut sw := time.new_stopwatch()
27+
mut total_us := i64(0)
28+
mut total_bytes := i64(0)
29+
mut total_tokens := i64(0)
30+
for f in files {
31+
if f == '' {
32+
continue
33+
}
34+
if f.ends_with('_test.v') {
35+
continue
36+
}
37+
// do not measure the scanning, but only the parsing:
38+
mut p := new_parser(f, .skip_comments, table, pref)
39+
///
40+
sw.restart()
41+
_ := p.parse()
42+
f_us := sw.elapsed().microseconds()
43+
///
44+
total_us += f_us
45+
total_bytes += p.scanner.text.len
46+
total_tokens += p.scanner.all_tokens.len
47+
println('${f_us:10}us ${p.scanner.all_tokens.len:10} ${p.scanner.text.len:10} ${(f64(p.scanner.text.len) / p.scanner.all_tokens.len):7.3} ${p.errors.len:4} $f')
48+
}
49+
println('${total_us:10}us ${total_tokens:10} ${total_bytes:10} ${(f64(total_tokens) / total_bytes):7.3} | speed: ${(f64(total_bytes) / total_us):2.5f} MB/s')
50+
}
51+
52+
fn new_parser(path string, comments_mode scanner.CommentsMode, table &ast.Table, pref &pref.Preferences) &parser.Parser {
53+
mut p := &parser.Parser{
54+
scanner: scanner.new_scanner_file(path, comments_mode, pref) or { panic(err) }
55+
comments_mode: comments_mode
56+
table: table
57+
pref: pref
58+
scope: &ast.Scope{
59+
start_pos: 0
60+
parent: table.global_scope
61+
}
62+
errors: []errors.Error{}
63+
warnings: []errors.Warning{}
64+
}
65+
p.set_path(path)
66+
return p
67+
}

cmd/tools/measure/scanner_speed.v

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
import time
3+
import v.scanner
4+
import v.pref
5+
6+
fn main() {
7+
files := os.args#[1..]
8+
if files.len > 0 && files[0].starts_with('@') {
9+
lst_path := files[0].all_after('@')
10+
listed_files := os.read_file(lst_path)?.split('\n')
11+
process_files(listed_files)?
12+
return
13+
}
14+
process_files(files)?
15+
}
16+
17+
fn process_files(files []string) ? {
18+
mut pref := pref.new_preferences()
19+
pref.is_fmt = true
20+
pref.skip_warnings = true
21+
pref.output_mode = .silent
22+
mut sw := time.new_stopwatch()
23+
mut total_us := i64(0)
24+
mut total_bytes := i64(0)
25+
mut total_tokens := i64(0)
26+
for f in files {
27+
if f == '' {
28+
continue
29+
}
30+
if f.ends_with('_test.v') {
31+
continue
32+
}
33+
sw.restart()
34+
s := scanner.new_scanner_file(f, .skip_comments, pref)?
35+
f_us := sw.elapsed().microseconds()
36+
total_us += f_us
37+
total_bytes += s.text.len
38+
total_tokens += s.all_tokens.len
39+
println('${f_us:10}us ${s.all_tokens.len:10} ${s.text.len:10} ${(f64(s.text.len) / s.all_tokens.len):7.3f} $f')
40+
}
41+
println('${total_us:10}us ${total_tokens:10} ${total_bytes:10} ${(f64(total_tokens) / total_bytes):7.3f} | speed: ${(f64(total_bytes) / total_us):2.5f} MB/s')
42+
}

vlib/v/parser/parser.v

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ mut:
2323
file_name_dir string // "/home/user"
2424
unique_prefix string // a hash of p.file_name, used for making anon fn generation unique
2525
file_backend_mode ast.Language // .c for .c.v|.c.vv|.c.vsh files; .js for .js.v files, .amd64/.rv32/other arches for .amd64.v/.rv32.v/etc. files, .v otherwise.
26-
scanner &scanner.Scanner
2726
comments_mode scanner.CommentsMode = .skip_comments
2827
// see comment in parse_file
2928
tok token.Token
@@ -78,10 +77,6 @@ mut:
7877
returns bool
7978
is_stmt_ident bool // true while the beginning of a statement is an ident/selector
8079
expecting_type bool // `is Type`, expecting type
81-
errors []errors.Error
82-
warnings []errors.Warning
83-
notices []errors.Notice
84-
vet_errors []vet.Error
8580
cur_fn_name string
8681
label_names []string
8782
name_error bool // indicates if the token is not a name or the name is on another line
@@ -95,6 +90,12 @@ mut:
9590
if_cond_comments []ast.Comment
9691
script_mode bool
9792
script_mode_start_token token.Token
93+
pub mut:
94+
scanner &scanner.Scanner
95+
errors []errors.Error
96+
warnings []errors.Warning
97+
notices []errors.Notice
98+
vet_errors []vet.Error
9899
}
99100

100101
__global codegen_files = []&ast.File{}

0 commit comments

Comments
 (0)