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

Use valueOf() to box primitive values instead of creating new objects every time #88

Merged
merged 7 commits into from
Sep 27, 2022

Conversation

rbasralian
Copy link
Contributor

No description provided.

@devinrsmith devinrsmith self-requested a review September 23, 2022 19:58
Copy link
Member

@devinrsmith devinrsmith left a comment

Choose a reason for hiding this comment

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

I like it. Few small things.

src/main/c/jpy_jtype.c Outdated Show resolved Hide resolved
Comment on lines 405 to 407
Py_BEGIN_ALLOW_THREADS; \
*JOBJECT_PTR = (*jenv)->CallStaticObjectMethod(jenv, TARGET_TYPE, STATIC_METHOD_ID, VALUE); \
Py_END_ALLOW_THREADS; \
Copy link
Member

Choose a reason for hiding this comment

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

It's not clear to me that we should / want to drop and then re-acquire the GIL (even thought the previous code to this was). In the other cases of CallStaticObjectMethod (and NewObject, etc) we are usually calling arbitrary pieces of code - at least in the context of this patch, we are only calling short valueOf methods.

@niloc132 @jmao-denver may have perspective as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any Java allocation could theoretically lead to a lengthy GC so worth keeping that in mind

Is there any performance testing around jpy/any idea what the cost of dropping/reacquiring the GIL is?

Copy link
Member

Choose a reason for hiding this comment

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

It's an interesting question - are allocation points also "safepoints"? I think "yes", but the real answer may be more nuanced than that.

That said, you are probably right and the way we do it now is probably the safest option.

} else {
return JType_PythonToJavaConversionError(type, pyArg);
}
return JType_CreateJavaObject(jenv, type, pyArg, JPy_Boolean_JClass, JPy_Boolean_Init_MID, value, objectRef);
JType_CallStaticObjectMethodPrimitiveArgAndReturn(JPy_Boolean_JClass, JPy_Boolean_ValueOf_SMID, value, objectRef);
Copy link
Member

Choose a reason for hiding this comment

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

Extra ; (also in other places) given the macro defines it as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can change it, but I figured better this way because it's nice to "pretend it's a regular function" and because this is how other similar macros are used (e.g. JPy_DELETE_LOCAL_REF)

src/main/c/jpy_jtype.c Outdated Show resolved Hide resolved
@devinrsmith devinrsmith self-assigned this Sep 23, 2022
Comment on lines 482 to 486
// always create new objects for long/int/short; any values assigned
// assigned to those types will be outside the default JVM cache
// range of [-128, 127].
if (i != j) {
value.j = j;
return JType_CreateJavaObject(jenv, type, pyArg, JPy_Long_JClass, JPy_Long_Init_MID, value, objectRef);
JType_CREATE_JAVA_OBJECT_1_AND_RETURN(JPy_Long_JClass, JPy_Long_Init_MID, j, objectRef);
Copy link
Member

Choose a reason for hiding this comment

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

So, it's not clear to me there will be any negligible performance difference if we keep doing valueOf. In fact, these constructors are deprecated. Not that I think they'll ever go away, but it might be worthwhile to stick w/ valueOf.

Copy link
Member

Choose a reason for hiding this comment

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

I also don't know if it makes a difference, but valueOf is HotSpotIntrinsicCandidate (I don't know if constructors are or can be?).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, thanks, good point. I changed it back. Doesn't make sense to use a deprecated code for performance reasons if there's no evidence it helps performance

@devinrsmith devinrsmith self-requested a review September 26, 2022 23:00
@devinrsmith devinrsmith merged commit 522bfad into jpy-consortium:master Sep 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants