Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
09/20/06 08:19
Read: times


 
#124651 - Reasons to Waste Machine Cycles
Responding to: ???'s previous message
Erik said:
I am giving an example of serial data being stored in various places after receipt and verification.

we have a "modular by russ" serial module to get, say 1024 bytes from serial we need to do this to load a buffer
 
for (index = 0; index <1024 ; index++)
{
  SpecificBuffer[index] = getCharacter();
}
 

if we program 'small embedded' instead of 'PC' we do this
 
for (index = 0; index <1024 ; index++)
{
  SpecificBuffer[index] = GlobalSerialBuffer[index];
}
 

and have saved something like 20*1024 instruction cycles.

Ok, the approach by Russ may be 'prettier', 'safer' or whatever, but WHY waste 20k instruction cycles in an environment that is definitely not 'resource rich'.

This is a good example. When you say that the received and verified data needs to be "stored in various places", I think you're implying that there are several different SpecificBuffer[]s, and that each one will have a dedicated loop like the one in your "small embedded" example to copy contents of the GlobalSerialBuffer[] to the corresponding SpecificBuffer[].

If I've correctly understood what you're suggesting, then there are several points to be made:
  • The first is the answer to your "WHY" question. The reason to spend some number of machine cycles on the overhead of the function call is so that you can hide the implementation of GlobalSerialBuffer[] from the other modules. Say that something changes and you suddenly have to accommodate a ten-byte message preamble that's sitting in the buffer, ahead of the 1024 bytes of data that the other modules care about. Or suppose that you want to change GlobalSerialBuffer[] from a linear buffer to a circular buffer of some kind.

    If your other modules pluck the data from GlobalSerialBuffer[] directly like you're suggesting, then you have to find and fix every single place that references the buffer. This creates a lot of redundant work, along with a corresponding number of opporunities to make silly mistakes. But if GlobalSerialBuffer[] is really just plain old SerialBuffer[], hidden from outside in its own module, and you access it through a getCharacter() function, you only have to change getCharacter().

  • Your estimate of "something like 20*1024 machine cycles" for the function overhead is high by roughly 20*(1024-1) machine cycles, because you would not want to grab the characters one by one if you knew you were sometimes going to need them in big chunks. In that case, your serial module should provide a function with a prototype something like this:
     
    void GetSerialData(char *pointerToCallersBuffer, int HowManyBytes);
     

    so that you get a whole buffer's worth of data with one call to the serial module. If the transfer was in fact a simple byte-to-byte copy, GetSerialData() could be a macro that translates into a call to the library routine memcpy(), which might be written in assembly language and might therefore be a little faster than code generated by the compiler. Otherwise (in the case of a circular buffer, for example) GetSerialData() would be a you-written function inside the serial module.

  • I don't think you should dismiss the value of effort towards a design that is "pretty" and "safe". Those qualities contribute to programs that are robust and maintainable every bit as much as things like proper variable names, good comments, and careful formatting of the source code. And if you wind up "wasting" a few machine cycles on "pretty" and "safe", so what? Chances are really good that those machine cycles were going to be truly wasted in an idle loop somewhere anyway.

Erik said:
There is nothing hindering a modular approach by using global variables i.e. I have all handling of serial in ONE module with nothing else, but to make getting a character a means of processor overload is ridiculous.

This is just plain wrong. Simply grouping related functions together into files is a step in the right direction, but that by itself doesn't buy you much of anything beyond making it easier to remember which file contains which functions.

The big point of modular programming is to make the modules independent, 1) so that changes in one of them don't break one or more of the others, and 2) so that you can extract a module from one project and reuse it elsewhere without having to drag along half of the first project to just to make it work. You simply cannot do these things if you allow your modules to muck around with each others' data.

-- Russ


List of 56 messages in thread
TopicAuthorDate
Variables within ISR            01/01/70 00:00      
   static            01/01/70 00:00      
      Static, link            01/01/70 00:00      
   beware            01/01/70 00:00      
      Modular Programming            01/01/70 00:00      
         a rebuttal            01/01/70 00:00      
            A Faq about...            01/01/70 00:00      
            Global variables            01/01/70 00:00      
               "Self Documenting"            01/01/70 00:00      
            Global Variables Compromise Modularity            01/01/70 00:00      
               Module vs Global scope            01/01/70 00:00      
               file static            01/01/70 00:00      
                  RE: file static            01/01/70 00:00      
                     extern does matter            01/01/70 00:00      
                  Undeclared Variables            01/01/70 00:00      
                     to Russ            01/01/70 00:00      
                     try this            01/01/70 00:00      
                        Re: try this            01/01/70 00:00      
               Not Evil            01/01/70 00:00      
               module variables            01/01/70 00:00      
                  yes and no all must be weighed            01/01/70 00:00      
         Hmm            01/01/70 00:00      
            the advantages of not suffering from interruptifob            01/01/70 00:00      
            That is the typical way            01/01/70 00:00      
               yes, absolutely            01/01/70 00:00      
                  Depends on the use            01/01/70 00:00      
                     every rule can (and should) be broken            01/01/70 00:00      
            Fixing Problems vs. Hiding Them            01/01/70 00:00      
               I agree, but many take this as an excuse            01/01/70 00:00      
                  Be Defensive            01/01/70 00:00      
   A Module Example            01/01/70 00:00      
      if we are to give examples            01/01/70 00:00      
         Poor example            01/01/70 00:00      
            not so poor            01/01/70 00:00      
         Reasons to Waste Machine Cycles            01/01/70 00:00      
            it is still 'hidden' and BTW why hide it            01/01/70 00:00      
               I'm Done Preaching            01/01/70 00:00      
                  I hope that as well            01/01/70 00:00      
         to asm programmers..            01/01/70 00:00      
            Poor Logic            01/01/70 00:00      
               what an utterly stupid teacher            01/01/70 00:00      
                  OO            01/01/70 00:00      
                     No            01/01/70 00:00      
                     What OOP tells you            01/01/70 00:00      
                        A long Time ago            01/01/70 00:00      
                  I disagree            01/01/70 00:00      
                     some of the worst 'spaghetti' I have seen            01/01/70 00:00      
               Rules in C            01/01/70 00:00      
                  rules imposed on C            01/01/70 00:00      
                     Language Problems            01/01/70 00:00      
            that is very relevant            01/01/70 00:00      
         what about the library?            01/01/70 00:00      
            OH NO            01/01/70 00:00      
               Experience is the best teacher            01/01/70 00:00      
                  overlay            01/01/70 00:00      
                  to Russell            01/01/70 00:00      

Back to Subject List