From 84464c92d574fd4edf076c17fa8296f50076d58a Mon Sep 17 00:00:00 2001 From: aitbenmouh Date: Fri, 31 Mar 2017 15:56:40 +0100 Subject: [PATCH 1/3] optimaze some type conversion --- src/async-wrap.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/async-wrap.cc b/src/async-wrap.cc index bc3e049d262c81..2b3ce781bcac4d 100644 --- a/src/async-wrap.cc +++ b/src/async-wrap.cc @@ -226,7 +226,7 @@ void AsyncWrap::DestroyIdsCb(uv_idle_t* handle) { // Want each callback to be cleaned up after itself, instead of cleaning // them all up after the while() loop completes. HandleScope scope(env->isolate()); - Local argv = Number::New(env->isolate(), current_id); + Local argv = Number::New(env->isolate(), static_cast(current_id)); MaybeLocal ret = fn->Call( env->context(), Undefined(env->isolate()), 1, &argv); @@ -260,7 +260,7 @@ AsyncWrap::AsyncWrap(Environment* env, CHECK_GE(object->InternalFieldCount(), 1); // Shift provider value over to prevent id collision. - persistent().SetWrapperClassId(NODE_ASYNC_ID_OFFSET + provider); + persistent().SetWrapperClassId(static_cast(NODE_ASYNC_ID_OFFSET + provider)); Local init_fn = env->async_hooks_init_function(); @@ -277,14 +277,14 @@ AsyncWrap::AsyncWrap(Environment* env, HandleScope scope(env->isolate()); Local argv[] = { - Number::New(env->isolate(), get_uid()), + Number::New(env->isolate(), static_cast(get_uid())), Int32::New(env->isolate(), provider), Null(env->isolate()), Null(env->isolate()) }; if (parent != nullptr) { - argv[2] = Number::New(env->isolate(), parent->get_uid()); + argv[2] = Number::New(env->isolate(), static_cast(parent->get_uid())); argv[3] = parent->object(); } @@ -320,7 +320,7 @@ Local AsyncWrap::MakeCallback(const Local cb, Local pre_fn = env()->async_hooks_pre_function(); Local post_fn = env()->async_hooks_post_function(); - Local uid = Number::New(env()->isolate(), get_uid()); + Local uid = Number::New(env()->isolate(), static_cast(get_uid())); Local context = object(); Local domain; bool has_domain = false; From b19aa380a4f826559369fc9e91e60fd540cc268e Mon Sep 17 00:00:00 2001 From: aitbenmouh Date: Fri, 31 Mar 2017 18:32:24 +0100 Subject: [PATCH 2/3] code optimisation --- src/cares_wrap.cc | 4 ++-- src/js_stream.cc | 2 +- src/node.cc | 8 ++++---- src/node_crypto.cc | 8 ++++---- src/node_crypto_clienthello-inl.h | 2 +- src/node_file.cc | 22 +++++++++++----------- src/node_http_parser.cc | 4 ++-- src/node_os.cc | 16 ++++++++-------- src/node_url.cc | 12 ++++++------ src/stream_base.cc | 2 +- src/stream_wrap.cc | 4 ++-- src/string_bytes.cc | 4 ++-- src/string_search.h | 2 +- 13 files changed, 45 insertions(+), 45 deletions(-) diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index 9fe20f15903e1e..0ca87f13d2b0f2 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -1150,7 +1150,7 @@ static void GetAddrInfo(const FunctionCallbackInfo& args) { node::Utf8Value hostname(env->isolate(), args[1]); int32_t flags = (args[3]->IsInt32()) ? args[3]->Int32Value() : 0; - int family; + int family = 0; switch (args[2]->Int32Value()) { case 0: @@ -1263,7 +1263,7 @@ static void SetServers(const FunctionCallbackInfo& args) { ares_addr_node* servers = new ares_addr_node[len]; ares_addr_node* last = nullptr; - int err; + int err = 0; for (uint32_t i = 0; i < len; i++) { CHECK(arr->Get(i)->IsArray()); diff --git a/src/js_stream.cc b/src/js_stream.cc index e51c4ae9b35084..df7dec73f36191 100644 --- a/src/js_stream.cc +++ b/src/js_stream.cc @@ -115,7 +115,7 @@ void JSStream::New(const FunctionCallbackInfo& args) { // normal function. CHECK(args.IsConstructCall()); Environment* env = Environment::GetCurrent(args); - JSStream* wrap; + JSStream* wrap = NULL; if (args.Length() == 0) { wrap = new JSStream(env, args.This(), nullptr); diff --git a/src/node.cc b/src/node.cc index 52bf719852d891..5c587a19737f56 100644 --- a/src/node.cc +++ b/src/node.cc @@ -2303,10 +2303,10 @@ void MemoryUsage(const FunctionCallbackInfo& args) { Local ab = array->Buffer(); double* fields = static_cast(ab->GetContents().Data()); - fields[0] = rss; - fields[1] = v8_heap_stats.total_heap_size(); - fields[2] = v8_heap_stats.used_heap_size(); - fields[3] = isolate->AdjustAmountOfExternalAllocatedMemory(0); + fields[0] = static_cast(rss); + fields[1] = static_cast(v8_heap_stats.total_heap_size()); + fields[2] = static_cast(v8_heap_stats.used_heap_size()); + fields[3] = static_cast(isolate->AdjustAmountOfExternalAllocatedMemory(0)); //possible loss data } diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 7698cf2062c8e1..5a4f0bfbc6ac62 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -3371,7 +3371,7 @@ void CipherBase::Init(const FunctionCallbackInfo& args) { const node::Utf8Value cipher_type(args.GetIsolate(), args[0]); const char* key_buf = Buffer::Data(args[1]); ssize_t key_buf_len = Buffer::Length(args[1]); - cipher->Init(*cipher_type, key_buf, key_buf_len); + cipher->Init(*cipher_type, key_buf, static_cast(key_buf_len)); } @@ -3437,7 +3437,7 @@ void CipherBase::InitIv(const FunctionCallbackInfo& args) { const char* key_buf = Buffer::Data(args[1]); ssize_t iv_len = Buffer::Length(args[2]); const char* iv_buf = Buffer::Data(args[2]); - cipher->InitIv(*cipher_type, key_buf, key_len, iv_buf, iv_len); + cipher->InitIv(*cipher_type, key_buf, static_cast(key_len), iv_buf, static_cast(iv_len)); } @@ -4378,7 +4378,7 @@ void Verify::VerifyFinal(const FunctionCallbackInfo& args) { } bool verify_result; - Error err = verify->VerifyFinal(kbuf, klen, hbuf, hlen, &verify_result); + Error err = verify->VerifyFinal(kbuf, klen, hbuf, static_cast(hlen), &verify_result); if (args[1]->IsString()) delete[] hbuf; if (err != kSignOk) @@ -4508,7 +4508,7 @@ void PublicKeyCipher::Cipher(const FunctionCallbackInfo& args) { reinterpret_cast(buf), len, &out_value, - &out_len); + static_cast(&out_len)); if (out_len == 0 || !r) { delete[] out_value; diff --git a/src/node_crypto_clienthello-inl.h b/src/node_crypto_clienthello-inl.h index 4044b0cde246df..8783e6021d0e0d 100644 --- a/src/node_crypto_clienthello-inl.h +++ b/src/node_crypto_clienthello-inl.h @@ -35,7 +35,7 @@ inline void ClientHelloParser::Reset() { extension_offset_ = 0; session_size_ = 0; session_id_ = nullptr; - tls_ticket_size_ = -1; + tls_ticket_size_ = static_cast(-1); tls_ticket_ = nullptr; servername_size_ = 0; servername_ = nullptr; diff --git a/src/node_file.cc b/src/node_file.cc index 9c180833fb926f..e10d0a4ee7b44c 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -224,11 +224,11 @@ static void After(uv_fs_t *req) { break; case UV_FS_OPEN: - argv[1] = Integer::New(env->isolate(), req->result); + argv[1] = Integer::New(env->isolate(), static_cast(req->result)); //possible los of data break; case UV_FS_WRITE: - argv[1] = Integer::New(env->isolate(), req->result); + argv[1] = Integer::New(env->isolate(), static_cast(req->result)); break; case UV_FS_MKDTEMP: @@ -281,7 +281,7 @@ static void After(uv_fs_t *req) { case UV_FS_READ: // Buffer interface - argv[1] = Integer::New(env->isolate(), req->result); + argv[1] = Integer::New(env->isolate(), static_cast(req->result)); break; case UV_FS_SCANDIR: @@ -442,19 +442,19 @@ static void Close(const FunctionCallbackInfo& args) { 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; + fields[0] = static_cast(s->st_dev); + fields[1] = static_cast(s->st_mode); + fields[2] = static_cast(s->st_nlink); + fields[3] = static_cast(s->st_uid); + fields[4] = static_cast(s->st_gid); + fields[5] = static_cast(s->st_rdev); #if defined(__POSIX__) fields[6] = s->st_blksize; #else fields[6] = -1; #endif - fields[7] = s->st_ino; - fields[8] = s->st_size; + fields[7] = static_cast(s->st_ino); + fields[8] = static_cast(s->st_size); #if defined(__POSIX__) fields[9] = s->st_blocks; #else diff --git a/src/node_http_parser.cc b/src/node_http_parser.cc index 38450148b706a8..2a3880f88c6061 100644 --- a/src/node_http_parser.cc +++ b/src/node_http_parser.cc @@ -315,7 +315,7 @@ class Parser : public AsyncWrap { return -1; } - return head_response->IntegerValue(); + return static_cast(head_response->IntegerValue()); } @@ -339,7 +339,7 @@ class Parser : public AsyncWrap { Local argv[3] = { current_buffer_, - Integer::NewFromUnsigned(env()->isolate(), at - current_buffer_data_), + Integer::NewFromUnsigned(env()->isolate(), static_cast(at - current_buffer_data_)), Integer::NewFromUnsigned(env()->isolate(), length) }; diff --git a/src/node_os.cc b/src/node_os.cc index d7be9095b170c9..910d17dc9668bc 100644 --- a/src/node_os.cc +++ b/src/node_os.cc @@ -168,12 +168,12 @@ static void GetCPUInfo(const FunctionCallbackInfo& args) { for (i = 0, field_idx = 0; i < count; i++) { uv_cpu_info_t* ci = cpu_infos + i; - fields[field_idx++] = ci->speed; - fields[field_idx++] = ci->cpu_times.user; - fields[field_idx++] = ci->cpu_times.nice; - fields[field_idx++] = ci->cpu_times.sys; - fields[field_idx++] = ci->cpu_times.idle; - fields[field_idx++] = ci->cpu_times.irq; + fields[field_idx++] = static_cast(ci->speed); + fields[field_idx++] = static_cast(ci->cpu_times.user); + fields[field_idx++] = static_cast(ci->cpu_times.nice); + fields[field_idx++] = static_cast(ci->cpu_times.sys); + fields[field_idx++] = static_cast(ci->cpu_times.idle); + fields[field_idx++] = static_cast(ci->cpu_times.irq); model_argv[model_idx++] = OneByteString(env->isolate(), ci->model); if (model_idx >= NODE_PUSH_VAL_TO_ARRAY_MAX) { @@ -193,7 +193,7 @@ static void GetCPUInfo(const FunctionCallbackInfo& args) { static void GetFreeMemory(const FunctionCallbackInfo& args) { - double amount = uv_get_free_memory(); + double amount = static_cast(uv_get_free_memory()); if (amount < 0) return; args.GetReturnValue().Set(amount); @@ -201,7 +201,7 @@ static void GetFreeMemory(const FunctionCallbackInfo& args) { static void GetTotalMemory(const FunctionCallbackInfo& args) { - double amount = uv_get_total_memory(); + double amount = static_cast(uv_get_total_memory()); if (amount < 0) return; args.GetReturnValue().Set(amount); diff --git a/src/node_url.cc b/src/node_url.cc index 6cd78c2c6c04c8..43c19ef9cddeb0 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -167,7 +167,7 @@ namespace url { } if (dots < 3 && ch != '.') goto end; - *piece_pointer = *piece_pointer * 0x100 + value; + *piece_pointer = static_cast(*piece_pointer * 0x100 + value); //aitbenmouh possible loss data if (dots & 0x1) piece_pointer++; if (ch != kEOL) { @@ -190,12 +190,12 @@ namespace url { default: goto end; } - *piece_pointer = value; + *piece_pointer = static_cast(value); piece_pointer++; } if (compress_pointer != nullptr) { - swaps = piece_pointer - compress_pointer; + swaps = static_cast(piece_pointer - compress_pointer); piece_pointer = last_piece - 1; while (piece_pointer != &host->value.ipv6[0] && swaps > 0) { uint16_t temp = *piece_pointer; @@ -266,7 +266,7 @@ namespace url { while (pointer <= end) { const char ch = pointer < end ? pointer[0] : kEOL; - const int remaining = end - pointer - 1; + const int remaining = static_cast(end - pointer - 1); if (ch == '.' || ch == kEOL) { if (++parts > 4) goto end; @@ -299,10 +299,10 @@ namespace url { } type = HOST_TYPE_IPV4; - val = numbers[parts - 1]; + val = static_cast(numbers[parts - 1]); for (int n = 0; n < parts - 1; n++) { double b = 3 - n; - val += numbers[n] * pow(256, b); + val += static_cast(numbers[n] * pow(256, b)); } host->value.ipv4 = val; diff --git a/src/stream_base.cc b/src/stream_base.cc index 3ed622d7ef35a2..fc921db83e303b 100644 --- a/src/stream_base.cc +++ b/src/stream_base.cc @@ -393,7 +393,7 @@ void StreamBase::EmitData(ssize_t nread, Environment* env = env_; Local argv[] = { - Integer::New(env->isolate(), nread), + Integer::New(env->isolate(), static_cast(nread)), buf, handle }; diff --git a/src/stream_wrap.cc b/src/stream_wrap.cc index 099151fdb71c35..6c3ff45fc96c9d 100644 --- a/src/stream_wrap.cc +++ b/src/stream_wrap.cc @@ -251,9 +251,9 @@ void StreamWrap::OnReadCommon(uv_stream_t* handle, if (nread > 0) { if (wrap->is_tcp()) { - NODE_COUNT_NET_BYTES_RECV(nread); + NODE_COUNT_NET_BYTES_RECV(static_cast(nread)); } else if (wrap->is_named_pipe()) { - NODE_COUNT_PIPE_BYTES_RECV(nread); + NODE_COUNT_PIPE_BYTES_RECV(static_cast(nread)); } } diff --git a/src/string_bytes.cc b/src/string_bytes.cc index 39239592595e7a..dda72c7322d5de 100644 --- a/src/string_bytes.cc +++ b/src/string_bytes.cc @@ -197,7 +197,7 @@ size_t hex_decode(char* buf, unsigned b = unhex(src[i * 2 + 1]); if (!~a || !~b) return i; - buf[i] = (a << 4) | b; + buf[i] = static_cast((a << 4) | b); } return i; @@ -515,7 +515,7 @@ static bool contains_non_ascii(const char* src, size_t len) { #if defined(_WIN64) || defined(_LP64) - const uintptr_t mask = 0x8080808080808080ll; + const uintptr_t mask = static_cast(0x8080808080808080ll); #else const uintptr_t mask = 0x80808080l; #endif diff --git a/src/string_search.h b/src/string_search.h index 6040888110c2ec..066543977d3b8a 100644 --- a/src/string_search.h +++ b/src/string_search.h @@ -403,7 +403,7 @@ size_t StringSearch::BoyerMooreSearch( static_cast(last_char)); } else { int gs_shift = good_suffix_shift[j + 1]; - int bc_occ = CharOccurrence(bad_char_occurrence, c); + int bc_occ = CharOccurrence(bad_char_occurrence, static_cast(c)); int shift = j - bc_occ; if (gs_shift > shift) { shift = gs_shift; From 45f6311ded2391e96fe0133dcfea1924fe16282b Mon Sep 17 00:00:00 2001 From: aitbenmouh Date: Mon, 3 Apr 2017 14:12:06 +0100 Subject: [PATCH 3/3] small commit --- src/node_url.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_url.cc b/src/node_url.cc index 43c19ef9cddeb0..a7a27c13af471c 100644 --- a/src/node_url.cc +++ b/src/node_url.cc @@ -167,7 +167,7 @@ namespace url { } if (dots < 3 && ch != '.') goto end; - *piece_pointer = static_cast(*piece_pointer * 0x100 + value); //aitbenmouh possible loss data + *piece_pointer = static_cast(*piece_pointer * 0x100 + value); // possible loss data if (dots & 0x1) piece_pointer++; if (ch != kEOL) {