副问题[/!--empirenews.page--]
媒介
Javascript中的数组和数组工具一向都是编程职员优化的首要方针,一样平常来说,数组只会包括一些根基范例数据,好比说32位整数或字符等等。因此,每个引擎城市对这些工具举办某些优化,并晋升差异元素范例的会见速率和麋集型暗示。

在JavaScriptCore中,JavaScript引擎是在WebKit中实现的,个中每一个存储在工具中的元素都代表着一个IndexingType值,一个8位整数代表一套Flag组合,详细的参数界说可以在IndexingType.h中找到。接下来,引擎会检测一个工具中indexing的范例,然后抉择行使哪一条快速路径,个中最重要的一种indexing范例就是ArrayWithUndecided,它暗示的是全部元素均为未界说(undefined),并且没有存储任何现实的值。在这种环境下,引擎为了晋升机能,会让这些元素保持未初始化。
说明
下面,我们一路看一看旧版本中实现Array.prototype.concat的代码(ArrayPrototype.cpp):
- EncodedJSValueJSC_HOST_CALL arrayProtoPrivateFuncConcatMemcpy(ExecState* exec)
- {
- ...
- unsigned resultSize =checkedResultSize.unsafeGet();
- IndexingType firstType =firstArray->indexingType();
- IndexingType secondType =secondArray->indexingType();
- IndexingType type =firstArray->mergeIndexingTypeForCopying(secondType); // [[ 1 ]]
- if (type == NonArray ||!firstArray->canFastCopy(vm, secondArray) || resultSize >=MIN_SPARSE_ARRAY_INDEX) {
- ...
- }
- JSGlobalObject* lexicalGlobalObject =exec->lexicalGlobalObject();
- Structure* resultStructure =lexicalGlobalObject->arrayStructureForIndexingTypeDuringAllocation(type);
- if(UNLIKELY(hasAnyArrayStorage(resultStructure->indexingType())))
- return JSValue::encode(jsNull());
- ASSERT(!lexicalGlobalObject->isHavingABadTime());
- ObjectInitializationScopeinitializationScope(vm);
- JSArray* result =JSArray::tryCreateUninitializedRestricted(initializationScope, resultStructure,resultSize);
- if (UNLIKELY(!result)) {
- throwOutOfMemoryError(exec, scope);
- return encodedJSValue();
- }
- if (type == ArrayWithDouble) {
- [[ 2 ]]
- double* buffer =result->butterfly()->contiguousDouble().data();
- memcpy(buffer,firstButterfly->contiguousDouble().data(), sizeof(JSValue) *firstArraySize);
- memcpy(buffer + firstArraySize,secondButterfly->contiguousDouble().data(), sizeof(JSValue) *secondArraySize);
- } else if (type != ArrayWithUndecided) {
- ...
这个函数首要用来判定功效数组[[1]]的indexing范例,我们可以看到,假如indexing范例为ArrayWithDouble,它将会选择[[2]]作为快速路径。接下来,我们看一看:
mergeIndexingTypeForCopying的实当代码,这个函数首要认真在Array.prototype.concat被挪用时,判定功效数组的indexing范例:
- inlineIndexingType JSArray::mergeIndexingTypeForCopying(IndexingType other)
- {
- IndexingType type = indexingType();
- if (!(type & IsArray && other& IsArray))
- return NonArray;
- if (hasAnyArrayStorage(type) ||hasAnyArrayStorage(other))
- return NonArray;
- if (type == ArrayWithUndecided)
- return other; [[ 3 ]]
- ...
(编辑:湖南网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|