Skip to content

Add caching for function and method ffi_cifs #795

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

Merged
merged 3 commits into from
Oct 5, 2017
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
2 changes: 1 addition & 1 deletion examples/Gameraww/app/MasterViewController.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ var JSMasterViewController = UITableViewController.extend(
"aboutPressed:" :
{returns : interop.types.void, params : [ UIControl ]}
}
});
});
2 changes: 2 additions & 0 deletions src/NativeScript/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
set(HEADER_FILES
Calling/FFICache.h
Calling/FFICall.h
Calling/FFICallback.h
Calling/FFICallbackInlines.h
Expand Down Expand Up @@ -102,6 +103,7 @@ set(HEADER_FILES
)

set(SOURCE_FILES
Calling/FFICache.cpp
Calling/FFICall.cpp
Calling/FFICallPrototype.cpp
Calling/FFIFunctionCall.mm
Expand Down
23 changes: 23 additions & 0 deletions src/NativeScript/Calling/FFICache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// FFICache.cpp
// NativeScript
//
// Created by Teodor Dermendzhiev on 02/10/2017.
// Copyright (c) 2014 г. Telerik. All rights reserved.
//

#include "FFICache.h"
#include <stdio.h>

namespace NativeScript {

FFICache* FFICache::global() {

static FFICache* instance;

if (!instance)
instance = new FFICache;
return instance;
}

} // namespace NativeScript
40 changes: 40 additions & 0 deletions src/NativeScript/Calling/FFICache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// FFICache.h
// NativeScript
//
// Created by Teodor Dermendzhiev on 02/10/2017.
//
//

#ifndef __NativeScript__FFICache__
#define __NativeScript__FFICache__

#include "FFIType.h"
#include <vector>

namespace NativeScript {

struct SignatureHash {

std::size_t operator()(std::vector<const ffi_type*> signature) const {
std::size_t seed = 2166136261;

for (size_t i = 0; i < signature.size(); i++) {
seed = (seed ^ reinterpret_cast<size_t>(signature[i])) * 16777619U;
}
return seed;
}
};

class FFICache {

public:
typedef std::unordered_map<std::vector<const ffi_type*>, std::shared_ptr<ffi_cif>, SignatureHash> FFIMap;
FFIMap cifCache;

static FFICache* global();
};

} // namespace NativeScript

#endif /* FFICache_h */
44 changes: 34 additions & 10 deletions src/NativeScript/Calling/FFICall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#include "FFICall.h"
#include "FFICache.h"
#include <JavaScriptCore/Interpreter.h>
#include <JavaScriptCore/JSPromiseDeferred.h>
#include <JavaScriptCore/StrongInlines.h>
Expand All @@ -18,9 +19,12 @@ using namespace JSC;

const ClassInfo FFICall::s_info = { "FFICall", &Base::s_info, 0, CREATE_METHOD_TABLE(FFICall) };

void FFICall::initializeFFI(VM& vm, const InvocationHooks& hooks, JSCell* returnType, const Vector<JSCell*>& parameterTypes, size_t initialArgumentIndex) {
ASSERT(this->methodTable()->destroy != FFICall::destroy);
void deleteCif(ffi_cif* cif) {
delete[] cif->arg_types;
delete cif;
}

void FFICall::initializeFFI(VM& vm, const InvocationHooks& hooks, JSCell* returnType, const Vector<JSCell*>& parameterTypes, size_t initialArgumentIndex) {
this->_invocationHooks = hooks;

this->_initialArgumentIndex = initialArgumentIndex;
Expand All @@ -33,8 +37,11 @@ void FFICall::initializeFFI(VM& vm, const InvocationHooks& hooks, JSCell* return

const ffi_type** parameterTypesFFITypes = new const ffi_type*[parametersCount + initialArgumentIndex];

this->signatureVector.push_back(getFFITypeMethodTable(returnType).ffiType);

for (size_t i = 0; i < initialArgumentIndex; ++i) {
parameterTypesFFITypes[i] = &ffi_type_pointer;
this->signatureVector.push_back(&ffi_type_pointer);
}

for (size_t i = 0; i < parametersCount; i++) {
Expand All @@ -45,10 +52,10 @@ void FFICall::initializeFFI(VM& vm, const InvocationHooks& hooks, JSCell* return
this->_parameterTypes.append(ffiTypeMethodTable);

parameterTypesFFITypes[i + initialArgumentIndex] = ffiTypeMethodTable.ffiType;
this->signatureVector.push_back(parameterTypesFFITypes[i + initialArgumentIndex]);
}

this->_cif = new ffi_cif;
ffi_prep_cif(this->_cif, FFI_DEFAULT_ABI, parametersCount + initialArgumentIndex, const_cast<ffi_type*>(this->_returnType.ffiType), const_cast<ffi_type**>(parameterTypesFFITypes));
this->_cif = getCif(parametersCount + initialArgumentIndex, const_cast<ffi_type*>(this->_returnType.ffiType), const_cast<ffi_type**>(parameterTypesFFITypes));

this->_argsCount = _cif->nargs;
this->_stackSize = 0;
Expand All @@ -57,17 +64,34 @@ void FFICall::initializeFFI(VM& vm, const InvocationHooks& hooks, JSCell* return
this->_stackSize += malloc_good_size(sizeof(void * [this->_cif->nargs]));

this->_returnOffset = this->_stackSize;
this->_stackSize += malloc_good_size(std::max(this->_cif->rtype->size, sizeof(ffi_arg)));
this->_stackSize += malloc_good_size(std::max(this->_cif.get()->rtype->size, sizeof(ffi_arg)));

for (size_t i = 0; i < this->_argsCount; i++) {
this->_argValueOffsets.push_back(this->_stackSize);
this->_stackSize += malloc_good_size(std::max(this->_cif->arg_types[i]->size, sizeof(ffi_arg)));
}
}

std::shared_ptr<ffi_cif> FFICall::getCif(unsigned int nargs, ffi_type* rtype, ffi_type** atypes) {

FFICache::FFIMap::const_iterator it = FFICache::global()->cifCache.find(this->signatureVector);

if (it == FFICache::global()->cifCache.end()) {
std::shared_ptr<ffi_cif> shared(new ffi_cif, deleteCif);
ffi_prep_cif(shared.get(), FFI_DEFAULT_ABI, nargs, rtype, atypes);
FFICache::global()->cifCache[this->signatureVector] = shared;
}

return FFICache::global()->cifCache[this->signatureVector];
}

FFICall::~FFICall() {
delete[] this->_cif->arg_types;
delete this->_cif;

if (this->_cif.use_count() == 2) {
FFICache::FFIMap::const_iterator it;
it = FFICache::global()->cifCache.find(this->signatureVector);
FFICache::global()->cifCache.erase(it);
}
}

void FFICall::visitChildren(JSCell* cell, SlotVisitor& visitor) {
Expand Down Expand Up @@ -99,7 +123,7 @@ EncodedJSValue JSC_HOST_CALL FFICall::call(ExecState* execState) {

{
JSLock::DropAllLocks locksDropper(execState);
ffi_call(callee->_cif, FFI_FN(invocation.function), invocation._buffer + callee->_returnOffset, reinterpret_cast<void**>(invocation._buffer + callee->_argsArrayOffset));
ffi_call(callee->_cif.get(), FFI_FN(invocation.function), invocation._buffer + callee->_returnOffset, reinterpret_cast<void**>(invocation._buffer + callee->_argsArrayOffset));
}

JSValue result = callee->_returnType.read(execState, invocation._buffer + callee->_returnOffset, callee->_returnTypeCell.get());
Expand Down Expand Up @@ -150,7 +174,7 @@ JSObject* FFICall::async(ExecState* execState, JSValue thisValue, const ArgList&
JSC::VM& vm = fakeExecState->vm();
auto scope = DECLARE_CATCH_SCOPE(vm);

ffi_call(callee->_cif, FFI_FN(invocation->function), invocation->resultBuffer(), reinterpret_cast<void**>(invocation->_buffer + callee->_argsArrayOffset));
ffi_call(callee->_cif.get(), FFI_FN(invocation->function), invocation->resultBuffer(), reinterpret_cast<void**>(invocation->_buffer + callee->_argsArrayOffset));

JSLockHolder lockHolder(fakeExecState);
// we no longer have a valid caller on the stack, what with being async and all
Expand Down Expand Up @@ -189,4 +213,4 @@ JSObject* FFICall::async(ExecState* execState, JSValue thisValue, const ArgList&

return deferred->promise();
}
}
} // namespace NativeScript
7 changes: 5 additions & 2 deletions src/NativeScript/Calling/FFICall.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class FFICall : public JSC::InternalFunction {
}

JSC::JSObject* async(JSC::ExecState*, JSC::JSValue thisValue, const JSC::ArgList&);
std::shared_ptr<ffi_cif> getCif(unsigned int nargs, ffi_type* rtype, ffi_type** atypes);

std::vector<const ffi_type*> signatureVector;

protected:
FFICall(JSC::VM& vm, JSC::Structure* structure)
Expand All @@ -34,7 +37,7 @@ class FFICall : public JSC::InternalFunction {

~FFICall();

ffi_cif* _cif;
std::shared_ptr<ffi_cif> _cif;

class Invocation {
WTF_MAKE_NONCOPYABLE(Invocation)
Expand Down Expand Up @@ -145,6 +148,6 @@ class FFICall : public JSC::InternalFunction {
size_t _argsArrayOffset;
std::vector<size_t> _argValueOffsets;
};
}
} // namespace NativeScript

#endif /* defined(__NativeScript__FFICall__) */
2 changes: 1 addition & 1 deletion src/NativeScript/Calling/FFIFunctionCall.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
[invocation.getResult<id>() release];
}
}
}
}
2 changes: 1 addition & 1 deletion src/NativeScript/ObjC/Constructor/ObjCConstructorCall.mm
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@

return true;
}
}
}