Java / Android - Fast ByteBuffer Analysis

I am writing a web server for Android mobile devices in java.

This web server is single-threaded and follows the idea of ​​nginx, node.js, etc .: don't create multiple threads, just use asynchronous operations in the event loop.

While using a multi-threaded web server may give better performance with the recent x86 processor, a lot of work will be required on a single core core processor.

To clarify, I know well that C and I implemented single-threaded web servers in simple c or multi-threaded in C # using IOPS for windows, but I wrote only a simple web server in java, one I want to replace it with a new one.

Right now, I am using java nio, and I read that ByteBuffer is pretty slow when converted to string, but that is not a problem because I do not need to do infact to gaix maximium performance, I want to parse and compare byte level .

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

I saw that ByteBuffer supports a get method that gives access to one byte and moves forward along the cursor, supports an array method that returns a support array, so my question is, which method is faster?

Can I work directly with the array, or should I avoid and use get?

I want to implement ByteBufferPool to reuse bytebuffer, I will talk about it by reading below, maybe this is a problem?

In some cases, I compare byte with byte, using a mask to handle case sensitivity (I mean, if the first byte is G, the third is T, and the fourth is a space (0x47, 0x54 and 0x20), I can process the request as GET), and in other cases I will need to compare strings with a byte array, for example, for headers (I will iterate over string characters, pass them into bytes and compare with bytes).

Sorry for these stupid questions, but I don’t know the java specifications and I don’t know the internal java files, so I need some information :)

Can someone give a hint? :)

PS: in truth, not all operations can be processed in do-stuff-pause-continue-return mode, so I will implement ThreadPool to avoid a fine for creating threads

+6
source share
1 answer

Give Netty a try. You can control the flow model, and you can only implement what you need.

+1
source

Source: https://habr.com/ru/post/900310/


All Articles