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

bindings/blst.{hpp,swg}: shift memory management to JVM GC. #54

Closed
wants to merge 4 commits into from

Conversation

dot-asm
Copy link
Collaborator

@dot-asm dot-asm commented Jan 29, 2021

So far Java interface was reliant on deprecated finalize() method to
free memory allocated by blst. Now we use Java long[] as opaque
storage for blst structures.

@Nashatyrev, @cemozerr, could you have a look at this? Not necessarily at C++ part, as main question is if auto-generated .java code is sensible. In other words, execute bindings/java/run.me and skim through newly created bindings/java/*.java. Please:-)

@dot-asm dot-asm force-pushed the java-memory-management branch 3 times, most recently from e04f148 to 445a70d Compare February 1, 2021 19:15
@Nashatyrev
Copy link

Hey @dot-asm !
That's totally cool! Especially taking into account that in Teku we may keep hundreds of thousands of Pubkeys in memory.

Java part looks good to me.

Some comments:

  • What's different with Pairing? Is it still left to be deleted manually (or with finalize())? Not a big deal though since these are kind of temporary objects and are deleted explicitly.
  • There public blstJNI.delete_* methods are still generated which do the following:
  arg1 = (blst::SecretKey *)JCALL(GetLongArrayElements, jarg1, 0);
  delete arg1;

which I think would definitely cause SIGSEGV. I see that they are not invoked anymore from other classes however it would be safer to remove them totally

  • (codestyle nit not directly related to this PR) there are methods like class P1 { P1 add(P1 a); } which modify and return this instance. From my perspective they could be erroneously interpreted as that a new instance is returned and this instance is left unmodified. Not sure about common C++ patterns, but Java dev would likely misinterpret this. Does it make sense to return void from these methods to avoid such misuse?

@dot-asm
Copy link
Collaborator Author

dot-asm commented Feb 4, 2021

  • What's different with Pairing? Is it still left to be deleted manually (or with finalize())? Not a big deal though since these are kind of temporary objects and are deleted explicitly.

No class has finalize or delete methods, including Pairing. You don't have to explicitly delete anything anymore. The only thing special about Pairing is that allocated size depends even on DST. But garbage collector doesn't care...

  • There public blstJNI.delete_* methods are still generated which do the following:
  arg1 = (blst::SecretKey *)JCALL(GetLongArrayElements, jarg1, 0);
  delete arg1;

which I think would definitely cause SIGSEGV. I see that they are not invoked anymore from other classes however it would be safer to remove them totally

Unfortunately I haven't found a way to suppress them [yet]. As you correctly point out, blstJNI.delete-s are not called from generated .java code, and application is not supposed to call blstJNI directly. This is considered tolerable [for now].

[I'll respond to 3rd bullet point separately.]

@Nashatyrev
Copy link

No class has finalize or delete methods, including Pairing

Oh right, sorry! Probably opened older Pairing.java version

So far Java interface was reliant on deprecated finalize() method to
free memory allocated by blst. Now we use Java long[] as opaque
storage for blst structures.
@dot-asm
Copy link
Collaborator Author

dot-asm commented Feb 5, 2021

Unfortunately I haven't found a way to suppress them [delete methods]

Fixed.

@dot-asm
Copy link
Collaborator Author

dot-asm commented Feb 6, 2021

It's committed now. Just in case, if you get "disaster" exception after git pull, it means that your ../libblst.a is stale. Delete it and re-run.me.

@dot-asm
Copy link
Collaborator Author

dot-asm commented Feb 8, 2021

[I'll respond to 3rd bullet point separately.]

I've created separate #57 to address this.

@dot-asm dot-asm closed this Feb 8, 2021
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