Monday, November 08, 2004

RPC voodoo

It's easy to blow off your data structures class as a weed-out class. There are lots of structures to learn and not much relavancy. Who really needs a linked list after all. You can get by with a dynamic array and some good index management.

The system I work on took that approach with its RPC APIs. The server passed back arrays of structures that were the size specified by the client. That way the client could statically allocate a buffer array and the server could fill the array. The server also passed back how many records were really legitimate. It has worked fine for many, many years.

Last week, I did some remote lab testing. Remote lab testing is where the server is sitting in another state from the client and has a DSL internet connection. Usually the server is sitting 5-10 feet from the client in the lab on a 100mbs network. One word to describe the situation - "Ouch". Performance really stunk.

So, based on the numbers, I tracked down the issue. Frequently the client was allocating a 10K buffer. So, even if there was a few records of 200 bytes (or so), the server would return an array of 10K. Something like 90% of the response was garbage.

Unfortunately, the server really doesn't know how many records are going to be returned because it is just reading information from a dynamic database cursor. So, I bit the bullet and changed from an array returned to a linked list of structures. Only valid data is returned. Performance improved greatly! In certain cases, results that used to take 1 second are returned in 0.1 second. Cool!!!

Lessons learned...

  1. If you use a client/server design, always have a development lab configuration that can duplicate real world/word case environment.
  2. Becareful with arrays, they can lull you asleep with a false sense of security.
  3. Data structures (specifically linked lists) are your friends. Yeah, they make you think, but done in moderation it's not too bad.