views:

112

answers:

5

Hi,

I've got a question related to java performance and method execution.

In my app there are a lot of place where I have to validate some parameter, so I've written a Validator class and put all the validation methods into it. Here is 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'm thinking about that. Is this OK? It's OO and modularized, but - considering performance - is it a good idea? What if I had awful lot of invocation at the same time? The snippet above is short and fast, but there are some methods that take more time.

What happens when there are a lot of calling to a static method or an instance method in the same class and the method is not synchronized? All the calling methods have to fall in line and the JVM executes them sequentially?

Is it a good idea to have some class that are identical to the above-mentioned and randomly call their identical methods? I think it is not, because "Don't repeat yourself " and "Duplication is Evil" etc. But what about performance?

Thanks is advance.

A: 

Not sure what are your concern. Since you mentioned the method not beeing synchronized I suppose you have concurrent invocations from multiple threads. And since the method is not sychronized any invocation can be executed concurrently without problems.

For sure you won't get any performance improvements by copy and paste this method in the calling classes. May be you would reduce performance because your code size will increase and will waste space in che processor cache, but for such a short method I think it's a trascurable effect.

Andrea Polci
Yeah, you're right! I have have concurrent invocations from multiple threads. I'm not an expert and I don't really know how it works. I thought there is a method and even if it is not synchronized only one thread could execute it one time, because "there is one of it"
Colby77
@Colby77: nope, that's not how it works. Threads can execute the same code simultaneously just fine; the method parameters and local variables live on the stack, and each thread has its own stack.
Michael Borgwardt
Thank you Michael, I did not know that!
Colby77
+1  A: 

On reentrancy of your method: if it's static, it doesn't hold any state, so it's perfectly safe.

On performance: look at your use cases. Since you're validating Strings, I can only assume you are validating user input. In any case, the number of simultaneous users of your system is not probable to incur a performance bottleneck.

xtofl
May also be he is validating input from a file it has to import, or he has a high concurrent server application. Nevertheless he don't have any visible performance problem.
Andrea Polci
Actually I was inspired some time ago by Atwood's fall into the micro-optimization trap (http://www.codinghorror.com/blog/2010/03/compiled-or-bust.html)
xtofl
A: 

Are you experiencing performance problems?

Make the code easy to maintain first, and if it's not living up to expectations, it'll be easy to read and easy to refactor.

If you make the code optimized for speed on every choice, you'll often wind up with something unreadable, and have to start from scratch to fix simple bugs.

That said, your method is a static, and will only need to be initialized once. That is the fast version. :-)

Dean J
+1  A: 

Just two comments:

1) Factoring out validation into methods may in fact improve performance a little. As far as I know, the JIT compiler is designed to detect frequent method invocations. The validation methods are therefore good candidates for JIT optimization.

2) Try avoiding 'catch(Exception e)'. This is not recommended since you are capturing all kinds of RuntimeException as well. If you have a bug in one of the non-trivial validations, you may throw a ValidationException that hides a bug in the code.

Eyal Schneider
A: 

I'm suspicious of this code, not so much on performance grounds as that I don't think it is successfully abstracting out anything that deserves abstraction.

If it is for checking user input, it replaces reasonable error messages like 'maximum number of widgets allowed is 9999' with 'ValidationException'. And if you add something like arguments (or try/catch clauses) to get the messages right in context, then almost certainly the required call-site code is more complex, harder to write and maintain, than the straightforward way of doing things.

If it is for internal sanity checking, you may well start to lose meaningful performance, and certainly greatly increase complexity and bugginess, if you are passing arguments round as strings all over the place and continually parsing and validating them.

soru
I validate the data on the client side with javascript. So there is not any chance to send bad data considering correct uses. If someone sended bad data I didn't have to send him back what he done wrong, because he's a hacker or something. If I had to send back what the incorrect data was I could use a String param in the validation method. It would be just a little bit harder to call.
Colby77