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

Reduce the code differences between C and Java versions #148

Closed
wants to merge 3 commits into from

Conversation

kno10
Copy link

@kno10 kno10 commented Aug 19, 2019

This patch reduces some trivial differences between the C and Java versions:

  • add some comments missing in the Java version
  • align some variable names to match the C version
  • import static Math.* to be able to use "exp" instead of "Math.exp"
  • import static libsvm.svm_params.* to get the constants such as C_SVC as in C
  • use NULL instead of null in m4

this should make it easier in the future to keep the C and Java versions in sync.

@cjlin1
Copy link
Owner

cjlin1 commented Aug 19, 2019 via email

@ZYQue
Copy link
Contributor

ZYQue commented Aug 27, 2019

Hello,
Thank you for your commits. Most of your modification looks nice, but we still have some suggestions and wish you can help to change:

  1. svm.java is an automatically generated file, so we have no intention to modify it.
  2. For the added comment "invalid parameter" in file svm.m4, we think it will be better to use "Unreachable" (same as another place).
  3. In file svm.m4, it's ok to follow java style and keep using "null" and "boolean".

Thanks again.

@kno10
Copy link
Author

kno10 commented Aug 27, 2019

This version of svm.java was generated from svm.m4; but since it is committed to the repository, I also committed the regenerated version.

I chose to use "NULL" and "bool" macros to minimize the delta between the C and the Java version, as this makes it easier to merge any changes from C to Java, even if of course "null" and "NULL" is a trivial delta, this still means you have to compare fewer lines.

Of course your are free to cherry pick only the changes that you like. I've broken the commits into three parts - (A) sync, (B) use static imports, (C) use NULL and bool macros. Maybe you only want to pick the first two commits, minus the derived files.

@cjlin1
Copy link
Owner

cjlin1 commented Sep 10, 2019

Hello,

We just released a new version of libsvm, in which your
first commit was incorporated.

We couldn't directly sign off your commit because of
some minor problems (one or two changes in your commit 1
indeed should be in commit 2). But in our commit message we did
clearly indicate your contribution.

We decided not to take the commit 2, in which packages
are imported. The reason why we used Math.abs rather
than imported Math package is to avoid naming conflicts. In C we cannot do
this but in java we can. Thus we used specific names since the
beginning. We also need backward compatibility. The change
may cause some existing java code using libsvm to have problems.
Thus in the end we decided not to take it.

Thank you again for the help and contribution.

@kno10
Copy link
Author

kno10 commented Sep 10, 2019

Hello,
It is perfectly fine with me to adapt the commit.

Wrt. the second commit:
Imports are local to the current class only (that is why you always have to import all the stuff again), and used only at compile time.
So the import static java.lang.Math.* I proposed should yield exactly the same byte code as the original version: there is no "import" in the bytecode; either is expanded by the compiler to the fully qualified java.lang.Math.abs in the bytecode, and must - by the Java language specification and design - not have an effect on outside classes. Encapsulation is very strong in Java.

I have not verified if the class files produced are exactly the same; but they should, unless I again have some smaller mix-up in splitting the commits.

@cjlin1
Copy link
Owner

cjlin1 commented Sep 10, 2019 via email

@cjlin1
Copy link
Owner

cjlin1 commented Nov 2, 2020

We finally checked this issue again
(with the help of Bill Tran)
From
https://docs.oracle.com/javase/tutorial/java/package/usepkgs.html
they mentioned
"Use static import very sparingly. ... readers of the code won't know which class defines a particular static object."
So in the end we decide to be conservative and explicitly specify
package+function.

Thank you again for the pull request.

@cjlin1 cjlin1 closed this Nov 2, 2020
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.

None yet

3 participants