0

I’ve build a cpp program that performs the following workflow sequentially:

Read serialized data (0.3 ms)
Receive search metadata (0.00.. ms)
Search data (0.01 ms)
Return search data(0.00.. ms)

Right now, I run the program with nodejs shell exec and serve it with an express api. The api is not used by many users, so reading the data will be performed just once a while. But one user will do like 20 queries on the same data. So right now, reading is performed 20 times on the same data.

Obviously reading the data takes most of the time. Additionally, the data that is being read never changes. To speed up the workflow, I want to read the data once, then wait for requests. Just like a MySQL db, that is waiting for statements.

What would be your approach to do that?

  1. Should I build a socket server? (Feels overkill)
  2. Should I try to run the Programm in background, store the pid and use a nodejs shell exec solution? (Feels hacky)

I think I’m missing the right terms. And it would be awesome if you can push me in the right direction!

James Z
  • 12,209
  • 10
  • 24
  • 44
schlumpel
  • 174
  • 1
  • 8
  • I believe node js has ability to interop with C++ or C interface. You may have two interface. `int Feed(const char* searchMetadata)` which returns a key. Then call `const char* search(int key, const char* searchingKeyword)` to search the meta data. Then call `search()` as many time as you want. It's also doable to cache the data as a file for C++, but reading back from file might impact your performance. – Louis Go May 11 '22 at 07:11
  • Is there any reason that prevents you from calling C/C++ api in nodejs? If none, executing shell seems like an overhead, so does socket server. – Louis Go May 11 '22 at 07:15
  • Right now I write search metadata to a file, the overhead is minimal. But still the Programm needs to read the serialized data all the time. I want to exclude read searchdata. Write meta -> read Searchdata -> read meta -> search -> return – schlumpel May 11 '22 at 09:34
  • If your bottleneck is reading serialize data. Try [flatbuffers](https://google.github.io/flatbuffers/) or [protobuf](https://developers.google.com/protocol-buffers). Then data could save/load in binary format with little overhead. – Louis Go May 12 '22 at 01:12

1 Answers1

0

You may call C++ directly from nodejs which should save you from overhead either executing the shell or building the socket server.

Eg: int Feed(const char* searchMetadata) which returns a key of data if you want multiple metadata being saved in C++part.

Then call const char* search(int dataKey, const char* searchingKeyword) to search the meta data. Then call search() as many times as you need.

It's also doable to cache the data as a file for C++. However you consider 0.3ms is an overhead, then it's better to avoid file open/save.

Louis Go
  • 2,213
  • 2
  • 16
  • 29
  • Thank you for this good solution! Though, it will force me into using nodejs (doing it right now anyway). And I didn’t mention, that I would prefer a solution without other dependencies, so I will accept your answer, if there are no more other „standalone“ ideas. Thank you! – schlumpel May 11 '22 at 09:01
  • @schlumpel I presume your caller is nodejs but it seems not. You may edit the question to clarify the scenario. – Louis Go May 12 '22 at 01:14