We're going to carve away a high-performance service from a very simple node app. We'll do load testing to verify our bottleneck, create a protobuf file to clearly communicate the service boundary, and then integrate a golang server implemented with an RPC library called Twirp.

Throughout October 2017, we ran the first season of Productivity Quest — our Slack-based series of challenges tailored to improve the way we work. It turned out to be a huge success and kept the community wanting more. This year, we decided to take a different spin.

Reddcoin was designed to be a social currency to credit creators and others that might be losing to the ever-changing advertisement industry. We're going to see how to work with RDD coins in the sense of sending, managing, and maintaining these coins with Node.js and JavaScript.

Many articles have been written about refactoring. What I'm trying to do here is to bring to light real life's example of how together with my team we approached the problem and how do we plan to deal with it.

SPL Iterators against the performance

This topic’s stayed in my mind for a while. Inspired by Joshua Thijssen’s presentation from PHP UK about (re)discovering the SPL in PHP, I decided to investigate this more carefully. I have to admit that took me some time to understand how the things work and how to not misunderstood the purposes of each iterator and because of lack of documentation it wasn’t that easy. I did a couple of mistakes and probably I will do more, but as Joshua said in his presentation:

The documentation of SPL is completely useless. What can we do? Blog about it!

So, brave yourself. Here my blogpost comes!

Today, I’d like to focus only on one part of documentation – the iterators, because it’s quite attractive how you can solve problems with using them. The main advantage to use the iterators in PHP is an easy way to achieve reusability, single responsibility and nice looking architecture with design patterns. I’d like to show you how quick some of common problems can be solved by using iterators and then, check if that does any impact on the performance.

Design Patterns

Connecting iterators – Decorator

The decorator pattern allows to add a behaviour to individual object, without affecting behaviour of other objects from the same class. As an example we can consider the price calculation for the billing in e-commerce shop where you need to add some taxes, ship price or duty to the final price.

We can split our requirements and create a single responsibility classes for each prize calculation. The third party object will decide which calculations should be included to the final price. The code will look like this:

The previous object is passed as an argument to the next one. When the getPrice method is called then this should call the same method from the passed object. In the result, we have a price calculated by all the objects.

The iterators work the same way in out case. We create a main object which is called ArrayIterator and the add a custom Filters, Iterators, Parsers etc.

FilterIterator

As you can see, it’s not so hard to deliver objects responsible only for one thing. One of the most useful iterator is FilterIterator in my opinion which is extended by FruitIterator in the example. The code might look like:

All necessary things there are placed in accept method which returns a bool value determines if the record can be attached or not. On the other hand, we do not need to implement a new class every time. We can use the CallbackFilterIterator and just attach the closure.

Aggregate Pattern

An Aggregate pattern can refer to concepts in either statistics or computer programming. Both uses deal with considering a large case as composed of smaller, simpler, pieces. This rather refers to an object such as a list, vector, or generator which provides an interface for creating iterators.

This can be quite useful, but only when your classes are kept small – so remember about the SOLID principles there.

Performance

Everything looks promising until now. Two examples above show us how to change ugly foreach or for loops into smart looking object oriented architecture. But the main question is – is that so clever? I have checked and the result surprised me.

I have created two pieces of code to check which method is faster – using full object iteration with SPL, or ugly loops.

Both do exactly the same. Generate a big array with random values and choose only odd results. I run them for 1 000 000 records and the execution time surprised me. I wasn’t prepared for such difference:

Loop: 1,42 s
SPL: 7,82 s

Nothing says more than raw numbers. How pretty SPL Iterators may be, they cannot be such fast as the standard loop. The difference is just astonishing, so I will be careful with using Iterator since now.

Shenzhen Sunper Opto Co.,ltd is a professional led high bay light manufacturer with ten years’ experience. We use Cree led and Meanwell driver, with the strength of top quality and competitive price, and with UL DLc CE RoHs Certificates,our products have a good reputation in North America and Europe.

Products Feature:
1.use Cree led and Meanwell driver specialize designed in led high bay light
2.3-5 years warranty
3.The appearance treatment in Black
a.Black demontrates elegance, fashion and distinction.
b.Black radiator with good heat dissipation, higher heat dissipation efficiency than others,reduce the lumens depreciation of the lamp, extend the
life-span of lamp.
4.The structure of lamp adopts round layered design, manifesting the uniform beauty of lamps.

if you need more, please feel free to contact me(angelina@sunper.net) or click our website for details:http://www.sunper.net/

Adrian Cardenas

Nice over view of how to use iterators & their benefits.

However, you skipped over probably the best reason to use iterators, which is the efficiencies gained in memory.

As you can see, you see the same marked increase in time from using loops to using iterators for the 100k iteration test. Memory usage is also more. However, in the iterators test going increasing the iterations by an order of magnitude increased the duration by an order of magnitude as well, but the memory usage remained constant. Increasing another order of magnitude would increase the duration as well, but memory usage wouldn’t go up much more than the 2.2 KB being used by the script.

http://piotrpasich.com/ Piotr Pasich

Hi Adrian,

nice answer. I can tell you what happen if you run 1,000,000 iterations based on loop – this will take about 72 MB in 1.13 s.

I have investigated the memory usage too, but the differences was too small to talk about this and was caused by using objects against simple arrays. Why the SPL Iterator’s code shows you so low memory usage? Because the memory is freed just after the last foreach. It works in the same way as in the case where you put all the code from http://codepad.viper-7.com/k68YS6 into function like this: https://gist.github.com/piotrpasich/857cd0e4a0988e11a5f4 . At the result, the memory usage is 0.58746337890625 KB.