Skip to content

Commit d09d878

Browse files
committed
fs: use AliasedBuffer for fs_stats_field_array
PR-URL: #18276 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 13bc53f commit d09d878

File tree

6 files changed

+37
-50
lines changed

6 files changed

+37
-50
lines changed

lib/fs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ Stats.prototype.isSocket = function() {
341341
return this._checkModeProperty(S_IFSOCK);
342342
};
343343

344-
const statValues = binding.getStatValues();
344+
const statValues = binding.statValues;
345345

346346
function statsFromValues() {
347347
return new Stats(statValues[0], statValues[1], statValues[2], statValues[3],

src/env-inl.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ inline Environment::Environment(IsolateData* isolate_data,
322322
#endif
323323
handle_cleanup_waiting_(0),
324324
http_parser_buffer_(nullptr),
325-
fs_stats_field_array_(nullptr),
325+
fs_stats_field_array_(isolate_, kFsStatsFieldsLength),
326326
context_(context->GetIsolate(), context) {
327327
// We'll be creating new objects so make sure we've entered the context.
328328
v8::HandleScope handle_scope(isolate());
@@ -547,13 +547,9 @@ inline void Environment::set_http2_state(
547547
http2_state_ = std::move(buffer);
548548
}
549549

550-
inline double* Environment::fs_stats_field_array() const {
551-
return fs_stats_field_array_;
552-
}
553-
554-
inline void Environment::set_fs_stats_field_array(double* fields) {
555-
CHECK_EQ(fs_stats_field_array_, nullptr); // Should be set only once.
556-
fs_stats_field_array_ = fields;
550+
inline AliasedBuffer<double, v8::Float64Array>*
551+
Environment::fs_stats_field_array() {
552+
return &fs_stats_field_array_;
557553
}
558554

559555
void Environment::CreateImmediate(native_immediate_callback cb,

src/env.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -610,8 +610,7 @@ class Environment {
610610
inline http2::http2_state* http2_state() const;
611611
inline void set_http2_state(std::unique_ptr<http2::http2_state> state);
612612

613-
inline double* fs_stats_field_array() const;
614-
inline void set_fs_stats_field_array(double* fields);
613+
inline AliasedBuffer<double, v8::Float64Array>* fs_stats_field_array();
615614

616615
inline performance::performance_state* performance_state();
617616
inline std::map<std::string, uint64_t>* performance_marks();
@@ -778,7 +777,10 @@ class Environment {
778777
char* http_parser_buffer_;
779778
std::unique_ptr<http2::http2_state> http2_state_;
780779

781-
double* fs_stats_field_array_;
780+
// stat fields contains twice the number of entries because `fs.StatWatcher`
781+
// needs room to store data for *two* `fs.Stats` instances.
782+
static const int kFsStatsFieldsLength = 2 * 14;
783+
AliasedBuffer<double, v8::Float64Array> fs_stats_field_array_;
782784

783785
struct BeforeExitCallback {
784786
void (*cb_)(void* arg);

src/node_file.cc

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

22+
#include "aliased_buffer.h"
2223
#include "node_buffer.h"
2324
#include "node_internals.h"
2425
#include "node_stat_watcher.h"
@@ -44,32 +45,34 @@
4445

4546
namespace node {
4647

47-
void FillStatsArray(double* fields, const uv_stat_t* s) {
48-
fields[0] = s->st_dev;
49-
fields[1] = s->st_mode;
50-
fields[2] = s->st_nlink;
51-
fields[3] = s->st_uid;
52-
fields[4] = s->st_gid;
53-
fields[5] = s->st_rdev;
48+
void FillStatsArray(AliasedBuffer<double, v8::Float64Array>* fields_ptr,
49+
const uv_stat_t* s, int offset) {
50+
AliasedBuffer<double, v8::Float64Array>& fields = *fields_ptr;
51+
fields[offset + 0] = s->st_dev;
52+
fields[offset + 1] = s->st_mode;
53+
fields[offset + 2] = s->st_nlink;
54+
fields[offset + 3] = s->st_uid;
55+
fields[offset + 4] = s->st_gid;
56+
fields[offset + 5] = s->st_rdev;
5457
#if defined(__POSIX__)
55-
fields[6] = s->st_blksize;
58+
fields[offset + 6] = s->st_blksize;
5659
#else
57-
fields[6] = -1;
60+
fields[offset + 6] = -1;
5861
#endif
59-
fields[7] = s->st_ino;
60-
fields[8] = s->st_size;
62+
fields[offset + 7] = s->st_ino;
63+
fields[offset + 8] = s->st_size;
6164
#if defined(__POSIX__)
62-
fields[9] = s->st_blocks;
65+
fields[offset + 9] = s->st_blocks;
6366
#else
64-
fields[9] = -1;
67+
fields[offset + 9] = -1;
6568
#endif
6669
// Dates.
6770
// NO-LINT because the fields are 'long' and we just want to cast to `unsigned`
68-
#define X(idx, name) \
69-
/* NOLINTNEXTLINE(runtime/int) */ \
70-
fields[idx] = ((unsigned long)(s->st_##name.tv_sec) * 1e3) + \
71-
/* NOLINTNEXTLINE(runtime/int) */ \
72-
((unsigned long)(s->st_##name.tv_nsec) / 1e6); \
71+
#define X(idx, name) \
72+
/* NOLINTNEXTLINE(runtime/int) */ \
73+
fields[offset + idx] = ((unsigned long)(s->st_##name.tv_sec) * 1e3) + \
74+
/* NOLINTNEXTLINE(runtime/int) */ \
75+
((unsigned long)(s->st_##name.tv_nsec) / 1e6); \
7376

7477
X(10, atim)
7578
X(11, mtim)
@@ -81,7 +84,6 @@ void FillStatsArray(double* fields, const uv_stat_t* s) {
8184
namespace fs {
8285

8386
using v8::Array;
84-
using v8::ArrayBuffer;
8587
using v8::Context;
8688
using v8::Float64Array;
8789
using v8::Function;
@@ -1295,22 +1297,6 @@ static void Mkdtemp(const FunctionCallbackInfo<Value>& args) {
12951297
}
12961298
}
12971299

1298-
void GetStatValues(const FunctionCallbackInfo<Value>& args) {
1299-
Environment* env = Environment::GetCurrent(args);
1300-
double* fields = env->fs_stats_field_array();
1301-
if (fields == nullptr) {
1302-
// stat fields contains twice the number of entries because `fs.StatWatcher`
1303-
// needs room to store data for *two* `fs.Stats` instances.
1304-
fields = new double[2 * 14];
1305-
env->set_fs_stats_field_array(fields);
1306-
}
1307-
Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(),
1308-
fields,
1309-
sizeof(double) * 2 * 14);
1310-
Local<Float64Array> fields_array = Float64Array::New(ab, 0, 2 * 14);
1311-
args.GetReturnValue().Set(fields_array);
1312-
}
1313-
13141300
void InitFs(Local<Object> target,
13151301
Local<Value> unused,
13161302
Local<Context> context,
@@ -1356,7 +1342,9 @@ void InitFs(Local<Object> target,
13561342

13571343
env->SetMethod(target, "mkdtemp", Mkdtemp);
13581344

1359-
env->SetMethod(target, "getStatValues", GetStatValues);
1345+
target->Set(context,
1346+
FIXED_ONE_BYTE_STRING(env->isolate(), "statValues"),
1347+
env->fs_stats_field_array()->GetJSArray()).FromJust();
13601348

13611349
StatWatcher::Initialize(env, target);
13621350

src/node_internals.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ v8::Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,
292292
const char* warning,
293293
const char* deprecation_code);
294294

295-
void FillStatsArray(double* fields, const uv_stat_t* s);
295+
void FillStatsArray(AliasedBuffer<double, v8::Float64Array>* fields_ptr,
296+
const uv_stat_t* s, int offset = 0);
296297

297298
void SetupProcessObject(Environment* env,
298299
int argc,

src/node_stat_watcher.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void StatWatcher::Callback(uv_fs_poll_t* handle,
8989
Context::Scope context_scope(env->context());
9090

9191
FillStatsArray(env->fs_stats_field_array(), curr);
92-
FillStatsArray(env->fs_stats_field_array() + 14, prev);
92+
FillStatsArray(env->fs_stats_field_array(), prev, 14);
9393
Local<Value> arg = Integer::New(env->isolate(), status);
9494
wrap->MakeCallback(env->onchange_string(), 1, &arg);
9595
}

0 commit comments

Comments
 (0)