I get FIX message string (ASCII) as ByteBuffer. I parse tag value pairs and store values as primitive objects in the treemap with tag as key. So I need to convert byte[] value to int/double/date, etc. depending on its type.
Simplest way is to create new String and pass it to standard converter functions. e.g.
int convertToInt(byte[] buffer, int offset, int length)
{
String valueStr = new String(buffer, offset, length);
return Integer.parseInt(valueStr);
}
I understand that in Java, creating new object is very inexpensive, still is there any way to convert this ascii byte[] to primitive type directly. I tried hand written functions to do so, but found it to be time consuming and didn’t result in better performance.
Are there any third party libraries for doing so and most of all is it worth doing?
Almost certainly not – and you should measure to check that this is a performance bottleneck before going to significant effort to alleviate it.
What’s your performance like now? What does it need to be? (“As fast as possible” isn’t a good goal, or you’ll never stop – work out when you can say you’re “done”.)
Profile the code – is the problem really in string creation? Check how often you’re garbage-collecting etc (again, with a profiler).
Each parsing type is likely to have different characteristics. For example, for parsing integers, if you find that for a significant amount of the time you’ve got a single digit, you might want to special-case that:
… but check how often this occurs before you go down that path. Applying lots of checks for particular optimizations that never actually crop up is counter-productive.