DevDisasters

Modular Process Improvement

After nearly eight years of working as a C++ developer at Rik V.'s insurance company, a certain coworker was finally laid off.

One task that fell to Rik was to run an application that his former colleague wrote, which compared two directories and put any files that had changed into a third. This was a semi-frequent task, and one that his coworker had devoted quite a bit of time to each month.

Performance Issues
The first time Rik ran the application, he noticed that it was taking an exorbitant amount of time to complete. After five minutes, it barely scratched the surface of the directories, so Rik took off for lunch and returned later to see that the app had run for 43 minutes. Curious as to how a directory comparison could take so long, he peeked at the code. Here's what he found:

long ReadBinaryFile(CString 
		strFile, BYTE** pResult)
{
    BYTE* pBuffer[256];
    BYTE* pResultBuffer = NULL;
    long nLenResultBuffer = 0;
    CFile file;
      
    if(!file.Open(strFile, 
	 CFile::modeRead)) {
      return NULL;
    }
      
    UINT nBytesRead = 256;
    while(nBytesRead)
    {
        nBytesRead = 
		file.Read(pBuffer, 255);
        if(nBytesRead)
        {
            BYTE* pNewBuffer = 
			new BYTE[nBytesRead 
		 + nLenResultBuffer];
            ZeroMemory(pNewBuffer,
 			nBytesRead 
	  + nLenResultBuffer);
            memcpy(pNewBuffer, 
			pResultBuffer, 
   nLenResultBuffer);
      memcpy(pNewBuffer 
		 + nLenResultBuffer, 
 pBuffer, nBytesRead);
      delete[] pResultBuffer;
      pResultBuffer = pNewBuffer;
      nLenResultBuffer 
	    += nBytesRead;
    }
 }
 *pResult = pResultBuffer;
 return nLenResultBuffer;
}

"I was stunned," Rik said, as he relayed the situation to Doug, who was in the next cubicle. "For a 5MB file, his code would require 20,000-plus loops, 20,000-plus memory allocations of gradually increasing size, and at least 40,000 memcopy operations. And that's just for one file!"

Do-Over
Not knowing whether to laugh or cry, Rik decided to take the safe approach and just rewrite it.

After rewriting, he restarted the app-and it finished in less than 20 seconds. With a couple of logic tweaks elsewhere in the code, he got this down to 13 seconds-from 40 minutes.

"I'm not sure if the guy was a moron or a genius," Rik told Doug. On one hand, the code was absolutely appalling. On the other, it gave the former coworker the opportunity to say: "Sorry, the application is still running; I've got to wait for it to finish"-and then go back to reading the newspaper.

About the Author

Alex Papadimoulis lives in Berea, Ohio. The principal member of Inedo, LLC, he uses his 10 years of IT experience to bring custom software solutions to small- and mid-sized businesses and to help other software development organizations utilize best practices in their products. On the Internet, Alex can usually be found answering questions in various newsgroups and posting some rather interesting real-life examples of how not to program on his Web site TheDailyWTF.com. You can contact Alex directly via email at [email protected].,

comments powered by Disqus

Featured

  • See Prompts Microsoft Engineers Use for Bleeding-Edge Multimodal RAG AI Research

    Vision-centric queries show how front-line experts are prompting LLMs these days.

  • AI Explains Expressions in Update to Java on VS Code

    "The Spring Tools now show code lenses above these expressions that allow you to quickly let GitHub Copilot explain those statements for you."

  • Microsoft Eases Integration with Semantic Kernel AI SDK

    The basic idea is to provide unified API abstractions, especially for idiomatic C# code, to help platform developers and others work with any provider with standard implementations for caching, telemetry, tool calling and other common tasks.

  • Final .NET 9 Preview Ships with Go-Live License

    Visual Studio developers can now download the SDK for .NET 9 Release Candidate 2 with a go-live license, meaning devs get Microsoft support for production applications even before the framework reaches general availability next month.

Subscribe on YouTube