I Wrote a Faster Sorting Algorithm

It took me more than a day of staring at profiling numbers before I realized why this was faster: It has better instruction level parallelism. You couldn’t have invented this algorithm on old computers because it would have been slower on old computers. The big problem with American Flag Sort is that it has to wait for the current swap to finish before it can start on the next swap. It doesn’t matter that there is no cache-miss: Modern CPUs could execute several swaps at once if only they didn’t have to wait for the previous one to finish. Unrolling the inner loop also helps to ensure this. Modern CPUs are amazing, so they could actually run several loops in parallel even without loop unrolling, but the loop unrolling helps.

.. In American Flag Sort my CPU achieves 1.61 instructions per cycle. In this new sorting algorithm it achieves 2.24 instructions per cycle. It doesn’t matter if you have to do a few instructions more, if you can do 40% more at a time.

Tracking Down a Freaky Python Memory Leak

To track the memory allocated by my Python process, I turned to Performance Monitor2.

Here’s what I did:

  1. I launched Performance Monitor (perfmon.msc).
  2. I created a new Data Collector Set and added the Process > Private Bytescounter for all Python instances3.
  3. I started the Data Collector Set.
  4. I let Performance Monitor collect data for several minutes4.
  5. I exported the data contained in the generated report to a CSV file.
  6. I opened the CSV file in Excel and created a chart for the process that I suspected had the memory leak:c

AsyncIO for the Working Python Developer

By using yield from on another coroutine we declare that the coroutine may give the control back to the event loop, in this case sleep will yield and the event loop will switch contexts to the next task scheduled for execution: bar. Similarly the bar function yields from sleep which allows the event loop to pass control back to foo at the point where it yielded, as it happens with all generators.

pgCenter: Top for Postgres

Couple of days ago a new version of pgCenter has been released. Though it’s still only 0.3.0, it includes many new features and improvements and I hope you will enjoy using this version.

Here are some key highlights:

  • Advanced sorting and filtering
  • Compatibility with PostgreSQL 9.6
  • pg_stat_replication improvements
  • pg_stat_statements improvements
  • Summary info about running (auto)vacuum tasks
  • Support of libpq environment variables
  • pg_stat_activity and pg_stat_statements informative by default
  • Errors reporting when internal errors occur
  • and of course, many internal refactoring and code improvements