Lambda functions – recursion and other tricks

A few useful lambda function ideas, especially how to write a recursive lambda function in C++11:

Interestingly, lambdas can be recursive. For example, here’s a lambda that implements the fibonacci series using recursion:

function<int(int)> fib1 = [&fib1](int n) -> int
{
  if(n <= 2)
    return 1;
  else
    return fib1(n-1) + fib1(n-2);
}

Note that we had to actually name the lambda in order to implement the recursion. Without a name, how would you make the recursive call? This self-referencing places some restrictions on how a recursive lambda can be used; the lambda doesn’t refer to itself, it refers to fib1, which happens to be itself.

Leave a comment

Filed under C++

The optimizer – Google’s Jeff Dean

Really interesting article on the difference a ten-times-better programmer can make, plus some bogus Jeff Dean facts:

  • Compilers don’t warn Jeff Dean. Jeff Dean warns compilers.
  • Jeff Dean writes directly in binary. He then writes the source
    code as documentation for other developers.
  • When Jeff Dean has an ergonomic evaluation, it is for the protection of his keyboard.
  • Jeff Dean was forced to invent asynchronous APIs one day when he optimized a function so that it returned before it was invoked.

Leave a comment

Filed under Programming, Technology

Dr Dobbs article on software development for Obama’s campaign

Dr Dobbs wrote this article on the ways developing for an election campaign differ from typical projects.

“We realized early on that we had to understand that ‘good enough’ was indeed good enough. Often, 90% of the way there was the same as 100% and we just didn’t have the bandwidth to put a lot of time into the final 10% or polishing every last detail.”

The client wasn’t used to iterative development where they saw early incomplete releases of features.

The difference turned out to be crucial, as many features were changed in ways that became critical to their smooth operation on election day.

After the feature freeze in October,

No new features were added, unless mandated by regulation or indispensable to the operation. And in some cases, when an app didn’t work as hoped, team members reassessed its criticality and simply dumped the app if it was no longer truly essential.

At least they won’t have to do it all again for the next election, right? Wrong.

The team was unequivocal in its conviction that by 2016, all the code would have to be written from scratch again, due to change in technology and how people communicate. They saw no possibility of reusing their code. In fact, they felt that part of their opponents’ software difficulty was tied to reworking code from 2008, rather than writing the apps from scratch.

Leave a comment

Filed under Programming

Time to apply the brakes to high-speed trading?

UltraHighFrequencyTrading.com posted a good article on the history behind high frequency trading and the potential consequences of some proposed changes to trading rules:

High-frequency trading might appear to pose threats on the horizon, notes Tabb, but hasty regulation is all but certain to trigger unintended consequences. “It could totally destroy the market,” he says. If rules lock a high-frequency investor into a bid of $102 for even half a second when the market value is $101, other investors could swoop in at $101 and make a dollar a share on the incorrect price. This will create incentives not to quote or provide liquidity, making it harder and much more expensive to invest.

Now the debate is getting political:

While the debate simmers, high-frequency traders are enlisting influential allies in Washington. Republican members of Congress Jeb Hensarling of Texas and Spencer Bachus of Alabama are advocating a slow approach to any regulatory initiatives. In letters to the SEC and the House Financial Services Committee, both congressmen warned not to “shoot the computers first and ask questions later.”

Leave a comment

Filed under Finance, Technology

Herb Sutter Video – C++ Concurrency

Another excellent video from Herb Sutter, this time on C++ Concurrency. The Monitor class is based on his Wrapper pattern
20130118-133301.jpg
and allows the caller to group related calls into a transaction, with synchronisation supplied via a mutex.
20130118-133722.jpg
The Concurrent class replaces the mutex with a concurrent_queue to avoid the caller blocking whilst the tasks complete.
20130118-133609.jpg
What’s especially elegant is ~Concurrent which pushes a done function onto message queue in order to signal not to wait for any more tasks (the concurrent_queue.pop() is assumed to wait until the next task is pushed). The done data member is not required to be atomic because it is only mutated on the worker thread, never on the calling thread.

1 Comment

Filed under C++, Video

Star programmer outsourced his own job!

TheRegister reports that an audit discovered a firm’s star programmer was outsourcing his own work to China!

A security audit of a US critical infrastructure company last year revealed that its star developer had outsourced his own job to a Chinese subcontractor and was spending all his work time playing around on the internet.

The ruse was obviously paying off for him handsomely:

Further investigation found that the enterprising Bob had actually taken jobs with other firms and had outsourced that work too, netting him hundreds of thousands of dollars in profit as well as lots of time to hang around on internet messaging boards.

Leave a comment

Filed under Programming

Personal Projects

