Why programming in ANSI C is not dead, or how I learnt to hate new wave of programming

Preface

Ok I must admit I am not a seasoned veteran with having been involved in numerous secret raids on Al-Quaeda, or have over 300 confirmed kills. Neither am I trained in gorilla warfare or I'm the top sniper in the entire US armed forces. But I know my shit, you can trust me on that.

I recently had to respond once again to a server issue. To give a small backstory, when we started Jar we started getting some initial incomes by providing web hosting and building wordpress sites. Your typical small size hosting company, with some boxes on aws, and a number of clients. Most of our clients are close friends, and we are proud of our work! go check them out! Safebebe.gr provides baby proofing for your house, and our friend Dimitris Petrounias has a very popular modern psychology site.

Well one of our servers used to have stability issues. With enough gigabytes in ram, and quite some CPU power, it kept crashing with out of memory errors. We have been trying to pinpoint the culprit for quite some time but to no avail. It kept coming back to the apache process. The traffic did not justify such behaviour and we were in a constant rat race of crashing and rebooting.

Now, let me tell you a story, of why knowing how to code simple scripts does not make you a programmer.

Web Server Basics

In web development, you usually have some very basic default configurations. You can go for a WAMP machine, which are the initials of the technology stack used, Windows, Apache, MySql, PHP, a LAMP machine, same but with Linux, and many more. We opted for a simple LAMP, since we are well versed in managing Linux servers, with an Apache 2 server, a MySql (or MariDB if you wish), and of course, PHP. We generally use this stack and deploy WordPress sites, as they are the easiest for our customers to manage and extend.

Each of the components of the server serves a different purpose and is good doing a number of things. I will not go into details, but all in all, your operating system excels at managing system resources, MySql is good at fetching, sorting, and filtering information (amongst others), and PHP is really good and efficient in constructing WebPages based on information received from multiple sources, as in your Database Management System.

All in all, if you use your technology stack right you should be able to work even with limited resources, as your technologies tend to be very efficient and optimized in CPU/Memory/Disk operation consumption in the things they do well.

ANSI C Nerd

I have been blamed multiple times that I code everything as if I'm writing ANSI C. I like managing my own memory resources, I like to get deep and dirty. This yields maximum control and efficiency. If you have fucked up it's on you.

In programming, each time you try to read information from the disk, this is loaded in the Memory. This means that if for instance you want to load a file of let's say 8 MegaBytes from the Disk to use in your program, you will end up allocating ~8MB of memory for that file to be in your memory and work with it. Similarly, if you want to load information from the Database, that information will allocate some amount of memory.

Modern programming languages and advances in hardware often allow you to forget about the fact that resources are not unlimited. You will see people loading gigabytes of information in memory only to do some simple operations that do not always need the whole file to be loaded in memory. One common technique to do that is paginating, which means loading and using the file in chunks. Many of the challenges you get when having an interview for a software development position, will involve some kind of pagination.

I will not bore you with more details, but you get the point. Resources are limited, to use some kind of information within a program you need to load it in memory.

Databases are BOSS

As I mentioned before, databases are awesome in storing information in a formalised manner. Exploiting their formalised storage of information, database management systems are capable of :

  • Fetching information fast
  • Ordering information fast
  • Filtering information fast
  • Manipulate information, well

Additionally, with some proper configuration (indexing etc) you can make Databases perform even better. One common challenge when dealing with very big and diverse datasets is making sure you only fetch the information you need, when you needed, in the proper format. As you can imaging, it is best to do this kind of operation in the Database, as it excels in doing exactly that! Lets say you have a table (imagine something like an excel sheet) where you store information about your blog posts. Say you want to fetch your latest 6 blog posts, ordered by date. You can tell your Database to Select * from blog_posts order by date Limit 6

As one might expect, a properly indexed database can execute such a query in less than a hundred of a second!

But there is another way...

I hate you script kiddos

Novice programmers tend to forget that they can exploit their tech stack in a way that provides maximum performance. A common mistake is that people will load everything from the database, with no filtering, no ordering, and then try to filter and order in the program's code.

While this might seem as a bad idea given the preface I've given, a lot of people don't think cleanly when they write code. It can be attributed to booze, sleepiness, or plain up incompetence. But what is the issue?

Well, if you load 6 blog posts from your database, and each blog post is of 1 MB size (sizing used for demonstration purposes. If your blog posts are 1 MB you have other problems), then you will load in your memory 6MB. If on the other hand you fetch all of your blogposts, and each blogpost is again of 1MB size, and you have 1024 blogposts, well guess what, you now hold 1GB of memory hostage, just to order your posts in your program (which usually means you will consume even more memory for sorting) and then you will pick the first posts, and then release your 1 GB of memory.

Now imagine, every time someone tries to open your site, you want them to be able to see the latest 6 blog posts. That means that for every page hit on your website, you will be trying to allocate 1GB of memory. Let's say that you average 50 people per minute (which is low for today's standards for big sites), that means that your poor memory will try to allocate 50 GB of memory every minute!

THAT'S A LOT, especially compared to a measly 6MB * 50 -> 300MB per minute that you'd get with the proper way of coding.

To the point

We were fed up with our server crashing. We had to get to the bottom of it.

We tested each and every single website on the server for performance, from cpu consumption, memory consumption, disk operations, traffic, response times etc. And we managed to pinpoint it to a single site. But why?!

Turns out that one of the plugin suites that we have been using, Elementor, and specifically the plugin Essential Addons for Elementor.

Using widget "post timeline", when trying to load 6 latest posts from our blog the widget exhausts the memory limit of 512MB.
This seems a bit excessive. The error from php says:

PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 8192 bytes) in wp-includes/wp-db.php on line 1989

The line from wp-db.php refers to the following:

while ( $row = mysqli_fetch_object( $this->result ) ) {
    $this->last_result[ $num_rows ] = $row;
    $num_rows++;
}

My gut feeling says that there is some kind fault in the query used. I feel as if the plugin is trying to fetch EVERYTHING from the db and then filter the posts on PHP side.

After disabling the widget our site loads 10x faster (from ~12 seconds to ~ 1.2 seconds).

Closing off

When you release a tool for wide use, be wary of your resource consumption. We may be living in 2020, but resources are not unlimited. The more we relax on our coding standards, the less we will achieve. It's a degeneration of quality phenomena that is plaguing the software world for years now. People will take for granted that resources are available (I'm looking at you Chrome). And don't forget, your mobile phone already has multitudes more resources than the computer that got us to the moon

That's all for today folks! I hope you have a good one!

Share this