| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent fad1793 commit dfdc062
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1089,37 +1089,57 @@ void IndexOfBuffer(const FunctionCallbackInfo<Value>& args) { | |||
| 1089 | 1089 | result == haystack_length ? -1 : static_cast<int>(result)); | |
| 1090 | 1090 | } | |
| 1091 | 1091 | ||
| 1092 | - void IndexOfNumber(const FunctionCallbackInfo<Value>& args) { | ||
| 1092 | + int32_t IndexOfNumber(const uint8_t* buffer_data, | ||
| 1093 | + size_t buffer_length, | ||
| 1094 | + uint32_t needle, | ||
| 1095 | + int64_t offset_i64, | ||
| 1096 | + bool is_forward) { | ||
| 1097 | + int64_t opt_offset = IndexOfOffset(buffer_length, offset_i64, 1, is_forward); | ||
| 1098 | + if (opt_offset <= -1 || buffer_length == 0) { | ||
| 1099 | + return -1; | ||
| 1100 | + } | ||
| 1101 | + size_t offset = static_cast<size_t>(opt_offset); | ||
| 1102 | + CHECK_LT(offset, buffer_length); | ||
| 1103 | + | ||
| 1104 | + const void* ptr; | ||
| 1105 | + if (is_forward) { | ||
| 1106 | + ptr = memchr(buffer_data + offset, needle, buffer_length - offset); | ||
| 1107 | + } else { | ||
| 1108 | + ptr = node::stringsearch::MemrchrFill(buffer_data, needle, offset + 1); | ||
| 1109 | + } | ||
| 1110 | + const uint8_t* ptr_uint8 = static_cast<const uint8_t*>(ptr); | ||
| 1111 | + return ptr != nullptr ? static_cast<int32_t>(ptr_uint8 - buffer_data) : -1; | ||
| 1112 | + } | ||
| 1113 | + | ||
| 1114 | + void SlowIndexOfNumber(const FunctionCallbackInfo<Value>& args) { | ||
| 1093 | 1115 | CHECK(args[1]->IsUint32()); | |
| 1094 | 1116 | CHECK(args[2]->IsNumber()); | |
| 1095 | 1117 | CHECK(args[3]->IsBoolean()); | |
| 1096 | 1118 | ||
| 1097 | 1119 | THROW_AND_RETURN_UNLESS_BUFFER(Environment::GetCurrent(args), args[0]); | |
| 1098 | - ArrayBufferViewContents<char> buffer(args[0]); | ||
| 1120 | + ArrayBufferViewContents<uint8_t> buffer(args[0]); | ||
| 1099 | 1121 | ||
| 1100 | 1122 | uint32_t needle = args[1].As<Uint32>()->Value(); | |
| 1101 | 1123 | int64_t offset_i64 = args[2].As<Integer>()->Value(); | |
| 1102 | 1124 | bool is_forward = args[3]->IsTrue(); | |
| 1103 | 1125 | ||
| 1104 | - int64_t opt_offset = | ||
| 1105 | - IndexOfOffset(buffer.length(), offset_i64, 1, is_forward); | ||
| 1106 | - if (opt_offset <= -1 || buffer.length() == 0) { | ||
| 1107 | - return args.GetReturnValue().Set(-1); | ||
| 1108 | - } | ||
| 1109 | - size_t offset = static_cast<size_t>(opt_offset); | ||
| 1110 | - CHECK_LT(offset, buffer.length()); | ||
| 1126 | + args.GetReturnValue().Set(IndexOfNumber( | ||
| 1127 | + buffer.data(), buffer.length(), needle, offset_i64, is_forward)); | ||
| 1128 | + } | ||
| 1111 | 1129 | ||
| 1112 | - const void* ptr; | ||
| 1113 | - if (is_forward) { | ||
| 1114 | - ptr = memchr(buffer.data() + offset, needle, buffer.length() - offset); | ||
| 1115 | - } else { | ||
| 1116 | - ptr = node::stringsearch::MemrchrFill(buffer.data(), needle, offset + 1); | ||
| 1117 | - } | ||
| 1118 | - const char* ptr_char = static_cast<const char*>(ptr); | ||
| 1119 | - args.GetReturnValue().Set(ptr ? static_cast<int>(ptr_char - buffer.data()) | ||
| 1120 | - : -1); | ||
| 1130 | + int32_t FastIndexOfNumber(v8::Local<v8::Value>, | ||
| 1131 | + const FastApiTypedArray<uint8_t>& buffer, | ||
| 1132 | + uint32_t needle, | ||
| 1133 | + int64_t offset_i64, | ||
| 1134 | + bool is_forward) { | ||
| 1135 | + uint8_t* buffer_data; | ||
| 1136 | + CHECK(buffer.getStorageIfAligned(&buffer_data)); | ||
| 1137 | + return IndexOfNumber( | ||
| 1138 | + buffer_data, buffer.length(), needle, offset_i64, is_forward); | ||
| 1121 | 1139 | } | |
| 1122 | 1140 | ||
| 1141 | + static v8::CFunction fast_index_of_number( | ||
| 1142 | + v8::CFunction::Make(FastIndexOfNumber)); | ||
| 1123 | 1143 | ||
| 1124 | 1144 | void Swap16(const FunctionCallbackInfo<Value>& args) { | |
| 1125 | 1145 | Environment* env = Environment::GetCurrent(args); | |
@@ -1431,7 +1451,11 @@ void Initialize(Local<Object> target, | |||
| 1431 | 1451 | SetMethodNoSideEffect(context, target, "compareOffset", CompareOffset); | |
| 1432 | 1452 | SetMethod(context, target, "fill", Fill); | |
| 1433 | 1453 | SetMethodNoSideEffect(context, target, "indexOfBuffer", IndexOfBuffer); | |
| 1434 | - SetMethodNoSideEffect(context, target, "indexOfNumber", IndexOfNumber); | ||
| 1454 | + SetFastMethodNoSideEffect(context, | ||
| 1455 | + target, | ||
| 1456 | + "indexOfNumber", | ||
| 1457 | + SlowIndexOfNumber, | ||
| 1458 | + &fast_index_of_number); | ||
| 1435 | 1459 | SetMethodNoSideEffect(context, target, "indexOfString", IndexOfString); | |
| 1436 | 1460 | ||
| 1437 | 1461 | SetMethod(context, target, "detachArrayBuffer", DetachArrayBuffer); | |
@@ -1492,7 +1516,9 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | |||
| 1492 | 1516 | registry->Register(CompareOffset); | |
| 1493 | 1517 | registry->Register(Fill); | |
| 1494 | 1518 | registry->Register(IndexOfBuffer); | |
| 1495 | - registry->Register(IndexOfNumber); | ||
| 1519 | + registry->Register(SlowIndexOfNumber); | ||
| 1520 | + registry->Register(FastIndexOfNumber); | ||
| 1521 | + registry->Register(fast_index_of_number.GetTypeInfo()); | ||
| 1496 | 1522 | registry->Register(IndexOfString); | |
| 1497 | 1523 | ||
| 1498 | 1524 | registry->Register(Swap16); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,6 +41,12 @@ using CFunctionCallbackWithTwoUint8ArraysFallback = | |||
| 41 | 41 | const v8::FastApiTypedArray<uint8_t>&, | |
| 42 | 42 | const v8::FastApiTypedArray<uint8_t>&, | |
| 43 | 43 | v8::FastApiCallbackOptions&); | |
| 44 | + using CFunctionCallbackWithUint8ArrayUint32Int64Bool = | ||
| 45 | + int32_t (*)(v8::Local<v8::Value>, | ||
| 46 | + const v8::FastApiTypedArray<uint8_t>&, | ||
| 47 | + uint32_t, | ||
| 48 | + int64_t, | ||
| 49 | + bool); | ||
| 44 | 50 | using CFunctionWithUint32 = uint32_t (*)(v8::Local<v8::Value>, | |
| 45 | 51 | const uint32_t input); | |
| 46 | 52 | using CFunctionWithDoubleReturnDouble = double (*)(v8::Local<v8::Value>, | |
@@ -68,6 +74,7 @@ class ExternalReferenceRegistry { | |||
| 68 | 74 | V(CFunctionCallbackWithStrings) \ | |
| 69 | 75 | V(CFunctionCallbackWithTwoUint8Arrays) \ | |
| 70 | 76 | V(CFunctionCallbackWithTwoUint8ArraysFallback) \ | |
| 77 | + V(CFunctionCallbackWithUint8ArrayUint32Int64Bool) \ | ||
| 71 | 78 | V(CFunctionWithUint32) \ | |
| 72 | 79 | V(CFunctionWithDoubleReturnDouble) \ | |
| 73 | 80 | V(CFunctionWithInt64Fallback) \ | |
| Back | FazBrowse Home | New Git URL |
0 commit comments