Skip to content

Commit ff87b5a

Browse files
committed
perf: trim per-construction work in RegisterInstance
- EncodeSignature formatted the argument count through a std::stringstream on every constructor and dynamic method resolution; it now appends std::to_string into a reserved buffer. - The instance's class is the global ref ResolveClass already returned from the class cache, so the second FindClass(fullClassName) lookup is gone. - JsArgConverter skips parsing the signature of a zero-argument constructor. - makeInstanceStrong skips storeClass, which takes two locks, when the previous registration stored the same class.
1 parent 3162af5 commit ff87b5a

4 files changed

Lines changed: 20 additions & 17 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ bool CallbackHandlers::RegisterInstance(Isolate *isolate, const Local<Object> &j
154154
success = !localInstance.IsNull();
155155

156156
if (success) {
157-
jclass instanceClass = env.FindClass(fullClassName);
158-
objectManager->SetJavaClass(jsObject, instanceClass);
157+
// ResolveClass returned the class cache's global ref for fullClassName.
158+
objectManager->SetJavaClass(jsObject, generatedJavaClass);
159159
} else {
160160
DEBUG_WRITE_FORCE("RegisterInstance failed with null new instance class: %s",
161161
fullClassName.c_str());

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ JsArgConverter::JsArgConverter(const v8::FunctionCallbackInfo<Value> &args,
8585
m_error(Error()) {
8686
m_argsLen = args.Length();
8787

88-
JniSignatureParser parser(methodSignature);
89-
m_ownedTokens = parser.Parse();
88+
if (m_argsLen > 0) {
89+
JniSignatureParser parser(methodSignature);
90+
m_ownedTokens = parser.Parse();
91+
}
9092

9193
for (int i = 0; i < m_argsLen; i++) {
9294
m_isValid = ConvertArg(args[i], i);

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include "NativeScriptException.h"
1414
#include "Runtime.h"
1515
#include "ObjectManager.h"
16-
#include <sstream>
1716

1817
using namespace v8;
1918

@@ -111,19 +110,14 @@ MethodCache::CacheMethodInfo MethodCache::ResolveConstructorSignature(const Args
111110

112111
// Encoded signature <className>.S/I.<methodName>.<argsCount>.<arg1class>.<...>
113112
string MethodCache::EncodeSignature(const string& className, const string& methodName, const FunctionCallbackInfo<Value>& args, bool isStatic) {
114-
string sig(className);
115-
sig.append(".");
116-
if (isStatic) {
117-
sig.append("S.");
118-
} else {
119-
sig.append("I.");
120-
}
113+
int len = args.Length();
114+
string sig;
115+
sig.reserve(className.size() + methodName.size() + 8 + len * 12);
116+
sig.append(className);
117+
sig.append(isStatic ? ".S." : ".I.");
121118
sig.append(methodName);
122119
sig.append(".");
123-
int len = args.Length();
124-
stringstream s;
125-
s << len;
126-
sig.append(s.str());
120+
sig.append(std::to_string(len));
127121

128122
for (int i = 0; i < len; i++) {
129123
sig.append(".");

‎test-app/runtime/src/main/java/com/tns/Runtime.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,8 @@ private static String getTypeMetadata(Class<?> clazz) {
962962
return ret;
963963
}
964964

965+
private Class<?> lastStoredClass;
966+
965967
@RuntimeCallable
966968
private void makeInstanceStrong(Object instance, int objectId) {
967969
if (instance == null) {
@@ -973,7 +975,12 @@ private void makeInstanceStrong(Object instance, int objectId) {
973975
strongJavaObjectToID.put(instance, key);
974976

975977
Class<?> clazz = instance.getClass();
976-
classStorageService.storeClass(clazz.getName(), clazz);
978+
// Instances of one class are usually registered back to back, and storeClass takes
979+
// two locks. A racing read only costs a redundant store.
980+
if (clazz != lastStoredClass) {
981+
classStorageService.storeClass(clazz.getName(), clazz);
982+
lastStoredClass = clazz;
983+
}
977984

978985
if (logger != null && logger.isEnabled()) {
979986
logger.write("MakeInstanceStrong (" + key + ", " + instance.getClass().toString() + ")");

0 commit comments

Comments
 (0)