| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent b05c56e commit 3505782
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -57,6 +57,7 @@ using v8::ArrayBufferView; | |||
| 57 | 57 | using v8::BackingStore; | |
| 58 | 58 | using v8::Context; | |
| 59 | 59 | using v8::EscapableHandleScope; | |
| 60 | + using v8::FastApiTypedArray; | ||
| 60 | 61 | using v8::FunctionCallbackInfo; | |
| 61 | 62 | using v8::Global; | |
| 62 | 63 | using v8::HandleScope; | |
@@ -1071,37 +1072,57 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) { | |||
| 1071 | 1072 | result == haystack_length ? -1 : static_cast<int>(result)); | |
| 1072 | 1073 | } | |
| 1073 | 1074 | ||
| 1074 | - void IndexOfNumber(const FunctionCallbackInfo<Value>& args) { | ||
| 1075 | + int32_t IndexOfNumber(const uint8_t* buffer_data, | ||
| 1076 | + size_t buffer_length, | ||
| 1077 | + uint32_t needle, | ||
| 1078 | + int64_t offset_i64, | ||
| 1079 | + bool is_forward) { | ||
| 1080 | + int64_t opt_offset = IndexOfOffset(buffer_length, offset_i64, 1, is_forward); | ||
| 1081 | + if (opt_offset <= -1 || buffer_length == 0) { | ||
| 1082 | + return -1; | ||
| 1083 | + } | ||
| 1084 | + size_t offset = static_cast<size_t>(opt_offset); | ||
| 1085 | + CHECK_LT(offset, buffer_length); | ||
| 1086 | + | ||
| 1087 | + const void* ptr; | ||
| 1088 | + if (is_forward) { | ||
| 1089 | + ptr = memchr(buffer_data + offset, needle, buffer_length - offset); | ||
| 1090 | + } else { | ||
| 1091 | + ptr = node::stringsearch::MemrchrFill(buffer_data, needle, offset + 1); | ||
| 1092 | + } | ||
| 1093 | + const uint8_t* ptr_uint8 = static_cast<const uint8_t*>(ptr); | ||
| 1094 | + return ptr != nullptr ? static_cast<int32_t>(ptr_uint8 - buffer_data) : -1; | ||
| 1095 | + } | ||
| 1096 | + | ||
| 1097 | + void SlowIndexOfNumber(const FunctionCallbackInfo<Value>& args) { | ||
| 1075 | 1098 | CHECK(args[1]->IsUint32()); | |
| 1076 | 1099 | CHECK(args[2]->IsNumber()); | |
| 1077 | 1100 | CHECK(args[3]->IsBoolean()); | |
| 1078 | 1101 | ||
| 1079 | 1102 | THROW_AND_RETURN_UNLESS_BUFFER(Environment::GetCurrent(args), args[0]); | |
| 1080 | - ArrayBufferViewContents<char> buffer(args[0]); | ||
| 1103 | + ArrayBufferViewContents<uint8_t> buffer(args[0]); | ||
| 1081 | 1104 | ||
| 1082 | 1105 | uint32_t needle = args[1].As<Uint32>()->Value(); | |
| 1083 | 1106 | int64_t offset_i64 = args[2].As<Integer>()->Value(); | |
| 1084 | 1107 | bool is_forward = args[3]->IsTrue(); | |
| 1085 | 1108 | ||
| 1086 | - int64_t opt_offset = | ||
| 1087 | - IndexOfOffset(buffer.length(), offset_i64, 1, is_forward); | ||
| 1088 | - if (opt_offset <= -1 || buffer.length() == 0) { | ||
| 1089 | - return args.GetReturnValue().Set(-1); | ||
| 1090 | - } | ||
| 1091 | - size_t offset = static_cast<size_t>(opt_offset); | ||
| 1092 | - CHECK_LT(offset, buffer.length()); | ||
| 1109 | + args.GetReturnValue().Set(IndexOfNumber( | ||
| 1110 | + buffer.data(), buffer.length(), needle, offset_i64, is_forward)); | ||
| 1111 | + } | ||
| 1093 | 1112 | ||
| 1094 | - const void* ptr; | ||
| 1095 | - if (is_forward) { | ||
| 1096 | - ptr = memchr(buffer.data() + offset, needle, buffer.length() - offset); | ||
| 1097 | - } else { | ||
| 1098 | - ptr = node::stringsearch::MemrchrFill(buffer.data(), needle, offset + 1); | ||
| 1099 | - } | ||
| 1100 | - const char* ptr_char = static_cast<const char*>(ptr); | ||
| 1101 | - args.GetReturnValue().Set(ptr ? static_cast<int>(ptr_char - buffer.data()) | ||
| 1102 | - : -1); | ||
| 1113 | + int32_t FastIndexOfNumber(v8::Local<v8::Value>, | ||
| 1114 | + const FastApiTypedArray<uint8_t>& buffer, | ||
| 1115 | + uint32_t needle, | ||
| 1116 | + int64_t offset_i64, | ||
| 1117 | + bool is_forward) { | ||
| 1118 | + uint8_t* buffer_data; | ||
| 1119 | + CHECK(buffer.getStorageIfAligned(&buffer_data)); | ||
| 1120 | + return IndexOfNumber( | ||
| 1121 | + buffer_data, buffer.length(), needle, offset_i64, is_forward); | ||
| 1103 | 1122 | } | |
| 1104 | 1123 | ||
| 1124 | + static v8::CFunction fast_index_of_number( | ||
| 1125 | + v8::CFunction::Make(FastIndexOfNumber)); | ||
| 1105 | 1126 | ||
| 1106 | 1127 | void Swap16(const FunctionCallbackInfo<Value>& args) { | |
| 1107 | 1128 | Environment* env = Environment::GetCurrent(args); | |
@@ -1413,7 +1434,11 @@ void Initialize(Local<Object> target, | |||
| 1413 | 1434 | SetMethodNoSideEffect(context, target, "compareOffset", CompareOffset); | |
| 1414 | 1435 | SetMethod(context, target, "fill", Fill); | |
| 1415 | 1436 | SetMethodNoSideEffect(context, target, "indexOfBuffer", IndexOfBuffer); | |
| 1416 | - SetMethodNoSideEffect(context, target, "indexOfNumber", IndexOfNumber); | ||
| 1437 | + SetFastMethodNoSideEffect(context, | ||
| 1438 | + target, | ||
| 1439 | + "indexOfNumber", | ||
| 1440 | + SlowIndexOfNumber, | ||
| 1441 | + &fast_index_of_number); | ||
| 1417 | 1442 | SetMethodNoSideEffect(context, target, "indexOfString", IndexOfString); | |
| 1418 | 1443 | ||
| 1419 | 1444 | SetMethod(context, target, "detachArrayBuffer", DetachArrayBuffer); | |
@@ -1472,7 +1497,9 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | |||
| 1472 | 1497 | registry->Register(CompareOffset); | |
| 1473 | 1498 | registry->Register(Fill); | |
| 1474 | 1499 | registry->Register(IndexOfBuffer); | |
| 1475 | - registry->Register(IndexOfNumber); | ||
| 1500 | + registry->Register(SlowIndexOfNumber); | ||
| 1501 | + registry->Register(FastIndexOfNumber); | ||
| 1502 | + registry->Register(fast_index_of_number.GetTypeInfo()); | ||
| 1476 | 1503 | registry->Register(IndexOfString); | |
| 1477 | 1504 | ||
| 1478 | 1505 | registry->Register(Swap16); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -27,6 +27,12 @@ using CFunctionCallbackWithStrings = | |||
| 27 | 27 | bool (*)(v8::Local<v8::Value>, | |
| 28 | 28 | const v8::FastOneByteString& input, | |
| 29 | 29 | const v8::FastOneByteString& base); | |
| 30 | + using CFunctionCallbackWithUint8ArrayUint32Int64Bool = | ||
| 31 | + int32_t (*)(v8::Local<v8::Value>, | ||
| 32 | + const v8::FastApiTypedArray<uint8_t>&, | ||
| 33 | + uint32_t, | ||
| 34 | + int64_t, | ||
| 35 | + bool); | ||
| 30 | 36 | using CFunctionWithUint32 = uint32_t (*)(v8::Local<v8::Value>, | |
| 31 | 37 | const uint32_t input); | |
| 32 | 38 | using CFunctionWithDoubleReturnDouble = double (*)(v8::Local<v8::Value>, | |
@@ -51,6 +57,7 @@ class ExternalReferenceRegistry { | |||
| 51 | 57 | V(CFunctionCallbackWithBool) \ | |
| 52 | 58 | V(CFunctionCallbackWithString) \ | |
| 53 | 59 | V(CFunctionCallbackWithStrings) \ | |
| 60 | + V(CFunctionCallbackWithUint8ArrayUint32Int64Bool) \ | ||
| 54 | 61 | V(CFunctionWithUint32) \ | |
| 55 | 62 | V(CFunctionWithDoubleReturnDouble) \ | |
| 56 | 63 | V(CFunctionWithInt64Fallback) \ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments