Skip to content

Commit

Permalink
fs: use AliasedBuffer for fs_stats_field_array
Browse files Browse the repository at this point in the history
PR-URL: #18276
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
joyeecheung committed Jan 23, 2018
1 parent 13bc53f commit d09d878
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 50 deletions.
2 changes: 1 addition & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ Stats.prototype.isSocket = function() {
return this._checkModeProperty(S_IFSOCK);
};

const statValues = binding.getStatValues();
const statValues = binding.statValues;

function statsFromValues() {
return new Stats(statValues[0], statValues[1], statValues[2], statValues[3],
Expand Down
12 changes: 4 additions & 8 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ inline Environment::Environment(IsolateData* isolate_data,
#endif
handle_cleanup_waiting_(0),
http_parser_buffer_(nullptr),
fs_stats_field_array_(nullptr),
fs_stats_field_array_(isolate_, kFsStatsFieldsLength),
context_(context->GetIsolate(), context) {
// We'll be creating new objects so make sure we've entered the context.
v8::HandleScope handle_scope(isolate());
Expand Down Expand Up @@ -547,13 +547,9 @@ inline void Environment::set_http2_state(
http2_state_ = std::move(buffer);
}

inline double* Environment::fs_stats_field_array() const {
return fs_stats_field_array_;
}

inline void Environment::set_fs_stats_field_array(double* fields) {
CHECK_EQ(fs_stats_field_array_, nullptr); // Should be set only once.
fs_stats_field_array_ = fields;
inline AliasedBuffer<double, v8::Float64Array>*
Environment::fs_stats_field_array() {
return &fs_stats_field_array_;
}

void Environment::CreateImmediate(native_immediate_callback cb,
Expand Down
8 changes: 5 additions & 3 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,7 @@ class Environment {
inline http2::http2_state* http2_state() const;
inline void set_http2_state(std::unique_ptr<http2::http2_state> state);

inline double* fs_stats_field_array() const;
inline void set_fs_stats_field_array(double* fields);
inline AliasedBuffer<double, v8::Float64Array>* fs_stats_field_array();

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

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

struct BeforeExitCallback {
void (*cb_)(void* arg);
Expand Down
60 changes: 24 additions & 36 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

#include "aliased_buffer.h"
#include "node_buffer.h"
#include "node_internals.h"
#include "node_stat_watcher.h"
Expand All @@ -44,32 +45,34 @@

namespace node {

void FillStatsArray(double* fields, const uv_stat_t* s) {
fields[0] = s->st_dev;
fields[1] = s->st_mode;
fields[2] = s->st_nlink;
fields[3] = s->st_uid;
fields[4] = s->st_gid;
fields[5] = s->st_rdev;
void FillStatsArray(AliasedBuffer<double, v8::Float64Array>* fields_ptr,
const uv_stat_t* s, int offset) {
AliasedBuffer<double, v8::Float64Array>& fields = *fields_ptr;
fields[offset + 0] = s->st_dev;
fields[offset + 1] = s->st_mode;
fields[offset + 2] = s->st_nlink;
fields[offset + 3] = s->st_uid;
fields[offset + 4] = s->st_gid;
fields[offset + 5] = s->st_rdev;
#if defined(__POSIX__)
fields[6] = s->st_blksize;
fields[offset + 6] = s->st_blksize;
#else
fields[6] = -1;
fields[offset + 6] = -1;
#endif
fields[7] = s->st_ino;
fields[8] = s->st_size;
fields[offset + 7] = s->st_ino;
fields[offset + 8] = s->st_size;
#if defined(__POSIX__)
fields[9] = s->st_blocks;
fields[offset + 9] = s->st_blocks;
#else
fields[9] = -1;
fields[offset + 9] = -1;
#endif
// Dates.
// NO-LINT because the fields are 'long' and we just want to cast to `unsigned`
#define X(idx, name) \
/* NOLINTNEXTLINE(runtime/int) */ \
fields[idx] = ((unsigned long)(s->st_##name.tv_sec) * 1e3) + \
/* NOLINTNEXTLINE(runtime/int) */ \
((unsigned long)(s->st_##name.tv_nsec) / 1e6); \
#define X(idx, name) \
/* NOLINTNEXTLINE(runtime/int) */ \
fields[offset + idx] = ((unsigned long)(s->st_##name.tv_sec) * 1e3) + \
/* NOLINTNEXTLINE(runtime/int) */ \
((unsigned long)(s->st_##name.tv_nsec) / 1e6); \

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

using v8::Array;
using v8::ArrayBuffer;
using v8::Context;
using v8::Float64Array;
using v8::Function;
Expand Down Expand Up @@ -1295,22 +1297,6 @@ static void Mkdtemp(const FunctionCallbackInfo<Value>& args) {
}
}

void GetStatValues(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
double* fields = env->fs_stats_field_array();
if (fields == nullptr) {
// stat fields contains twice the number of entries because `fs.StatWatcher`
// needs room to store data for *two* `fs.Stats` instances.
fields = new double[2 * 14];
env->set_fs_stats_field_array(fields);
}
Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(),
fields,
sizeof(double) * 2 * 14);
Local<Float64Array> fields_array = Float64Array::New(ab, 0, 2 * 14);
args.GetReturnValue().Set(fields_array);
}

void InitFs(Local<Object> target,
Local<Value> unused,
Local<Context> context,
Expand Down Expand Up @@ -1356,7 +1342,9 @@ void InitFs(Local<Object> target,

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

env->SetMethod(target, "getStatValues", GetStatValues);
target->Set(context,
FIXED_ONE_BYTE_STRING(env->isolate(), "statValues"),
env->fs_stats_field_array()->GetJSArray()).FromJust();

StatWatcher::Initialize(env, target);

Expand Down
3 changes: 2 additions & 1 deletion src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ v8::Maybe<bool> ProcessEmitDeprecationWarning(Environment* env,
const char* warning,
const char* deprecation_code);

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

void SetupProcessObject(Environment* env,
int argc,
Expand Down
2 changes: 1 addition & 1 deletion src/node_stat_watcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void StatWatcher::Callback(uv_fs_poll_t* handle,
Context::Scope context_scope(env->context());

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

0 comments on commit d09d878

Please sign in to comment.