Thought-provoking essay on the hidden value of personal projects:

All of these properties of personal projects suggest they’re a really good predictor of talent that is worth hiring. Though this approach may suffer from the occasional false negative (even the greats now and again bang out quick-and-dirty personal projects don’t necessarily impress), a false positive probably never does: you’ll probably never find someone with a great personal project who also turns out to be rubbish.

Leave a comment

Filed under Programming

Memory corruption due to C++ code incompatible with C++11 standard

I’m a big fan of C++11 and the new features that have become available, including move semantics.  However, our team found today that a code optimization which worked under VS 2005 was causing memory corruption under VS 2010.

The story goes back to a string holder class a bit like this (if you think BSTRs you’ll be on the right lines, but taking COM out of the picture makes the example clearer):

class CDeepCopy
{
protected:
  char* m_pStr;
  size_t m_length;

  void clone( size_t length, const char* pStr )
  {
    m_length = length;
    m_pStr = new char [m_length+1];
    for ( size_t i = 0; i < length; ++i )
    {
      m_pStr[i] = pStr[i];
    }
    m_pStr[length] = '';
  }

public:
  CDeepCopy() : m_pStr( nullptr ), m_length( 0 )
  {
  }

  CDeepCopy( const std::string& str )
  {
    clone( str.length(), str.c_str() );
  }

  CDeepCopy( const CDeepCopy& rhs )
  {
    clone( rhs.m_length, rhs.m_pStr );
  }

  CDeepCopy& operator=( const CDeepCopy& rhs )
  {
    if (this == &rhs)
      return *this;

    clone( rhs.m_length, rhs.m_pStr );
    return *this;
  }

  bool operator<( const CDeepCopy& rhs ) const
  {
    if (m_length < rhs.m_length)
      return true;
    else if (rhs.m_length < m_length)
      return false;

    return strcmp( m_pStr, rhs.m_pStr ) < 0;
  }

  virtual ~CDeepCopy()
  {
    delete [] m_pStr;
  }
};

CDeepCopy was used as the key into a map. For large data sets with heavily overlapping key values, this was inefficient because each lookup would entail the cost of a copy constructor – this was particularly expensive due to the heap operations. As an optimization, the following technique was used to avoid the copy constructor call in the event of an update operation, yet worked correctly if the operator[]() call led to an insertion.

class CShallowCopy : public CDeepCopy
  {
  public:

  CShallowCopy( const std::string& str ) : CDeepCopy()
  {
    m_pStr = const_cast<char*>(str.c_str());
    m_length = str.length();
  }

  ~CShallowCopy()
  {
    m_pStr = nullptr;
  }
};

So you could do an efficient lookup like this that only caused a deep copy if an insertion into the map was required instead of an update:

int _tmain(int argc, _TCHAR* argv[])
{
  std::map<CDeepCopy, int> entries;
  std::string hello( "Hello" );

  CDeepCopy key( hello );
  entries[key] = 1;

  // Named variable
  CShallowCopy key2( hello );
  entries[key2] = 2;

  // Unnamed temporary variable
  entries[ CShallowCopy( hello ) ] = 3;

  return 0;
}

This code worked as intended in VS2005. However, compiled under VS2010 we were seeing memory corruption in the final case (the one with the unnamed temporary). After some debugging, we realised that under C++11, std::map has an operator[]( KeyT&& key ), that is the key is an rvalue reference – and this form is called for our unnamed temporary case. That avoids calling our CDeepCopy copy constructor, and so the key in the map can be left referencing deallocated memory.

A simple workaround prevents this happening again – declare CDeepCopy( CDeepCopy&& ) as private but omit the definition – this means that the unnamed temporary usage does not compile.

Leave a comment

Filed under C++ Code

Pop-up buttons for flat screen technology

TechCrunch broadcast an interview with Tactus, the company behind an innovation to bring transient pop-up buttons to flat screen devises.

Leave a comment

Filed under Technology, Video

BATS looks to compensate after long-term system error

BATS Global Markets has identified a software bug that has caused almost 450,000 trades to be executed at the wrong price over a period of 5 years, according to thetradenews.com:

The exchange group found that some short-sale orders were executed at a price that is equal to or less than the national best bid or offer, while non-displayed pegged orders may not have been executed at the most optimal price.

Under the SEC’s Reg NMS rules, trades must be routed to the market displaying the best price. As per short-selling rules, if a stock declines by 10% from the previous day’s close, traders can only sell short at a price one tick above the national best bid or offer until the end of the next trading day.

This sounds like an oversight in their understanding of the complex market rules rather than a bug – it wasn’t that the software was implemented correctly, this rule wasn’t implemented at all!

Leave a comment

Filed under Finance, Programming