1- use crate :: leb128:: { self , largest_max_leb128_len } ;
1+ use crate :: leb128;
22use crate :: serialize:: { Decodable , Decoder , Encodable , Encoder } ;
33use std:: fs:: File ;
44use std:: io:: { self , Write } ;
@@ -14,6 +14,9 @@ use std::ptr;
1414
1515pub type FileEncodeResult = Result < usize , io:: Error > ;
1616
17+ /// The size of the buffer in `FileEncoder`.
18+ const BUF_SIZE : usize = 8192 ;
19+
1720/// `FileEncoder` encodes data to file via fixed-size buffer.
1821///
1922/// There used to be a `MemEncoder` type that encoded all the data into a
@@ -35,26 +38,12 @@ pub struct FileEncoder {
3538
3639impl FileEncoder {
3740 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-
5241 // Create the file for reading and writing, because some encoders do both
5342 // (e.g. the metadata encoder when -Zmeta-stats is enabled)
5443 let file = File :: options ( ) . read ( true ) . write ( true ) . create ( true ) . truncate ( true ) . open ( path) ?;
5544
5645 Ok ( FileEncoder {
57- buf : Box :: new_uninit_slice ( capacity ) ,
46+ buf : Box :: new_uninit_slice ( BUF_SIZE ) ,
5847 buffered : 0 ,
5948 flushed : 0 ,
6049 file,
@@ -159,19 +148,11 @@ impl FileEncoder {
159148 & self . file
160149 }
161150
162- #[ inline]
163- fn capacity ( & self ) -> usize {
164- self . buf . len ( )
165- }
166-
167151 #[ inline]
168152 fn write_one ( & mut self , value : u8 ) {
169- // We ensure this during `FileEncoder` construction.
170- debug_assert ! ( self . capacity( ) >= 1 ) ;
171-
172153 let mut buffered = self . buffered ;
173154
174- if std:: intrinsics:: unlikely ( buffered >= self . capacity ( ) ) {
155+ if std:: intrinsics:: unlikely ( buffered + 1 > BUF_SIZE ) {
175156 self . flush ( ) ;
176157 buffered = 0 ;
177158 }
@@ -187,13 +168,12 @@ impl FileEncoder {
187168
188169 #[ inline]
189170 fn write_all ( & mut self , buf : & [ u8 ] ) {
190- let capacity = self . capacity ( ) ;
191171 let buf_len = buf. len ( ) ;
192172
193- if std:: intrinsics:: likely ( buf_len <= capacity ) {
173+ if std:: intrinsics:: likely ( buf_len <= BUF_SIZE ) {
194174 let mut buffered = self . buffered ;
195175
196- if std:: intrinsics:: unlikely ( buf_len > capacity - buffered ) {
176+ if std:: intrinsics:: unlikely ( buffered + buf_len > BUF_SIZE ) {
197177 self . flush ( ) ;
198178 buffered = 0 ;
199179 }
@@ -271,13 +251,11 @@ macro_rules! write_leb128 {
271251 fn $this_fn( & mut self , v: $int_ty) {
272252 const MAX_ENCODED_LEN : usize = $crate:: leb128:: max_leb128_len:: <$int_ty>( ) ;
273253
274- // We ensure this during `FileEncoder` construction.
275- debug_assert!( self . capacity( ) >= MAX_ENCODED_LEN ) ;
276-
277254 let mut buffered = self . buffered;
278255
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 ) {
281259 self . flush( ) ;
282260 buffered = 0 ;
283261 }
0 commit comments