Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvements for method calls #438

Merged
merged 2 commits into from
Dec 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/NativeScript/Calling/FFICall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ CallType FFICall::getCallData(JSCell*, CallData& callData) {
EncodedJSValue JSC_HOST_CALL FFICall::call(ExecState* execState) {
FFICall* callee = jsCast<FFICall*>(execState->callee());
Invocation invocation(callee);
ReleasePoolHolder releasePoolHolder;
ReleasePoolHolder releasePoolHolder(execState);

callee->preCall(execState, invocation);
callee->_invocationHooks.pre(callee, execState, invocation);
Expand All @@ -109,7 +109,7 @@ EncodedJSValue JSC_HOST_CALL FFICall::call(ExecState* execState) {

JSObject* FFICall::async(ExecState* execState, JSValue thisValue, const ArgList& arguments) {
__block std::unique_ptr<Invocation> invocation(new Invocation(this));
ReleasePoolHolder releasePoolHolder;
ReleasePoolHolder releasePoolHolder(execState);

Register* fakeCallFrame = new Register[JSStack::CallFrameHeaderSize + execState->argumentCount() + 1];
ExecState* fakeExecState = ExecState::create(fakeCallFrame);
Expand Down
8 changes: 8 additions & 0 deletions src/NativeScript/GlobalObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <JavaScriptCore/JSGlobalObject.h>
#include <objc/runtime.h>
#include <map>
#include <wtf/Deque.h>

namespace NativeScript {
class ObjCConstructorBase;
Expand All @@ -22,6 +23,7 @@ class TypeFactory;
class ObjCWrapperObject;
class GlobalObjectInspectorController;
class FFICallPrototype;
class ReleasePoolBase;

class GlobalObject : public JSC::JSGlobalObject {
public:
Expand Down Expand Up @@ -138,6 +140,10 @@ class GlobalObject : public JSC::JSGlobalObject {
this->_microtaskRunLoopMode = mode;
}

WTF::Deque<std::map<std::string, std::unique_ptr<ReleasePoolBase>>>& releasePools() {
return this->_releasePools;
}

#if defined(__LP64__) && __LP64__
JSC::WeakGCMap<const void*, ObjCWrapperObject>& taggedPointers() {
return this->_taggedPointers;
Expand Down Expand Up @@ -194,6 +200,8 @@ class GlobalObject : public JSC::JSGlobalObject {

std::map<const Protocol*, JSC::Strong<ObjCProtocolWrapper>> _objCProtocolWrappers;

WTF::Deque<std::map<std::string, std::unique_ptr<ReleasePoolBase>>> _releasePools;

#if defined(__LP64__) && __LP64__
// See comment in toValue
JSC::WeakGCMap<const void*, ObjCWrapperObject> _taggedPointers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ static void cStringType_write(ExecState* execState, const JSValue& value, void*
if (value.isString()) {
WTF::CString result = value.toString(execState)->value(execState).utf8();
*static_cast<const char**>(buffer) = result.data();
ReleasePool<WTF::CString>::releaseSoon(std::move(result));
releaseSoon(execState, std::move(result));
return;
}

Expand Down
3 changes: 0 additions & 3 deletions src/NativeScript/ObjC/Inheritance/ObjCClassBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@

#include <vector>

@protocol TNSDerivedClass
@end

namespace Metadata {
struct ProtocolMeta;
struct MethodMeta;
Expand Down
4 changes: 4 additions & 0 deletions src/NativeScript/ObjC/Inheritance/ObjCClassBuilder.mm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#include "Interop.h"
#include "TNSFastEnumerationAdapter.h"

@protocol TNSDerivedClass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to completely remove this protocol now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still used within the class builder and I can't figure out a substitution. It's not on a hot path there so I guess it's fine.


@end

namespace NativeScript {
using namespace JSC;
using namespace Metadata;
Expand Down
37 changes: 27 additions & 10 deletions src/NativeScript/ObjC/ObjCMethodCall.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include "ObjCMethodCall.h"
#include <objc/message.h>
#include "ObjCTypes.h"
#include "ObjCClassBuilder.h"
#include "ObjCPrototype.h"
#include "ObjCConstructorDerived.h"
#include "ObjCSuperObject.h"
#include "TypeFactory.h"
#include "Metadata.h"
#include "AllocatedPlaceholder.h"
Expand Down Expand Up @@ -70,33 +72,48 @@
this->setSelector(metadata->selector());
}

static bool isJavaScriptDerived(JSC::JSValue value) {
if (value.isCell()) {
JSCell* cell = value.asCell();
const Structure* structure = cell->structure();
const ClassInfo* info = structure->classInfo();
if (info == ObjCWrapperObject::info()) {
JSC::JSValue prototype = structure->storedPrototype();
return prototype.isCell() && prototype.asCell()->classInfo() != ObjCPrototype::info();
} else {
return info == ObjCConstructorDerived::info() || info == ObjCSuperObject::info();
}
}

return false;
}

void ObjCMethodCall::preInvocation(FFICall* callee, ExecState* execState, FFICall::Invocation& invocation) {
ObjCMethodCall* call = jsCast<ObjCMethodCall*>(callee);
id target = NativeScript::toObject(execState, execState->thisValue());
Class targetClass = object_getClass(target);

if (call->_hasErrorOutParameter && call->_parameterTypesCells.size() - 1 == execState->argumentCount()) {
std::vector<NSError*> outError = { nil };
invocation.setArgument(call->_argsCount - 1, outError.data());
ReleasePool<decltype(outError)>::releaseSoon(std::move(outError));
releaseSoon(execState, std::move(outError));
}

if (class_conformsToProtocol(targetClass, @protocol(TNSDerivedClass))) {
if (isJavaScriptDerived(execState->thisValue())) {
std::unique_ptr<objc_super> super = std::make_unique<objc_super>();
super->receiver = target;
super->super_class = class_getSuperclass(targetClass);
super->super_class = class_getSuperclass(object_getClass(target));
#ifdef DEBUG_OBJC_INVOCATION
bool isInstance = !class_isMetaClass(targetClass);
NSLog(@"> %@[%@(%@) %@]", isInstance ? @"-" : @"+", NSStringFromClass(targetClass), NSStringFromClass(super->super_class), NSStringFromSelector(this->selector()));
bool isInstance = !class_isMetaClass(object_getClass(target));
NSLog(@"> %@[%@(%@) %@]", isInstance ? @"-" : @"+", NSStringFromClass(object_getClass(target)), NSStringFromClass(super->super_class), NSStringFromSelector(this->selector()));
#endif
invocation.setArgument(0, super.get());
invocation.setArgument(1, call->_selector);
invocation.function = call->_msgSendSuper;
ReleasePool<decltype(super)>::releaseSoon(std::move(super));
releaseSoon(execState, std::move(super));
} else {
#ifdef DEBUG_OBJC_INVOCATION
bool isInstance = !class_isMetaClass(targetClass);
NSLog(@"> %@[%@ %@]", isInstance ? @"-" : @"+", NSStringFromClass(targetClass), NSStringFromSelector(this->selector()));
bool isInstance = !class_isMetaClass(object_getClass(target));
NSLog(@"> %@[%@ %@]", isInstance ? @"-" : @"+", NSStringFromClass(object_getClass(target)), NSStringFromSelector(this->selector()));
#endif
invocation.setArgument(0, target);
invocation.setArgument(1, call->_selector);
Expand Down
56 changes: 34 additions & 22 deletions src/NativeScript/Runtime/ReleasePool.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
#ifndef __NativeScript__ReleasePool__
#define __NativeScript__ReleasePool__

#include <type_traits>
#include <typeindex>
#include <vector>
#include <map>
#include <wtf/Deque.h>
#include <string>
#include <WTF/ThreadSpecific.h>

namespace NativeScript {
class ReleasePoolBase {
Expand All @@ -27,28 +24,24 @@ class ReleasePoolBase {
virtual ~ReleasePoolBase() = default;

protected:
friend class ReleasePoolHolder;

ReleasePoolBase() = default;

static WTF::Deque<Item>& releasePools() {
static auto pools = new WTF::ThreadSpecific<WTF::Deque<Item>>();
return **pools;
}
};

template <typename T>
class ReleasePool : ReleasePoolBase {
static_assert(std::is_destructible<T>::value, "Type must be destructible");
static_assert(!std::is_pointer<T>::value, "Type must not be a pointer");

public:
static void releaseSoon(T&& item) {
ASSERT(!releasePools().isEmpty());
private:
template <typename U>
friend void releaseSoon(GlobalObject*, U&&);

static void releaseSoon(GlobalObject* globalObject, T&& item) {
ASSERT(!globalObject->releasePools().isEmpty());

ReleasePool<T>* pool = nullptr;

Item& poolsMap = releasePools().last();
Item& poolsMap = globalObject->releasePools().last();
std::string key(__PRETTY_FUNCTION__);

auto iter = poolsMap.find(key);
Expand All @@ -66,20 +59,23 @@ class ReleasePool : ReleasePoolBase {
_items.clear();
}

private:
ReleasePool() = default;

std::vector<T> _items;
};

class ReleasePoolHolder {
public:
ReleasePoolHolder() {
ReleasePoolBase::releasePools().append(ReleasePoolBase::Item());
ReleasePoolHolder(JSC::ExecState* execState) {
init(JSC::jsCast<GlobalObject*>(execState->lexicalGlobalObject()));
}

ReleasePoolHolder(GlobalObject* globalObject) {
init(globalObject);
}

ReleasePoolBase::Item relinquish() {
auto& releasePools = ReleasePoolBase::releasePools();
auto& releasePools = _globalObject->releasePools();
ASSERT(!_didRelinquish);
ASSERT(!releasePools.isEmpty());

Expand All @@ -89,8 +85,8 @@ class ReleasePoolHolder {

void drain() {
if (LIKELY(!_didRelinquish)) {
ASSERT(!ReleasePoolBase::releasePools().isEmpty());
auto& poolsMap = ReleasePoolBase::releasePools().last();
ASSERT(!_globalObject->releasePools().isEmpty());
auto& poolsMap = _globalObject->releasePools().last();
for (auto& pair : poolsMap) {
pair.second->drain();
}
Expand All @@ -99,14 +95,30 @@ class ReleasePoolHolder {

~ReleasePoolHolder() {
if (LIKELY(!_didRelinquish)) {
ASSERT(!ReleasePoolBase::releasePools().isEmpty());
ReleasePoolBase::releasePools().removeLast();
ASSERT(!_globalObject->releasePools().isEmpty());
_globalObject->releasePools().removeLast();
}
}

private:
void init(GlobalObject* globalObject) {
_globalObject = globalObject;
_globalObject->releasePools().append(ReleasePoolBase::Item());
}

GlobalObject* _globalObject;
bool _didRelinquish;
};

template <typename T>
void releaseSoon(GlobalObject* globalObject, T&& item) {
ReleasePool<T>::releaseSoon(globalObject, std::move(item));
}

template <typename T>
void releaseSoon(JSC::ExecState* execState, T&& item) {
releaseSoon(JSC::jsCast<GlobalObject*>(execState->lexicalGlobalObject()), std::move(item));
}
}

#endif /* defined(__NativeScript__ReleasePool__) */