@@ -55,11 +55,7 @@ impl Default for CacheLeaf {
55
55
}
56
56
}
57
57
58
- use std:: {
59
- fs:: File ,
60
- io:: { BufRead , BufReader } ,
61
- path:: PathBuf ,
62
- } ;
58
+ use std:: { fs:: File , io:: Read , path:: PathBuf } ;
63
59
64
60
impl CacheLeaf {
65
61
/// Construct a [CacheLeaf] by reading a specified input file
@@ -71,7 +67,7 @@ impl CacheLeaf {
71
67
/// ```
72
68
pub fn read_file ( f : PathBuf ) -> Result < Self , ErrorKind > {
73
69
let mut me: Self = Default :: default ( ) ;
74
- let my_file = File :: open ( & f) ?;
70
+ let mut my_file = File :: open ( & f) ?;
75
71
76
72
// This is a clusterfuck really, the internal .modified takes a lot of
77
73
// mangling to get the internal unix-time value out of the metadata,
@@ -87,18 +83,17 @@ impl CacheLeaf {
87
83
0 ,
88
84
) ;
89
85
90
- // Note the default of 8k for BufReader is excessive for us, as it
91
- // accounts for 8/9ths of the overall heap size, which is
92
- // silly when you consider the file we're reading is typically
93
- // under 200 *bytes*, and all lines are under *21* bytes each,
94
- // and the whole point of using BufReader is to get the lines()
95
- // abstraction.
86
+ // We would use a BufReader here, but that cocks up amazingly when
87
+ // some idiot passes a directory as the PathBuf, and BufReader
88
+ // repeatedly invokes File::read() which repeatedly returns an
89
+ // Err(), and as an Err() is a Some(Err()) not a None(),
90
+ // doesn't end the iteration, and so on the next iteration ... it
91
+ // calls read() again, gets the same result, and subsequently
92
+ // iterates forever doing nothing.
96
93
//
97
- // With an 8k buffer we're twice as bad as the native ccache
98
- // implementation for heap usage, with a 100byte buffer, we're
99
- // 1/5th of the native ccache's heap use :)
100
- let buf = BufReader :: with_capacity ( 100 , my_file) ;
101
-
94
+ // We have a <1k file, who cares!?
95
+ let mut buf = String :: new ( ) ;
96
+ my_file. read_to_string ( & mut buf) ?;
102
97
// We collect all lines verbatim, and then use the FIELD_DATA_ORDER
103
98
// array to pick values out of it. That way if there are lines in the
104
99
// input source that we haven't coded behaviour for yet, it won't
@@ -107,25 +102,18 @@ impl CacheLeaf {
107
102
// The input source having fewer items than FIELD_DATA_ORDER is gated
108
103
// by the field_addr <= last_line control, so too-few lines will
109
104
// result in just a bunch of 0 entries in the dataset.
110
- let lines: Vec < std :: io :: Result < String > > = buf. lines ( ) . collect ( ) ;
105
+ let lines: Vec < & str > = buf. lines ( ) . collect ( ) ;
111
106
let last_line = lines. len ( ) - 1 ;
112
107
113
108
for field in FIELD_DATA_ORDER {
114
109
let field_addr: usize = field. as_usize ( ) ;
115
110
if field_addr <= last_line {
116
- if let Ok ( line) = & lines[ field_addr] {
117
- if let Ok ( v) = line. parse :: < u64 > ( ) {
118
- me. fields . set_field ( * field, v) ;
119
- } else {
120
- unimplemented ! (
121
- "Line {} in {:?} did not parse as u64" ,
122
- field_addr,
123
- f
124
- ) ;
125
- }
111
+ let line = & lines[ field_addr] ;
112
+ if let Ok ( v) = line. parse :: < u64 > ( ) {
113
+ me. fields . set_field ( * field, v) ;
126
114
} else {
127
115
unimplemented ! (
128
- "Line {} in {:?} did not read correctly " ,
116
+ "Line {} in {:?} did not parse as u64 " ,
129
117
field_addr,
130
118
f
131
119
) ;
0 commit comments