Skip to content

Commit 68ae24c

Browse files
authored
Merge pull request #1234 from NativeScript/bektchiev/profiling-improvements
perf: Optimize metadata lookup by native name
2 parents 96d46c9 + dcd8428 commit 68ae24c

4 files changed

Lines changed: 21 additions & 13 deletions

File tree

src/NativeScript/Metadata/Metadata.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ struct Meta {
586586
}
587587

588588
const char* name() const {
589-
return (this->hasName()) ? this->_names.names->name.valuePtr() : this->jsName();
589+
return (this->hasName()) ? this->_names.names->name.valuePtr() : this->_names.name.valuePtr();
590590
}
591591

592592
/**

src/NativeScript/TypeFactory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class TypeFactory : public JSC::JSDestructibleObject {
5454

5555
const WTF::Vector<JSC::Strong<JSC::JSCell>> parseTypes(GlobalObject*, const Metadata::TypeEncoding*& typeEncodings, int count, bool isStructMember);
5656

57-
JSC::Strong<ObjCConstructorNative> getObjCNativeConstructorByNativeName(GlobalObject* globalObject, const WTF::String& klassName, const Metadata::ProtocolMetas& protocols);
57+
JSC::Strong<ObjCConstructorNative> getObjCNativeConstructorByNativeName(GlobalObject* globalObject, Class klass, const Metadata::ProtocolMetas& protocols);
5858
JSC::Strong<ObjCConstructorNative> getObjCNativeConstructorByJsName(GlobalObject* globalObject, const WTF::String& klassName, const Metadata::ProtocolMetas& protocols);
5959
JSC::Strong<ObjCConstructorNative> getObjCNativeConstructor(GlobalObject*, const Metadata::InterfaceMeta* metadata, const Metadata::ProtocolMetas& protocols);
6060

src/NativeScript/TypeFactory.mm

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,14 @@
241241
}
242242

243243
Strong<ObjCConstructorNative> TypeFactory::getObjCNativeConstructorByNativeName(GlobalObject* globalObject,
244-
const WTF::String& klassName,
244+
Class klass,
245245
const ProtocolMetas& protocols) {
246-
const InterfaceMeta* metadata = MetaFile::instance()->globalTableNativeInterfaces()->findInterfaceMeta(klassName.impl());
246+
247+
if (ObjCConstructorNative* type = this->_cacheId.get(ConstructorKey(klass, protocols))) {
248+
return Strong<ObjCConstructorNative>(globalObject->vm(), type);
249+
}
250+
251+
const InterfaceMeta* metadata = MetaFile::instance()->globalTableNativeInterfaces()->findInterfaceMeta(class_getName(klass));
247252
return this->getObjCNativeConstructor(globalObject, metadata, protocols);
248253
}
249254

@@ -284,12 +289,15 @@
284289
Strong<ObjCConstructorNative> TypeFactory::getObjCNativeConstructor(GlobalObject* globalObject, const ConstructorKey& constructorKey, const InterfaceMeta* metadata, const tns::instrumentation::Frame& frame) {
285290
assert(constructorKey.klasses.known);
286291

287-
// remove unnecessary protocols which the actual interface already conforms to
288292
ConstructorKey dedupedKey(constructorKey.klasses);
289-
auto protocolsSet = metadata->deepProtocolsSet();
290-
for (auto p : constructorKey.additionalProtocols) {
291-
if (protocolsSet.count(p) == 0) {
292-
dedupedKey.additionalProtocols.append(p);
293+
294+
// remove unnecessary protocols which the actual interface already conforms to
295+
if (constructorKey.additionalProtocols.size()) {
296+
auto protocolsSet = metadata->deepProtocolsSet();
297+
for (auto p : constructorKey.additionalProtocols) {
298+
if (protocolsSet.count(p) == 0) {
299+
dedupedKey.additionalProtocols.append(p);
300+
}
293301
}
294302
}
295303

@@ -322,7 +330,7 @@
322330
: class_getSuperclass(dedupedKey.klasses.known);
323331

324332
if (superClass) {
325-
parentConstructor = getObjCNativeConstructorByNativeName(globalObject, class_getName(superClass), ProtocolMetas()).get();
333+
parentConstructor = getObjCNativeConstructorByNativeName(globalObject, superClass, ProtocolMetas()).get();
326334
parentPrototype = parentConstructor.get(globalObject->globalExec(), vm.propertyNames->prototype);
327335

328336
/// If we have a super class which somehow references us, our constructor will already have been cached
@@ -359,7 +367,7 @@
359367
return Strong<ObjCConstructorNative>(globalObject->vm(), this->_nsObjectConstructor.get());
360368
}
361369

362-
auto constructor = getObjCNativeConstructorByNativeName(globalObject, "NSObject"_s, ProtocolMetas());
370+
auto constructor = getObjCNativeConstructorByNativeName(globalObject, [NSObject class], ProtocolMetas());
363371
this->_nsObjectConstructor.set(globalObject->vm(), this, constructor.get());
364372
return constructor;
365373
}
@@ -501,7 +509,7 @@
501509

502510
case BinaryTypeEncodingType::IdEncoding: {
503511
auto additionalProtocols = getProtocolMetas(typeEncoding->details.idDetails._protocols);
504-
result = getObjCNativeConstructorByNativeName(globalObject, "NSObject", additionalProtocols);
512+
result = getObjCNativeConstructorByNativeName(globalObject, [NSObject class], additionalProtocols);
505513
break;
506514
}
507515
case BinaryTypeEncodingType::ConstantArrayEncoding: {

0 commit comments

Comments
 (0)