Our main RSS-feed at DeWereldMorgen.be is the most requested page next to our homepage.
It seems logic, think of how many rss-readers hourly check the feed. And, think of how many cpu and RAM that consumes, certainly with a fat system like Drupal.
An RSS-feed is easy to make in PHP. All you need is one custom query and a decent library like the Universal Feed Generator to generate the XML.
Create stripped version of Drupal setup
I used the minimal code that is needed to work in the Drupal framework. So I made a blank php-file in my www-root with this code:
This code has the security and functions of Drupal, but without the menu’s, theme’s, and a lot of module hooks. You don’t need a menu or a theme to create an RSS-feed, do you?
So I created my RSS-feed with just this in my php-file:
- the drupal_bootstrap function
- the Universal Feed Generator library included
- one query with db_query()
Let’s see performance wise. This is the report of a feed generated based on a user’s blog and articles. Memcache was not cleared before execution. Stats are generated with XHProf.
|Normal setup||My stripped setup|
|Number of function calls||115.200||18.400|
|Consumed RAM||126 MB||62 MB|
|Total execution time||1.169 MS||462 MS|
|Number of database queries||33||15|
|Query execution time||137 MS||80 MS|
Performance wise, my solution is twice as fast.
These stats are generated when caches were cleared.
|Normal setup||My stripped setup|
|Number of function calls||3,284,343||83,330|
|Consumed RAM||172 MB||76 MB|
|Total execution time||9,131 MS||1,000 MS|
|Number of database queries||926||131|
|Query execution time||769 MS||183 MS|
9x as fast. 7x less queries.
Of course, this is just for the first run, but still.
More soon. Please discuss this idea.
- Published:November 8th, 2012
As an alternative to arrays in C#, the MS-programmers use “generic collections”; some kind of an array where the value could be any kind of object. There are 3 big types of generic collections: lists, hashsets and dictionaries. This article is about when you use which type of generic collection, based on own experiences and literature.
A list is ideal for all kinds of collections with less than 5 elements.
A list is fast in adding elements, but exponentially slow in searching for elements. The more elements, the slower a search will be.
For searching elements, always use Find() (Find() is up to 3x as fast as First(), and First() is about 1,5x as fast as Single()).
Elements in a list maintain an order (= enumerable)
A dictionary has a key/value principle, like an array.
Searching for keys is faster than searching for values. Arrange your dictionary that the key value is what you search for. If you need to search for both (keys and values), it’s best to maintain two identical dictionaries, but in the second dictionary, the keys and values are switched.
The creation is slower than the creation of a List.
Every key must be unique.
Searching keys in a Dictionary is up to 3x as fast than searching in a Lists (if the List is bigger than 5 elements). The size of a dictionary doesn’t make the search significantly slower.
A Dictionary is slower to add an element to than a List.
Elements in a Dictionary maintain an order (= enumerable)
Use dictionaries for big lists where searching for elements is more important than adding elements.
To get a value from a key, TryGetValue() is the fastest.
A hashset doesn’t maintain order (= not enumerable). This is different from a Dictionary and a List.
Use a hashset when you want to check if an element is in a list (with “Contains”).
Use a hashset when your list could contain thousands of elements.
Hashsets, like Dictionaries, use the GetHashCode() method of an object to check if it’s unique. If you constructed a hashset with your own objects, you have to make sure the object contains a GetHashCode() override (to check if it’s unique).
|List (< 5 elements)||List (> 5 elements)||Dictionary||Hashset|
|Searching elements||Fastest||(Very) slow (exp.)||Fast||Faster|
(maintains order, loop-able)