Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java/Android - Fast ByteBuffer Parsing

i'm writing a webserver for mobile android based devices in java.

This webserver is single-threaded and follow the idea behind nginx, node.js and similar: don't spawn multiple threads just use async operations in an event loop.

While using a multi-threaded webserver may give better performance on x86 recent cpus, on arm based single core cpu will need to do a lot of more job.

To clarify, i know quite well C and i've implemented single threaded webservers in plain c or multithreaded one in C#, taking advantage of IOPS on windows, but i've wrote only a simple webserver in java, the one i want to replace with this new one.

Right now, i'm using java nio and i've readed that ByteBuffer are quite slow when converted to string but this isn't a problem because i don't need to do, infact to gaix maximium performances i wanna to implement parsing and comparing at byte level.

My question is, which method for parsing byte buffer is faster?

I've seen that ByteBuffer supports get method, that give access to a single byte and move ahead the cursor, supports array method, that return the backing array, so my question is which method is faster?

I can work directly on backed array, or i should avoid and use get?

I wanna to implement a ByteBufferPool to reuse bytebuffer, i'll make thread-aware it, read below, can be this an issue?

In some cases i'll compare byte to byte, appling a mask to handle case sensitivity (i mean, if the first byte is G, the third is T and fourth is a space (0x47, 0x54 and 0x20) i can treath the request as a GET one) and in other cases i'll need to compare strings with byte array, like for headers (i'll loop through string chars, cast them to bytes and compare to bytes).

Sorry for these silly questions, but i don't know java specs and don't know internal java stuff, so i need infos :)

Someone can give an hint? :)

PS: obiviously, not all operation can be handled in a do-stuff-pause-continue-return manner, so i'll implement a ThreadPool to avoid thread creation penalty

like image 981
Daniele Salvatore Albano Avatar asked Oct 28 '11 17:10

Daniele Salvatore Albano


1 Answers

Give Netty a try. You can control the threading model and you can implement just what you need.

like image 137
ApriOri Avatar answered Oct 29 '22 11:10

ApriOri