Skip to content

Commit f724c56

Browse files
committed
Revert "token,scanner,parser,checker,cgen: add file_idx to token.Pos (#25429)" (performance degraded, without an immediate clear benefit)
This reverts commit d948bf1.
1 parent d948bf1 commit f724c56

11 files changed

Lines changed: 71 additions & 98 deletions

File tree

‎cmd/tools/measure/fmt_speed.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ fn process_files(files []string) ! {
8989

9090
fn new_parser(path string, comments_mode scanner.CommentsMode, table &ast.Table, pref_ &pref.Preferences) &parser.Parser {
9191
mut p := &parser.Parser{
92-
scanner: scanner.new_scanner_file(path, -1, comments_mode, pref_) or { panic(err) }
92+
scanner: scanner.new_scanner_file(path, comments_mode, pref_) or { panic(err) }
9393
table: table
9494
pref: pref_
9595
scope: &ast.Scope{

‎cmd/tools/measure/parser_speed.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn process_files(files []string) ! {
8787

8888
fn new_parser(path string, comments_mode scanner.CommentsMode, table &ast.Table, pref_ &pref.Preferences) &parser.Parser {
8989
mut p := &parser.Parser{
90-
scanner: scanner.new_scanner_file(path, -1, comments_mode, pref_) or { panic(err) }
90+
scanner: scanner.new_scanner_file(path, comments_mode, pref_) or { panic(err) }
9191
table: table
9292
pref: pref_
9393
scope: &ast.Scope{

‎cmd/tools/measure/scanner_speed.v‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn process_files(files []string) ! {
5555
}
5656
total_files++
5757
sw.restart()
58-
s := scanner.new_scanner_file(f, -1, comments_mode, pref_)!
58+
s := scanner.new_scanner_file(f, comments_mode, pref_)!
5959
f_us := sw.elapsed().microseconds()
6060
total_us += f_us
6161
total_bytes += s.text.len

‎vlib/v/ast/table.v‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ pub mut:
103103
new_int bool // use 64bit/32bit platform dependent `int`
104104
new_int_fmt_fix bool // vfmt will fix `int` to `i32`
105105
export_names map[string]string // @[export] names
106-
filelist []string // all files list
107106
}
108107

109108
pub struct ComptTimeCondResult {

‎vlib/v/checker/errors.v‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ fn (mut c Checker) add_error_detail(s string) {
1414
}
1515

1616
fn (mut c Checker) add_error_detail_with_pos(msg string, pos token.Pos) {
17-
file_path := if pos.file_idx < 0 { c.file.path } else { c.table.filelist[pos.file_idx] }
18-
c.add_error_detail(util.formatted_error('details:', msg, file_path, pos))
17+
c.add_error_detail(util.formatted_error('details:', msg, c.file.path, pos))
1918
}
2019

2120
fn (mut c Checker) add_instruction_for_option_type() {
@@ -91,14 +90,13 @@ fn (mut c Checker) note(message string, pos token.Pos) {
9190
c.error_details = []
9291
}
9392
// deduplicate notices for the same line
94-
file_path := if pos.file_idx < 0 { c.file.path } else { c.table.filelist[pos.file_idx] }
95-
kpos := '${file_path}:${pos.line_nr}:${message}'
93+
kpos := '${c.file.path}:${pos.line_nr}:${message}'
9694
if kpos !in c.notice_lines {
9795
c.notice_lines[kpos] = true
9896
note := errors.Notice{
9997
reporter: errors.Reporter.checker
10098
pos: pos
101-
file_path: file_path
99+
file_path: c.file.path
102100
message: message
103101
details: details
104102
}
@@ -124,21 +122,20 @@ fn (mut c Checker) warn_or_error(message string, pos token.Pos, warn bool) {
124122
details = c.error_details.join('\n')
125123
c.error_details = []
126124
}
127-
file_path := if pos.file_idx < 0 { c.file.path } else { c.table.filelist[pos.file_idx] }
128125
if warn && !c.pref.skip_warnings {
129126
c.nr_warnings++
130127
if c.pref.message_limit >= 0 && c.nr_warnings >= c.pref.message_limit {
131128
c.should_abort = true
132129
return
133130
}
134131
// deduplicate warnings for the same line
135-
kpos := '${file_path}:${pos.line_nr}:${message}'
132+
kpos := '${c.file.path}:${pos.line_nr}:${message}'
136133
if kpos !in c.warning_lines {
137134
c.warning_lines[kpos] = true
138135
wrn := errors.Warning{
139136
reporter: errors.Reporter.checker
140137
pos: pos
141-
file_path: file_path
138+
file_path: c.file.path
142139
message: message
143140
details: details
144141
}
@@ -151,7 +148,7 @@ fn (mut c Checker) warn_or_error(message string, pos token.Pos, warn bool) {
151148
if c.pref.fatal_errors {
152149
util.show_compiler_message('error:', errors.CompilerMessage{
153150
pos: pos
154-
file_path: file_path
151+
file_path: c.file.path
155152
message: message
156153
details: details
157154
})
@@ -163,13 +160,13 @@ fn (mut c Checker) warn_or_error(message string, pos token.Pos, warn bool) {
163160
return
164161
}
165162
// deduplicate errors for the same line
166-
kpos := '${file_path}:${pos.line_nr}:${message}'
163+
kpos := '${c.file.path}:${pos.line_nr}:${message}'
167164
if kpos !in c.error_lines {
168165
c.error_lines[kpos] = true
169166
err := errors.Error{
170167
reporter: errors.Reporter.checker
171168
pos: pos
172-
file_path: file_path
169+
file_path: c.file.path
173170
message: message
174171
details: details
175172
}

‎vlib/v/gen/c/cgen.v‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6590,8 +6590,7 @@ fn verror(s string) {
65906590

65916591
@[noreturn]
65926592
fn (g &Gen) error(s string, pos token.Pos) {
6593-
file_path := if pos.file_idx < 0 { g.file.path } else { g.table.filelist[pos.file_idx] }
6594-
util.show_compiler_message('cgen error:', pos: pos, file_path: file_path, message: s)
6593+
util.show_compiler_message('cgen error:', pos: pos, file_path: g.file.path, message: s)
65956594
exit(1)
65966595
}
65976596

‎vlib/v/parser/messages.v‎

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,20 @@ fn (mut p Parser) note(s string) {
6868
fn (mut p Parser) error_with_pos(s string, pos token.Pos) ast.NodeError {
6969
// print_backtrace()
7070
mut kind := 'error:'
71-
file_path := if pos.file_idx < 0 { p.file_path } else { p.table.filelist[pos.file_idx] }
7271
if p.pref.fatal_errors {
73-
util.show_compiler_message(kind, pos: pos, file_path: file_path, message: s)
72+
util.show_compiler_message(kind, pos: pos, file_path: p.file_path, message: s)
7473
exit(1)
7574
}
7675
if p.pref.output_mode == .stdout && !p.pref.check_only && !p.is_vls {
7776
if p.pref.is_verbose {
7877
print_backtrace()
7978
kind = 'parser error:'
8079
}
81-
util.show_compiler_message(kind, pos: pos, file_path: file_path, message: s)
80+
util.show_compiler_message(kind, pos: pos, file_path: p.file_path, message: s)
8281
exit(1)
8382
} else {
8483
p.errors << errors.Error{
85-
file_path: file_path
84+
file_path: p.file_path
8685
pos: pos
8786
reporter: .parser
8887
message: s
@@ -146,16 +145,15 @@ fn (mut p Parser) warn_with_pos(s string, pos token.Pos) {
146145
if p.pref.skip_warnings {
147146
return
148147
}
149-
file_path := if pos.file_idx < 0 { p.file_path } else { p.table.filelist[pos.file_idx] }
150148
if p.pref.output_mode == .stdout && !p.pref.check_only {
151-
util.show_compiler_message('warning:', pos: pos, file_path: file_path, message: s)
149+
util.show_compiler_message('warning:', pos: pos, file_path: p.file_path, message: s)
152150
} else {
153151
if p.pref.message_limit >= 0 && p.warnings.len >= p.pref.message_limit {
154152
p.should_abort = true
155153
return
156154
}
157155
p.warnings << errors.Warning{
158-
file_path: file_path
156+
file_path: p.file_path
159157
pos: pos
160158
reporter: .parser
161159
message: s
@@ -177,12 +175,11 @@ fn (mut p Parser) note_with_pos(s string, pos token.Pos) {
177175
p.error_with_pos(s, pos)
178176
return
179177
}
180-
file_path := if pos.file_idx < 0 { p.file_path } else { p.table.filelist[pos.file_idx] }
181178
if p.pref.output_mode == .stdout && !p.pref.check_only {
182-
util.show_compiler_message('notice:', pos: pos, file_path: file_path, message: s)
179+
util.show_compiler_message('notice:', pos: pos, file_path: p.file_path, message: s)
183180
} else {
184181
p.notices << errors.Notice{
185-
file_path: file_path
182+
file_path: p.file_path
186183
pos: pos
187184
reporter: .parser
188185
message: s

‎vlib/v/parser/parser.v‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ pub:
1919
mut:
2020
file_base string // "hello.v"
2121
file_path string // "/home/user/hello.v"
22-
file_idx int // file idx in the global table `filelist`
2322
file_display_path string // just "hello.v", when your current folder for the compilation is "/home/user/", otherwise the full path "/home/user/hello.v"
2423
unique_prefix string // a hash of p.file_path, used for making anon fn generation unique
2524
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.
@@ -256,13 +255,8 @@ pub fn parse_file(path string, mut table ast.Table, comments_mode scanner.Commen
256255
$if trace_parse_file ? {
257256
eprintln('> ${@MOD}.${@FN} comments_mode: ${comments_mode:-20} | path: ${path}')
258257
}
259-
mut file_idx := table.filelist.index(path)
260-
if file_idx == -1 {
261-
file_idx = table.filelist.len
262-
table.filelist << path
263-
}
264258
mut p := Parser{
265-
scanner: scanner.new_scanner_file(path, file_idx, comments_mode, pref_) or { panic(err) }
259+
scanner: scanner.new_scanner_file(path, comments_mode, pref_) or { panic(err) }
266260
table: table
267261
pref: pref_
268262
// Only set vls mode if it's the file the user requested via `v -vls-mode file.v`
@@ -274,7 +268,6 @@ pub fn parse_file(path string, mut table ast.Table, comments_mode scanner.Commen
274268
}
275269
errors: []errors.Error{}
276270
warnings: []errors.Warning{}
277-
file_idx: file_idx
278271
}
279272
p.set_path(path)
280273
res := p.parse()

‎vlib/v/scanner/scanner.v‎

Lines changed: 41 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ pub struct Scanner {
2424
pub mut:
2525
file_path string // '/path/to/file.v'
2626
file_base string // 'file.v'
27-
file_idx int = -1 // file idx in the global table `filelist`
2827
text string // the whole text of the file
2928
pos int = -1 // current position in the file, first character is s.text[0]
3029
line_nr int // current line number
@@ -108,7 +107,7 @@ pub enum CommentsMode {
108107
}
109108

110109
// new scanner from file.
111-
pub fn new_scanner_file(file_path string, file_idx int, comments_mode CommentsMode, pref_ &pref.Preferences) !&Scanner {
110+
pub fn new_scanner_file(file_path string, comments_mode CommentsMode, pref_ &pref.Preferences) !&Scanner {
112111
if !os.is_file(file_path) {
113112
return error('${file_path} is not a .v file')
114113
}
@@ -124,7 +123,6 @@ pub fn new_scanner_file(file_path string, file_idx int, comments_mode CommentsMo
124123
comments_mode: comments_mode
125124
file_path: file_path
126125
file_base: os.base(file_path)
127-
file_idx: file_idx
128126
}
129127
s.scan_all_tokens_in_buffer()
130128
return s
@@ -188,28 +186,26 @@ fn (mut s Scanner) new_token(tok_kind token.Kind, lit string, len int) token.Tok
188186
max_column = 1
189187
}
190188
return token.Token{
191-
kind: tok_kind
192-
lit: lit
193-
line_nr: s.line_nr + line_offset
194-
col: max_column
195-
pos: s.pos - len + 1
196-
len: len
197-
tidx: cidx
198-
file_idx: s.file_idx
189+
kind: tok_kind
190+
lit: lit
191+
line_nr: s.line_nr + line_offset
192+
col: max_column
193+
pos: s.pos - len + 1
194+
len: len
195+
tidx: cidx
199196
}
200197
}
201198

202199
@[inline]
203200
fn (s &Scanner) new_eof_token() token.Token {
204201
return token.Token{
205-
kind: .eof
206-
lit: ''
207-
line_nr: s.line_nr + 1
208-
col: s.current_column()
209-
pos: s.pos
210-
len: 1
211-
tidx: s.tidx
212-
file_idx: s.file_idx
202+
kind: .eof
203+
lit: ''
204+
line_nr: s.line_nr + 1
205+
col: s.current_column()
206+
pos: s.pos
207+
len: 1
208+
tidx: s.tidx
213209
}
214210
}
215211

@@ -222,14 +218,13 @@ fn (mut s Scanner) new_multiline_token(tok_kind token.Kind, lit string, len int,
222218
max_column = 1
223219
}
224220
return token.Token{
225-
kind: tok_kind
226-
lit: lit
227-
line_nr: start_line + 1
228-
col: max_column
229-
pos: s.pos - len + 1
230-
len: len
231-
tidx: cidx
232-
file_idx: s.file_idx
221+
kind: tok_kind
222+
lit: lit
223+
line_nr: start_line + 1
224+
col: max_column
225+
pos: s.pos - len + 1
226+
len: len
227+
tidx: cidx
233228
}
234229
}
235230

@@ -950,11 +945,10 @@ pub fn (mut s Scanner) text_scan() token.Token {
950945
comment := s.text[start - 1..s.pos].trim_space()
951946
if s.line_nr != 1 {
952947
comment_pos := token.Pos{
953-
line_nr: s.line_nr - 1
954-
len: comment.len
955-
pos: start
956-
col: s.current_column() - comment.len
957-
file_idx: s.file_idx
948+
line_nr: s.line_nr - 1
949+
len: comment.len
950+
pos: start
951+
col: s.current_column() - comment.len
958952
}
959953
s.error_with_pos('a shebang is only valid at the top of the file',
960954
comment_pos)
@@ -1128,11 +1122,10 @@ pub fn (mut s Scanner) text_scan() token.Token {
11281122
mut comment := s.text[start..(s.pos - 1)]
11291123
if !comment.contains('\n') {
11301124
comment_pos := token.Pos{
1131-
line_nr: start_line
1132-
len: comment.len + 4
1133-
pos: start
1134-
col: s.current_column() - comment.len - 4
1135-
file_idx: s.file_idx
1125+
line_nr: start_line
1126+
len: comment.len + 4
1127+
pos: start
1128+
col: s.current_column() - comment.len - 4
11361129
}
11371130
s.error_with_pos('inline comment is deprecated, please use line comment',
11381131
comment_pos)
@@ -1200,10 +1193,9 @@ pub fn (mut s Scanner) ident_string() string {
12001193
s.is_nested_string = false
12011194
}
12021195
lspos := token.Pos{
1203-
line_nr: s.line_nr
1204-
pos: s.pos
1205-
col: s.pos - s.last_nl_pos - 1
1206-
file_idx: s.file_idx
1196+
line_nr: s.line_nr
1197+
pos: s.pos
1198+
col: s.pos - s.last_nl_pos - 1
12071199
}
12081200
q := s.text[s.pos]
12091201
is_quote := q in [single_quote, double_quote]
@@ -1519,10 +1511,9 @@ fn trim_slash_line_break(s string) string {
15191511
/// escaped utf8 runes in octal like `\342\230\205` => (★)
15201512
pub fn (mut s Scanner) ident_char() string {
15211513
lspos := token.Pos{
1522-
line_nr: s.line_nr
1523-
pos: s.pos
1524-
col: s.pos - s.last_nl_pos - 1
1525-
file_idx: s.file_idx
1514+
line_nr: s.line_nr
1515+
pos: s.pos
1516+
col: s.pos - s.last_nl_pos - 1
15261517
}
15271518

15281519
start := s.pos // the string position of the first backtick char
@@ -1678,10 +1669,9 @@ fn (mut s Scanner) inc_line_number() {
16781669

16791670
pub fn (mut s Scanner) current_pos() token.Pos {
16801671
return token.Pos{
1681-
line_nr: s.line_nr
1682-
pos: s.pos
1683-
col: s.current_column() - 1
1684-
file_idx: s.file_idx
1672+
line_nr: s.line_nr
1673+
pos: s.pos
1674+
col: s.current_column() - 1
16851675
}
16861676
}
16871677

@@ -1691,9 +1681,8 @@ pub fn (mut s Scanner) note(msg string) {
16911681
return
16921682
}
16931683
pos := token.Pos{
1694-
line_nr: s.line_nr
1695-
pos: s.pos
1696-
file_idx: s.file_idx
1684+
line_nr: s.line_nr
1685+
pos: s.pos
16971686
}
16981687
if s.pref.output_mode == .stdout && !s.pref.check_only {
16991688
util.show_compiler_message('notice:', pos: pos, file_path: s.file_path, message: msg)

0 commit comments

Comments
 (0)