@@ -6,27 +6,32 @@ const { Buffer } = require('buffer');
6
6
7
7
const { FSReqCallback, close, read } = internalBinding ( 'fs' ) ;
8
8
9
- const kReadFileBufferLength = 8 * 1024 ;
9
+ // Use 64kb in case the file type is not a regular file and thus do not know the
10
+ // actual file size. Increasing the value further results in more frequent over
11
+ // allocation for small files and consumes CPU time and memory that should be
12
+ // used else wise.
13
+ // Use up to 512kb per read otherwise to partition reading big files to prevent
14
+ // blocking other threads in case the available threads are all in use.
15
+ const kReadFileUnknownBufferLength = 64 * 1024 ;
16
+ const kReadFileBufferLength = 512 * 1024 ;
10
17
11
18
function readFileAfterRead ( err , bytesRead ) {
12
19
const context = this . context ;
13
20
14
21
if ( err )
15
22
return context . close ( err ) ;
16
23
17
- if ( bytesRead === 0 )
18
- return context . close ( ) ;
19
-
20
24
context . pos += bytesRead ;
21
25
22
- if ( context . size !== 0 ) {
23
- if ( context . pos === context . size )
24
- context . close ( ) ;
25
- else
26
- context . read ( ) ;
26
+ if ( context . pos === context . size || bytesRead === 0 ) {
27
+ context . close ( ) ;
27
28
} else {
28
- // Unknown size, just read until we don't get bytes.
29
- context . buffers . push ( context . buffer . slice ( 0 , bytesRead ) ) ;
29
+ if ( context . size === 0 ) {
30
+ // Unknown size, just read until we don't get bytes.
31
+ const buffer = bytesRead === kReadFileUnknownBufferLength ?
32
+ context . buffer : context . buffer . slice ( 0 , bytesRead ) ;
33
+ context . buffers . push ( buffer ) ;
34
+ }
30
35
context . read ( ) ;
31
36
}
32
37
}
@@ -60,7 +65,7 @@ class ReadFileContext {
60
65
constructor ( callback , encoding ) {
61
66
this . fd = undefined ;
62
67
this . isUserFd = undefined ;
63
- this . size = undefined ;
68
+ this . size = 0 ;
64
69
this . callback = callback ;
65
70
this . buffers = null ;
66
71
this . buffer = null ;
@@ -75,9 +80,10 @@ class ReadFileContext {
75
80
let length ;
76
81
77
82
if ( this . size === 0 ) {
78
- buffer = this . buffer = Buffer . allocUnsafeSlow ( kReadFileBufferLength ) ;
83
+ buffer = Buffer . allocUnsafeSlow ( kReadFileUnknownBufferLength ) ;
79
84
offset = 0 ;
80
- length = kReadFileBufferLength ;
85
+ length = kReadFileUnknownBufferLength ;
86
+ this . buffer = buffer ;
81
87
} else {
82
88
buffer = this . buffer ;
83
89
offset = this . pos ;
0 commit comments