Skip to content

Commit 49332f3

Browse files
committed
perf(runtime): cheaper JS -> Java return values and array element access
- A JS value returned to Java went through a JsArgToArrayConverter that heap-allocated a one-slot array and a vector entry, deleted a null global ref, and copied the converted local ref only to delete the original. The single-value converter now converts into an inline slot, and ConvertJsValueToJavaObject takes ownership of the converted local ref (a borrowed ref is still copied into a new local). - CreateJSInstanceNative read the instance's class name back through GetObjectClass + Class.getName even though Java passes that same name. - Java array element access copied the element signature and compared it against up to nine strings per access; it now switches on the element type. Object elements go through ObjectManager::GetOrCreateJSWrapper like method results and field reads do, instead of GetOrCreateObjectId + GetClassName + a node lookup under the global node mutex.
1 parent 2f82403 commit 49332f3

5 files changed

Lines changed: 136 additions & 134 deletions

File tree

‎test-app/runtime/src/main/cpp/ArrayElementAccessor.cpp‎

Lines changed: 77 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ using namespace v8;
99
using namespace std;
1010
using namespace tns;
1111

12+
namespace {
13+
// The element type of a primitive array's signature ("[I" -> 'I'), else '\0'.
14+
char PrimitiveElementType(const string& arraySignature) {
15+
return arraySignature.size() == 2 ? arraySignature[1] : '\0';
16+
}
17+
}
18+
1219
Local<Value> ArrayElementAccessor::GetArrayElement(Isolate* isolate, Local<Context> context, const Local<Object>& array, uint32_t index, const string& arraySignature) {
1320
JEnv env;
1421

@@ -24,52 +31,67 @@ Local<Value> ArrayElementAccessor::GetArrayElement(Isolate* isolate, Local<Conte
2431
jsize startIndex = index;
2532
const jsize length = 1;
2633

27-
const string elementSignature = arraySignature.substr(1);
28-
29-
if (elementSignature == "Z") {
30-
jbooleanArray boolArr = static_cast<jbooleanArray>(arr);
31-
jboolean boolArrValue;
32-
env.GetBooleanArrayRegion(boolArr, startIndex, length, &boolArrValue);
33-
value = ConvertToJsValue(isolate, objectManager, env, elementSignature, &boolArrValue);
34-
} else if (elementSignature == "B") {
35-
jbyteArray byteArr = static_cast<jbyteArray>(arr);
36-
jbyte byteArrValue;
37-
env.GetByteArrayRegion(byteArr, startIndex, length, &byteArrValue);
38-
value = ConvertToJsValue(isolate, objectManager, env, elementSignature, &byteArrValue);
39-
} else if (elementSignature == "C") {
40-
jcharArray charArr = static_cast<jcharArray>(arr);
41-
jchar charArrValue;
42-
env.GetCharArrayRegion(charArr, startIndex, length, &charArrValue);
43-
value = ConvertToJsValue(isolate, objectManager, env, elementSignature, &charArrValue);
44-
} else if (elementSignature == "S") {
45-
jshortArray shortArr = static_cast<jshortArray>(arr);
46-
jshort shortArrValue;
47-
env.GetShortArrayRegion(shortArr, startIndex, length, &shortArrValue);
48-
value = ConvertToJsValue(isolate, objectManager, env, elementSignature, &shortArrValue);
49-
} else if (elementSignature == "I") {
50-
jintArray intArr = static_cast<jintArray>(arr);
51-
jint intArrValue;
52-
env.GetIntArrayRegion(intArr, startIndex, length, &intArrValue);
53-
value = ConvertToJsValue(isolate, objectManager, env, elementSignature, &intArrValue);
54-
} else if (elementSignature == "J") {
55-
jlongArray longArr = static_cast<jlongArray>(arr);
56-
jlong longArrValue;
57-
env.GetLongArrayRegion(longArr, startIndex, length, &longArrValue);
58-
value = ConvertToJsValue(isolate, objectManager, env, elementSignature, &longArrValue);
59-
} else if (elementSignature == "F") {
60-
jfloatArray floatArr = static_cast<jfloatArray>(arr);
61-
jfloat floatArrValue;
62-
env.GetFloatArrayRegion(floatArr, startIndex, length, &floatArrValue);
63-
value = ConvertToJsValue(isolate, objectManager, env, elementSignature, &floatArrValue);
64-
} else if (elementSignature == "D") {
65-
jdoubleArray doubleArr = static_cast<jdoubleArray>(arr);
66-
jdouble doubleArrValue;
67-
env.GetDoubleArrayRegion(doubleArr, startIndex, length, &doubleArrValue);
68-
value = ConvertToJsValue(isolate, objectManager, env, elementSignature, &doubleArrValue);
69-
} else {
70-
jobject result = env.GetObjectArrayElement(static_cast<jobjectArray>(arr), index);
71-
value = ConvertToJsValue(isolate, objectManager, env, elementSignature, &result);
72-
env.DeleteLocalRef(result);
34+
switch (PrimitiveElementType(arraySignature)) {
35+
case 'Z': {
36+
jboolean boolArrValue;
37+
env.GetBooleanArrayRegion(static_cast<jbooleanArray>(arr), startIndex, length, &boolArrValue);
38+
value = Boolean::New(isolate, boolArrValue);
39+
break;
40+
}
41+
case 'B': {
42+
jbyte byteArrValue;
43+
env.GetByteArrayRegion(static_cast<jbyteArray>(arr), startIndex, length, &byteArrValue);
44+
value = Integer::New(isolate, byteArrValue);
45+
break;
46+
}
47+
case 'C': {
48+
jchar charArrValue;
49+
env.GetCharArrayRegion(static_cast<jcharArray>(arr), startIndex, length, &charArrValue);
50+
value = ArgConverter::jcharToV8String(isolate, charArrValue);
51+
break;
52+
}
53+
case 'S': {
54+
jshort shortArrValue;
55+
env.GetShortArrayRegion(static_cast<jshortArray>(arr), startIndex, length, &shortArrValue);
56+
value = Integer::New(isolate, shortArrValue);
57+
break;
58+
}
59+
case 'I': {
60+
jint intArrValue;
61+
env.GetIntArrayRegion(static_cast<jintArray>(arr), startIndex, length, &intArrValue);
62+
value = Integer::New(isolate, intArrValue);
63+
break;
64+
}
65+
case 'J': {
66+
jlong longArrValue;
67+
env.GetLongArrayRegion(static_cast<jlongArray>(arr), startIndex, length, &longArrValue);
68+
value = ArgConverter::ConvertFromJavaLong(isolate, longArrValue);
69+
break;
70+
}
71+
case 'F': {
72+
jfloat floatArrValue;
73+
env.GetFloatArrayRegion(static_cast<jfloatArray>(arr), startIndex, length, &floatArrValue);
74+
value = Number::New(isolate, floatArrValue);
75+
break;
76+
}
77+
case 'D': {
78+
jdouble doubleArrValue;
79+
env.GetDoubleArrayRegion(static_cast<jdoubleArray>(arr), startIndex, length, &doubleArrValue);
80+
value = Number::New(isolate, doubleArrValue);
81+
break;
82+
}
83+
default: {
84+
jobject result = env.GetObjectArrayElement(static_cast<jobjectArray>(arr), index);
85+
if (result == nullptr) {
86+
value = Null(isolate);
87+
} else if (arraySignature.compare(1, string::npos, "Ljava/lang/String;") == 0) {
88+
value = ArgConverter::jstringToV8String(isolate, static_cast<jstring>(result));
89+
} else {
90+
value = objectManager->GetOrCreateJSWrapper(result);
91+
}
92+
env.DeleteLocalRef(result);
93+
break;
94+
}
7395
}
7496

7597
return handleScope.Escape(value);
@@ -86,29 +108,29 @@ void ArrayElementAccessor::SetArrayElement(Isolate* isolate, Local<Context> cont
86108

87109
assertNonNullNativeArray(arr);
88110

89-
const string elementSignature = arraySignature.substr(1);
111+
const char elementType = PrimitiveElementType(arraySignature);
90112

91-
if (elementSignature == "Z") { //bool
113+
if (elementType == 'Z') { //bool
92114
jboolean boolElementValue = (jboolean) value->BooleanValue(isolate);
93115
jbooleanArray boolArr = static_cast<jbooleanArray>(arr);
94116
env.SetBooleanArrayRegion(boolArr, index, 1, &boolElementValue);
95-
} else if (elementSignature == "B") { //byte
117+
} else if (elementType == 'B') { //byte
96118
jbyte byteElementValue = (jbyte) value->Int32Value(context).ToChecked();
97119
jbyteArray byteArr = static_cast<jbyteArray>(arr);
98120
env.SetByteArrayRegion(byteArr, index, 1, &byteElementValue);
99-
} else if (elementSignature == "C") { //char
121+
} else if (elementType == 'C') { //char
100122
jchar charElementValue = ArgConverter::ToJavaChar(isolate, value->ToString(context).ToLocalChecked());
101123
jcharArray charArr = static_cast<jcharArray>(arr);
102124
env.SetCharArrayRegion(charArr, index, 1, &charElementValue);
103-
} else if (elementSignature == "S") { //short
125+
} else if (elementType == 'S') { //short
104126
jshort shortElementValue = (jshort) value->Int32Value(context).ToChecked();
105127
jshortArray shortArr = static_cast<jshortArray>(arr);
106128
env.SetShortArrayRegion(shortArr, index, 1, &shortElementValue);
107-
} else if (elementSignature == "I") { //int
129+
} else if (elementType == 'I') { //int
108130
jint intElementValue = value->Int32Value(context).ToChecked();
109131
jintArray intArr = static_cast<jintArray>(arr);
110132
env.SetIntArrayRegion(intArr, index, 1, &intElementValue);
111-
} else if (elementSignature == "J") { //long
133+
} else if (elementType == 'J') { //long
112134
jlong longElementValue;
113135
if (value->IsObject()) {
114136
longElementValue = (jlong) ArgConverter::ConvertToJavaLong(isolate, value);
@@ -117,11 +139,11 @@ void ArrayElementAccessor::SetArrayElement(Isolate* isolate, Local<Context> cont
117139
}
118140
jlongArray longArr = static_cast<jlongArray>(arr);
119141
env.SetLongArrayRegion(longArr, index, 1, &longElementValue);
120-
} else if (elementSignature == "F") { //float
142+
} else if (elementType == 'F') { //float
121143
jfloat floatElementValue = (jfloat) value->NumberValue(context).ToChecked();
122144
jfloatArray floatArr = static_cast<jfloatArray>(arr);
123145
env.SetFloatArrayRegion(floatArr, index, 1, &floatElementValue);
124-
} else if (elementSignature == "D") { //double
146+
} else if (elementType == 'D') { //double
125147
jdouble doubleElementValue = (jdouble) value->NumberValue(context).ToChecked();
126148
jdoubleArray doubleArr = static_cast<jdoubleArray>(arr);
127149
env.SetDoubleArrayRegion(doubleArr, index, 1, &doubleElementValue);
@@ -145,51 +167,6 @@ void ArrayElementAccessor::SetArrayElement(Isolate* isolate, Local<Context> cont
145167
}
146168
}
147169

148-
Local<Value> ArrayElementAccessor::ConvertToJsValue(Isolate* isolate, ObjectManager* objectManager, JEnv& env, const string& elementSignature, const void* value) {
149-
Local<Value> jsValue;
150-
151-
if (elementSignature == "Z") {
152-
jsValue = Boolean::New(isolate, *(jboolean*) value);
153-
} else if (elementSignature == "B") {
154-
jsValue = Integer::New(isolate, *(jbyte*) value);
155-
} else if (elementSignature == "C") {
156-
jsValue = ArgConverter::jcharToV8String(isolate, *(jchar*) value);
157-
} else if (elementSignature == "S") {
158-
jsValue = Integer::New(isolate, *(jshort*) value);
159-
} else if (elementSignature == "I") {
160-
jsValue = Integer::New(isolate, *(jint*) value);
161-
} else if (elementSignature == "J") {
162-
jsValue = ArgConverter::ConvertFromJavaLong(isolate, *(jlong*) value);
163-
} else if (elementSignature == "F") {
164-
jsValue = Number::New(isolate, *(jfloat*) value);
165-
} else if (elementSignature == "D") {
166-
jsValue = Number::New(isolate, *(jdouble*) value);
167-
} else {
168-
if (nullptr != (*(jobject*) value)) {
169-
bool isString = elementSignature == "Ljava/lang/String;";
170-
171-
if (isString) {
172-
jsValue = ArgConverter::jstringToV8String(isolate, *(jstring*) value);
173-
} else {
174-
jint javaObjectID = objectManager->GetOrCreateObjectId(*(jobject*) value);
175-
jsValue = objectManager->GetJsObjectByJavaObject(javaObjectID);
176-
177-
if (jsValue.IsEmpty()) {
178-
// Named after the element's runtime class, as a method result is: the
179-
// wrapper's name must be a JNI class name, and it is later read back as
180-
// the array signature of the element itself.
181-
auto className = objectManager->GetClassName(*(jobject*) value);
182-
jsValue = objectManager->CreateJSWrapper(javaObjectID, className);
183-
}
184-
}
185-
} else {
186-
jsValue = Null(isolate);
187-
}
188-
}
189-
190-
return jsValue;
191-
}
192-
193170
void ArrayElementAccessor::assertNonNullNativeArray(tns::JniLocalRef& arrayReference) {
194171
if(arrayReference.IsNull()){
195172
throw NativeScriptException("Failed calling indexer operator on native array. The JavaScript instance no longer has available Java instance counterpart.");

‎test-app/runtime/src/main/cpp/ArrayElementAccessor.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class ArrayElementAccessor {
1414
void SetArrayElement(v8::Isolate* isolate, v8::Local<v8::Context> context, const v8::Local<v8::Object>& array, uint32_t index, const std::string& arraySignature, v8::Local<v8::Value>& value);
1515

1616
private:
17-
v8::Local<v8::Value> ConvertToJsValue(v8::Isolate* isolate, ObjectManager* objectManager, JEnv& env, const std::string& elementSignature, const void* value);
1817
void assertNonNullNativeArray(tns::JniLocalRef& arrayReference);
1918
};
2019
}

‎test-app/runtime/src/main/cpp/JsArgToArrayConverter.cpp‎

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ using namespace tns;
2020
JsArgToArrayConverter::JsArgToArrayConverter(Isolate *isolate, Local<Context> context,
2121
const v8::Local<Value> &arg,
2222
bool isImplementationObject, int classReturnType)
23-
: m_isolate(isolate), m_arr(nullptr), m_argsAsObject(nullptr), m_argsLen(0), m_isValid(false), m_error(Error()),
23+
: m_isolate(isolate), m_arr(nullptr), m_args(nullptr), m_argsLen(0), m_isValid(false), m_error(Error()),
2424
m_return_type(classReturnType) {
2525
if (!isImplementationObject) {
2626
m_argsLen = 1;
27-
m_argsAsObject = new jobject[m_argsLen];
28-
memset(m_argsAsObject, 0, m_argsLen * sizeof(jobject));
27+
m_args = &m_inlineArg;
2928

3029
m_isValid = ConvertArg(context, arg, 0);
3130
}
@@ -36,15 +35,14 @@ JsArgToArrayConverter::JsArgToArrayConverter(Isolate *isolate, Local<Context> co
3635
*/
3736
JsArgToArrayConverter::JsArgToArrayConverter(const v8::FunctionCallbackInfo<Value> &args,
3837
bool hasImplementationObject)
39-
: m_isolate(args.GetIsolate()), m_arr(nullptr), m_argsAsObject(nullptr), m_argsLen(0), m_isValid(false), m_error(Error()),
38+
: m_isolate(args.GetIsolate()), m_arr(nullptr), m_args(nullptr), m_argsLen(0), m_isValid(false), m_error(Error()),
4039
m_return_type(static_cast<int>(Type::Null)) {
4140
m_argsLen = !hasImplementationObject ? args.Length() : args.Length() - 2;
4241

4342
bool success = true;
4443

4544
if (m_argsLen > 0) {
46-
m_argsAsObject = new jobject[m_argsLen];
47-
memset(m_argsAsObject, 0, m_argsLen * sizeof(jobject));
45+
m_args = new ConvertedArg[m_argsLen]();
4846

4947
auto context = m_isolate->GetCurrentContext();
5048
for (int i = 0; i < m_argsLen; i++) {
@@ -316,14 +314,29 @@ bool JsArgToArrayConverter::ConvertArg(Local<Context> context, const Local<Value
316314
}
317315

318316
jobject JsArgToArrayConverter::GetConvertedArg() {
319-
return (m_argsLen > 0) ? m_argsAsObject[0] : nullptr;
317+
return (m_argsLen > 0) ? m_args[0].obj : nullptr;
320318
}
321319

322-
void JsArgToArrayConverter::SetConvertedObject(JEnv &env, int index, jobject obj, bool isGlobal) {
323-
m_argsAsObject[index] = obj;
324-
if ((obj != nullptr) && !isGlobal) {
325-
m_storedIndexes.push_back(index);
320+
jobject JsArgToArrayConverter::ReleaseConvertedArg() {
321+
if (m_argsLen <= 0 || m_args[0].obj == nullptr) {
322+
return nullptr;
323+
}
324+
325+
auto &arg = m_args[0];
326+
if (arg.ownsLocalRef) {
327+
arg.ownsLocalRef = false;
328+
auto obj = arg.obj;
329+
arg.obj = nullptr;
330+
return obj;
326331
}
332+
333+
JEnv env;
334+
return env.NewLocalRef(arg.obj);
335+
}
336+
337+
void JsArgToArrayConverter::SetConvertedObject(JEnv &env, int index, jobject obj, bool isGlobal) {
338+
m_args[index].obj = obj;
339+
m_args[index].ownsLocalRef = (obj != nullptr) && !isGlobal;
327340
}
328341

329342
int JsArgToArrayConverter::Length() const {
@@ -359,7 +372,7 @@ jobjectArray JsArgToArrayConverter::ToJavaArray() {
359372
m_arr = (jobjectArray) env.NewGlobalRef(tmpArr);
360373

361374
for (int i = 0; i < m_argsLen; i++) {
362-
env.SetObjectArrayElement(m_arr, i, m_argsAsObject[i]);
375+
env.SetObjectArrayElement(m_arr, i, m_args[i].obj);
363376
}
364377
}
365378

@@ -370,15 +383,19 @@ JsArgToArrayConverter::~JsArgToArrayConverter() {
370383
if (m_argsLen > 0) {
371384
JEnv env;
372385

373-
env.DeleteGlobalRef(m_arr);
386+
if (m_arr != nullptr) {
387+
env.DeleteGlobalRef(m_arr);
388+
}
374389

375-
int length = m_storedIndexes.size();
376-
for (int i = 0; i < length; i++) {
377-
int index = m_storedIndexes[i];
378-
env.DeleteLocalRef(m_argsAsObject[index]);
390+
for (int i = 0; i < m_argsLen; i++) {
391+
if (m_args[i].ownsLocalRef) {
392+
env.DeleteLocalRef(m_args[i].obj);
393+
}
379394
}
380395

381-
delete[] m_argsAsObject;
396+
if (m_args != &m_inlineArg) {
397+
delete[] m_args;
398+
}
382399
}
383400
}
384401

‎test-app/runtime/src/main/cpp/JsArgToArrayConverter.h‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@ class JsArgToArrayConverter {
1414

1515
JsArgToArrayConverter(v8::Isolate* isolate, v8::Local<v8::Context> context, const v8::Local<v8::Value>& arg, bool isImplementationObject, int classReturnType);
1616

17+
JsArgToArrayConverter(const JsArgToArrayConverter&) = delete;
18+
19+
JsArgToArrayConverter& operator=(const JsArgToArrayConverter&) = delete;
20+
1721
~JsArgToArrayConverter();
1822

1923
jobjectArray ToJavaArray();
2024

2125
jobject GetConvertedArg();
2226

27+
// The converted value as a local reference the caller deletes, or null. The
28+
// converter no longer refers to it afterwards.
29+
jobject ReleaseConvertedArg();
30+
2331
int Length() const;
2432

2533
bool IsValid() const;
@@ -52,9 +60,15 @@ class JsArgToArrayConverter {
5260

5361
Error m_error;
5462

55-
std::vector<int> m_storedIndexes;
63+
struct ConvertedArg {
64+
jobject obj = nullptr;
65+
bool ownsLocalRef = false;
66+
};
67+
68+
// The single-value constructor converts into m_inlineArg.
69+
ConvertedArg m_inlineArg;
5670

57-
jobject* m_argsAsObject;
71+
ConvertedArg* m_args;
5872

5973
jobjectArray m_arr;
6074

0 commit comments

Comments
 (0)