📜 ⬆️ ⬇️

Well, yes, C ++ sites

I missed a couple of topics about the web in C ++ and many comrades misunderstand the meaning of using C ++ on the web. Therefore, I will tell you how I used it in my experience. I used, however, Python, but only because the WebToolKit type library was not at hand.

To be honest, I understand C ++ like a hacker in women's lipstick - like many, I mostly use PHP (and more and more Python, but rarely for the web - more for OpenGL), but nevertheless it does not bother me (C ++ ) use. This is to the fact that do not be afraid of the complexity of the language - everything is not as bad as it seems.

But what is important: "Premature is the root of all evil." And this is important. To write some complicated system right on C ++ is pointless (I’m talking about sites, of course). Optimize, as a rule, you need only 3% of the code. That's about these 3 percent, and I will tell.
')
I had a website and on it on all pages users could write something (to explain for a long time) and in a corner there was a “stream” in real time (via AJAX update every 5 seconds) who wrote where and what. It was done, as it should be on PHP / MySQL - a table, in it we select the last (relatively speaking: WHERE id> MAX (id) -10 ORDER BY id DESC) records. Periodically delete everything except the last ten. It worked great. While TechCrunch about the site did not write. Dead server because of this part. As soon as I turned it off (remember about 3%?) - all tens of thousands of people that they sent me - they began to see the site perfectly. I decided this sloppy - made memcache. PHP chose the last 10 entries and wrote in memcache, and when queried it, it gave a line from memcache. However, this decision as a result began to die.

I wrote a simple http-server in Python (from the standard library), which had an array of 10 lines and gave it to an AJAX request or added a new one and deleted the first (FIFO).

That is, PHP (on the server processed forms and sent it via file_get_contents ('http://mysite.com:9000/?put='.urlencode ($ _GET [msg])); and AJAX (in the sense of a JavaScript client) has stepped on mysite.com : 9000 /? get - bypassing all Apaches, PHP interpreter, MySQL and even memcached (which you still need to connect to - and this time is overhead).

To write such an application in Python or C ++ if you have a good HTTP-server library - no difference. C ++ already has a lot of tools for high-level programming. Even an average PHP programmer is able to write safe and serious applications in modern C ++.

But, and here it is already interesting: the Python HTTPd script script eats up to 10-20Mb of RAM, and the C ++ server launched with the recently published WebToolKit eats just 0.6-1.4Mb (this is about RES memory). So such small optimizations on the server can run the sea (unlike Python, which quickly eats up all the memory in chunks of 20Mb on a small VPS).

Similarly, to make, for example, a real-time display of users on the site — someone on which page — from where.

As a result, the site is still in PHP, it is accelerated dozens of times (due to optimization of the hardest part to execute), the database is unloaded, no dog pile of caching effects, etc., and by the time it takes 2-3 hours to this optimization.

Another example is voting. Suppose that you need to add a voice for someone via AJAX and save the user's IP so that he no longer votes. In C ++, it will take in memory well, maybe byte 20, so you can safely store all the data about all users and votes for the last week with instant access to them. At the same time, if you do it in the usual way (PHP / Python + MySQL / SQLite / Postgres ...), the database will grow and access to it will slow down and if the calls go all the time, it can slow down the server significantly. I don’t say that SQL will lose once in 10 memories, but I have had such cases (in particular, SQLite: memory: it loses files to SQLite tens or even hundreds of times).

Another example is the user's geo-location. You can load information about all IPs into memory (of course, in the form of a range), and C ++ will take one cycle in nanoseconds, while PHP + MySQL will dig for a long time choosing the right range.

Another thing: the counter of something, let's say the number of votes for the post (or karma) is the karma of all users really kept in memory and let AJAX directly contact the sish server, at the same time PHP / MySQL, and Django what they will do: check changed whether the file (PHP), let's do a regex check where we are going (django routes), if there is no optimizer in PHP, we will read the file again, parse it, initialize the connection to the database, request one digit, set the lock so that no one has changed it yet, increase it, go back, remove the lock.

And that is if there is no ORM, and after all Django still creates objects to process the request and return it. This is all, of course, a problem, not a catastrophe, but then again - if you spend 99% of your time on useless things - is it not better to get rid of them in an hour?

At the same time, for C ++ it is practically a pair of processor cycles (+1 to the Integer in memory and translated into a string). Simply put, while PHP + MySQL processes one such request, C ++ can do a thousand, maybe a million (if you do not take into account the time for processing the HTTP protocol itself). Yes, and such a program in C ++ will actually be shorter (but it is hardly much longer) than PHP + MySQL.

In general, if there are tasks of the level “you need to add one to something”, “to find something among a small set of something” - then pieces of sites in C ++ can help you very much. But the general idea is still better to do something more high-level (such as PHP / Python) or look at languages ​​such as Arc.

Disadvantages of this approach?

Well, more troubles.

1. Monitoring, for example. As an option - the easiest, let's say there is compiled_file.cpp, which compiles in compiled_file (without the extension). Go to the crontab (crontab -e) and append:

* * * * * / bin / pgrep compiled_file || / path / to / compiled_file

The script will lie down for a minute if something goes wrong, then it will restart. In reality - less than half a minute.

2. Update the running process. Yes, just replacing the file, as in PHP will not do, will have to kill the process (killall compiled_file) and restart.

So you shouldn’t reject the idea of ​​C ++ sites right away, but you don’t need to suffer fanaticism either. You can write a whole site in C ++, but if you have half a man a day, then why?

Just using this language should be reasonable. If your server is 100% loaded and you know that 90% of requests go to one place, which could very simply be expressed as a small server with a 1MB memory footprint and that would not require databases, interpreters, etc. - then why buy more expensive equipment instead of replacing this piece with a compiled one?

And if C ++ is very scary, then look at what Cython is for Python.


Yoi Haji
view from Habra

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


All Articles