1
- use crate :: leb128:: { self , largest_max_leb128_len } ;
1
+ use crate :: leb128;
2
2
use crate :: serialize:: { Decodable , Decoder , Encodable , Encoder } ;
3
3
use std:: fs:: File ;
4
4
use std:: io:: { self , Write } ;
@@ -14,6 +14,9 @@ use std::ptr;
14
14
15
15
pub type FileEncodeResult = Result < usize , io:: Error > ;
16
16
17
+ /// The size of the buffer in `FileEncoder`.
18
+ const BUF_SIZE : usize = 8192 ;
19
+
17
20
/// `FileEncoder` encodes data to file via fixed-size buffer.
18
21
///
19
22
/// There used to be a `MemEncoder` type that encoded all the data into a
@@ -35,26 +38,12 @@ pub struct FileEncoder {
35
38
36
39
impl FileEncoder {
37
40
pub fn new < P : AsRef < Path > > ( path : P ) -> io:: Result < Self > {
38
- const DEFAULT_BUF_SIZE : usize = 8192 ;
39
- FileEncoder :: with_capacity ( path, DEFAULT_BUF_SIZE )
40
- }
41
-
42
- pub fn with_capacity < P : AsRef < Path > > ( path : P , capacity : usize ) -> io:: Result < Self > {
43
- // Require capacity at least as large as the largest LEB128 encoding
44
- // here, so that we don't have to check or handle this on every write.
45
- assert ! ( capacity >= largest_max_leb128_len( ) ) ;
46
-
47
- // Require capacity small enough such that some capacity checks can be
48
- // done using guaranteed non-overflowing add rather than sub, which
49
- // shaves an instruction off those code paths (on x86 at least).
50
- assert ! ( capacity <= usize :: MAX - largest_max_leb128_len( ) ) ;
51
-
52
41
// Create the file for reading and writing, because some encoders do both
53
42
// (e.g. the metadata encoder when -Zmeta-stats is enabled)
54
43
let file = File :: options ( ) . read ( true ) . write ( true ) . create ( true ) . truncate ( true ) . open ( path) ?;
55
44
56
45
Ok ( FileEncoder {
57
- buf : Box :: new_uninit_slice ( capacity ) ,
46
+ buf : Box :: new_uninit_slice ( BUF_SIZE ) ,
58
47
buffered : 0 ,
59
48
flushed : 0 ,
60
49
file,
@@ -159,19 +148,11 @@ impl FileEncoder {
159
148
& self . file
160
149
}
161
150
162
- #[ inline]
163
- fn capacity ( & self ) -> usize {
164
- self . buf . len ( )
165
- }
166
-
167
151
#[ inline]
168
152
fn write_one ( & mut self , value : u8 ) {
169
- // We ensure this during `FileEncoder` construction.
170
- debug_assert ! ( self . capacity( ) >= 1 ) ;
171
-
172
153
let mut buffered = self . buffered ;
173
154
174
- if std:: intrinsics:: unlikely ( buffered >= self . capacity ( ) ) {
155
+ if std:: intrinsics:: unlikely ( buffered + 1 > BUF_SIZE ) {
175
156
self . flush ( ) ;
176
157
buffered = 0 ;
177
158
}
@@ -187,13 +168,12 @@ impl FileEncoder {
187
168
188
169
#[ inline]
189
170
fn write_all ( & mut self , buf : & [ u8 ] ) {
190
- let capacity = self . capacity ( ) ;
191
171
let buf_len = buf. len ( ) ;
192
172
193
- if std:: intrinsics:: likely ( buf_len <= capacity ) {
173
+ if std:: intrinsics:: likely ( buf_len <= BUF_SIZE ) {
194
174
let mut buffered = self . buffered ;
195
175
196
- if std:: intrinsics:: unlikely ( buf_len > capacity - buffered ) {
176
+ if std:: intrinsics:: unlikely ( buffered + buf_len > BUF_SIZE ) {
197
177
self . flush ( ) ;
198
178
buffered = 0 ;
199
179
}
@@ -271,13 +251,11 @@ macro_rules! write_leb128 {
271
251
fn $this_fn( & mut self , v: $int_ty) {
272
252
const MAX_ENCODED_LEN : usize = $crate:: leb128:: max_leb128_len:: <$int_ty>( ) ;
273
253
274
- // We ensure this during `FileEncoder` construction.
275
- debug_assert!( self . capacity( ) >= MAX_ENCODED_LEN ) ;
276
-
277
254
let mut buffered = self . buffered;
278
255
279
- // This can't overflow. See assertion in `FileEncoder::with_capacity`.
280
- if std:: intrinsics:: unlikely( buffered + MAX_ENCODED_LEN > self . capacity( ) ) {
256
+ // This can't overflow because BUF_SIZE and MAX_ENCODED_LEN are both
257
+ // quite small.
258
+ if std:: intrinsics:: unlikely( buffered + MAX_ENCODED_LEN > BUF_SIZE ) {
281
259
self . flush( ) ;
282
260
buffered = 0 ;
283
261
}
0 commit comments