2

I currently have a c++ program receiving requests from a web application via a socket and generating in return a Json file (150 Ko) that will be read by this application.

  1. I wondered if it would not be faster to provide the data directly via the socket ?
  2. if so what would be the fastest format to send it (Json, Xml, personnal format that I would parse myself,... ) ?
  3. should I send the whole package (150 Ko) at once or should I stream it through the socket ?
Arcyno
  • 4,153
  • 3
  • 34
  • 52
  • 'fastest' means 'transferring it quickly' or 'processing it quickly' ? You might experiment with basic compression algorithms and time your responses. – Marco A. May 28 '15 at 13:55
  • fastest would be transfering and processing it quickly... But the more I think about that, I would say that the processing time is actually why I really want to reduce – Arcyno May 28 '15 at 13:57
  • You should have a look at Googles Protobuf. – Rambo Ramon May 28 '15 at 13:59
  • didn't know avout Protobuf.. it seems really nice ! But it would only help me reduce the righting time of my data, but not the reading (as it is not compatible with php) – Arcyno May 28 '15 at 14:01
  • What is `150 Ko`, 150 kilobytes or kilo-octets or...? – Morten Jensen May 28 '15 at 14:03
  • well is quite the same, but I meant `150 Kb` – Arcyno May 28 '15 at 14:13

1 Answers1

2

If I understand what you have written correctly, the web server is forwarding requests to your C++ program, and you want a PHP application to read the data by making a web request to the web server, which in turn forwards the request to the C++ program for serving the response.

Here are three ideas for improving the speed of this setup:

  1. Consider using a streaming parser to parse the data read from the socket. This will allow your PHP application to begin consuming and processing the data before all of the data is read from the socket.

    Many JSON libraries do not offer streaming parsing APIs (see Is there a streaming API for JSON?). For example, PHP's built-in JSON APIs do not. But, see Incremental JSON parsing in php.

    PHP has a built-in XML pull parser, XMLReader. If your C++ program generated XML data, you could use an XMLReader in PHP to parse the XML incrementally, meaning that your PHP application would not need to wait until the data was fully downloaded.

  2. If your C++ program is reading JSON from a file and sending the file's contents over the socket, consider using zero-copy I/O. See also vmsplice().

  3. If the C++ program and your PHP application are running on the same server, you could use a shared memory segment. This would eliminate the need for transferring the data over a socket, because the C++ program and PHP application would both have access to a segment of memory. The C++ program would write all of the data into the shared memory segment, and then the PHP application would read the data.

    See PHP's Semaphore functions for more information.

Community
  • 1
  • 1
Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
  • In my case the third option seems perfect !.. I'll try to learn about how to do that... I don't see how I can tell the c++ program where is the shared memory allocated by my php app – Arcyno May 29 '15 at 09:15
  • @Arcyno: I would definitely try one and two first to see if that improves the speed enough for your application. 150 KiB is not a lot of data, so it would seem that your application is not primarily I/O bound. For idea three, you would need to modify your C++ to effectively call PHP's shm_put_var(). Also, note that there is still some overhead to PHP's built-in shared memory routines in the form of marshalling/unmarshalling the data. Your best option may be to write a custom extension for PHP to expose the data in shared memory more directly. – Daniel Trebbien May 29 '15 at 14:06