Java performance issue

I have a question related to java performance and method execution.

There is a lot of place in my application where I have to check some parameter, so I wrote a Validator class and put all the validation methods in it. Here's an example:

public class NumberValidator {
    public static short shortValidator(String s) throws ValidationException{
        try{
            short sh = Short.parseShort(s);

            if(sh < 1){
                throw new ValidationException();
            }
            return sh;
        }catch (Exception e) {
            throw new ValidationException("The parameter is wrong!");
        }
    }
...

      

But I think about it. This is normal? It's OO and modular, but - considering performance - is it a good idea? What if I had many calls at the same time? The snippet above is short and quick, but there are some methods that take longer.

What happens when there are many calls to a static or instance method in the same class and the method is out of sync? All callers should fall in line, and the JVM executes them sequentially?

Is it good to have some class that is identical to the above and randomly calls their identical methods? I think it is not, because "Don't repeat yourself" and "Duplicate evil" etc. But what about performance?

Thanks in advance.

+2


a source to share


5 answers


When reusing your method: if it's static, it has no state at all, so it's perfectly safe.



By performance: look at your use cases. Since you are validating strings, I can only assume that you are validating user input. In any case, the number of concurrent users on your system is insecure if there is a performance bottleneck.

+1


a source


Just two comments:

1) Factoring validation in methods can actually improve performance. As far as I know, the JIT compiler is designed to detect frequent method calls. Therefore, validation methods are good candidates for JIT optimization.



2) Try to avoid "catch (Exception e)". This is not recommended as you are catching all sorts of RuntimeExceptions as well. If you have an error in one of the non-trivial checks, you can throw a ValidationException that hides the error in your code.

+1


a source


Not sure what is bothering you. Since you mentioned that the method is not synchronized, I assume you have concurrent calls from multiple threads. And since the method is not synchronized, any call can be made concurrently without issue.

Of course, you won't get any performance improvement by copying and pasting this method into the calling classes. You may be degrading performance because your code will grow in size and waste space on CPU cache, but for such a short method, I find it effective.

0


a source


Are you having performance issues?

Make your code easy to maintain, and if it falls short of expectations, it will be easy to read and easy to refactor.

If you make your code optimized for speed with every choice, you will often end up with something unreadable and have to start from scratch to fix simple mistakes.

However, your method is static and will only need to be initialized once. This is the swift version. :-)

0


a source


I am suspicious of this code, not for performance metrics, as I don't think it successfully abstracts anything that deserves an abstraction.

If it is intended to validate user input, it replaces reasonable error messages such as "The maximum number of widgets allowed is 9999" with "ValidationException". And if you add something like arguments (or try / catch clauses) to get messages in context correctly, then almost certainly the calling site code required is more complex, harder to write and maintain than a simple way of doing things.

If it is for checking internal state, you can start to lose meaningful performance and certainly increase the complexity and buzz significantly if you pass arguments around strings all over the place and constantly parse and validate them.

0


a source







All Articles