• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Avoid Large Local Variables in Modules

    July 25, 2007 Ted Holt

    In two previous articles, I dealt with some program design practices that can cause performance problems. Today, I deal with another area where large variables can have a derogatory impact on performance, and I show you a couple of ways to get around the problem. I use RPG for my examples, but the principle applies to any language.

    Let’s say we need three string-handling subprocedures–I’ll call them DoThis, DoThat, and DoTheOther–that will be used in many programs. Since they’re used so widely, let’s put them into a service program. Let’s say further that each of these subprocedures accepts two 64K variable-length character parameters, and that each one needs a 64K character work variable. Here’s the source for module MYSRVPGM.

    H nomain                                            
                                                        
     /copy prototypes,MYSRVPGM                          
                                                        
    P DoThis          b                   export        
    D                 pi                                
    D* parameters                                       
    D  OneString                 65535a   varying const 
    D  AnotherString             65535a   varying const 
    D* local variables                                  
    D  WorkString     s          65535a   varying       
     /free                                              
           WorkString = OneString;                      
           Return;                                      
     /end-free                                          
    P                 e                                 
                                                        
    P DoThat          b                   export        
    D                 pi                                
    D* parameters                                       
    D  OneString                 65535a   varying const 
    D  AnotherString             65535a   varying const 
    D* local variables                                  
    D  WorkString     s          65535a   varying       
     /free                                              
           WorkString = OneString;                      
           Return;                                      
     /end-free                                          
    P                 e                                 
                                                        
    P DoTheOther      b                   export        
    D                 pi                                
    D* parameters                                       
    D  OneString                 65535a   varying const 
    D  AnotherString             65535a   varying const 
    D* local variables                                  
    D  WorkString     s          65535a   varying       
     /free                                              
           WorkString = OneString;                      
           Return;                                      
     /end-free                                          
    P                 e                                 
    

    If this were a real application, the subprocedures would do something, of course. These stubs will serve to help us get a rough idea of how large local variables can affect performance.

    Here’s part of a typical caller.

    /free                                 
        *inlr = *on;                      
        for Index = 1 to Limit;           
           DoThis (String1: String2);     
           DoThat (String1: String2);     
           DoTheOther (String1: String2); 
        endfor;                           
    

    Each time DoThis, DoThat, or DoTheOther is invoked, the system has to allocate a 64-kilobyte variable. When the subprocedure returns to the caller, the system deallocates the local variable. This allocation and deallocation is of no importance for small variables, but can be expensive when the variable is large, as in this case.

    In order to get a rough idea of how performance might suffer, I submitted a calling routine to batch and looked to see how many CPU seconds the system would take. This gave me a baseline to compare other techniques against. Here are the figures in CPU seconds, according to the job log.

    Number of iterations

    CPU seconds

    1

    1

    1,000,000

    8

    10,000,000

    75

    There is nothing scientific about these measurements. All this table really says is that allocation of a large local variable is no big deal if you don’t create it too often. I realize that some programs do run for hours, processing hundreds of thousands, if not millions of records, so the idea of invoking three subprocedures 10 million times is not unrealistic, but I do think it is atypical of most programs.

    In cases where 75 CPU seconds of run time is a problem, we need to find some way to amputate some of that run time. Here are two other approaches you might take to improve performance.

    1.    Use global variables, rather than local variables, in the module. In the following variation of the module, all three subprocedures use the same copy of a global variable, which is allocated only once, when the service program is first activated.

    H nomain                                            
                                                        
     /copy prototypes,MYSRVPGM                          
    D  WorkString     s          65535a   varying       
                                                        
    P DoThis          b                   export        
    D                 pi                                
    D* parameters                                       
    D  OneString                 65535a   varying const 
    D  AnotherString             65535a   varying const 
     /free                                              
           WorkString = OneString;                      
           Return;                                      
     /end-free                                          
    P                 e                                 
                                                        
    P DoThat          b                   export        
    D                 pi                                
    D* parameters                                       
    D  OneString                 65535a   varying const 
    D  AnotherString             65535a   varying const 
     /free                                              
           WorkString = OneString;                      
           Return;                                      
     /end-free                                          
    P                 e                                 
                                                        
    P DoTheOther      b                   export        
    D                 pi                                
    D* parameters                                       
    D  OneString                 65535a   varying const 
    D  AnotherString             65535a   varying const 
     /free                                              
           WorkString = OneString;                      
           Return;                                      
     /end-free                                          
    P                 e                                 
    

    I submitted the caller to batch with this service program, and found better results.

    Number of iterations

    CPU seconds

    10,000,000

    4

    2.    Use the STATIC keyword on the local variables.

    H nomain                                           
                                                       
     /copy prototypes,MYSRVPGM                         
                                                       
    P DoThis          b                   export       
    D                 pi                               
    D* parameters                                      
    D  OneString                 65535a   varying const
    D  AnotherString             65535a   varying const
    D* local variables                                 
    D  WorkString     s          65535a   varying static
     /free                                             
           WorkString = OneString;                     
           Return;                                     
     /end-free                                         
    P                 e                                
                                                       
    P DoThat          b                   export       
    D                 pi                               
    D* parameters                                      
    D  OneString                 65535a   varying const
    D  AnotherString             65535a   varying const
    D* local variables                                 
    D  WorkString     s          65535a   varying static
     /free                                              
           WorkString = OneString;                      
           Return;                                      
     /end-free                                          
    P                 e                                 
                                                        
    P DoTheOther      b                   export        
    D                 pi                                
    D* parameters                                       
    D  OneString                 65535a   varying const 
    D  AnotherString             65535a   varying const 
    D* local variables                                  
    D  WorkString     s          65535a   varying static
     /free                                              
           WorkString = OneString;                      
           Return;                                      
     /end-free                                          
    P                 e                                 
    

    Static variables are allocated once and retain their state across invocations. The system does not have to allocate and deallocate with each invocation of a subprocedure. The results looked equally as good as the results I got when I used a local variable.

    Number of iterations

    CPU seconds

    10,000,000

    4

    Of the two approaches, I prefer the STATIC approach. It gives me the advantages of local variables without the possibility that two subprocedures might step on each other’s feet.

    The lesson I take is this: Use all the small local variables you want, but look for alternatives before declaring large variables in subprocedures.

    RELATED STORIES

    Parameter Passing and Performance

    Performance of Function Subprocedures



                         Post this story to del.icio.us
                   Post this story to Digg
        Post this story to Slashdot

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Maxava

    Migrate IBM i with Confidence

    Tired of costly and risky migrations? Maxava Migrate Live minimizes disruption with seamless transitions. Upgrading to Power10 or cloud hosted system, Maxava has you covered!

    Learn More

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    Maximum Availabilty:  The Ultimate System i Replication for Business of All Sizes
    COMMON:  Join us at the Annual 2008 conference, March 30 - April 3, in Nashville, Tennessee
    New Generation Software:  Leading provider of iSeries BI and financial management software

    IT Jungle Store Top Book Picks

    The System i Pocket RPG & RPG IV Guide: List Price, $69.95
    The iSeries Pocket Database Guide: List Price, $59.00
    The iSeries Pocket Developers' Guide: List Price, $59.00
    The iSeries Pocket SQL Guide: List Price, $59.00
    The iSeries Pocket Query Guide: List Price, $49.00
    The iSeries Pocket WebFacing Primer: List Price, $39.00
    Migrating to WebSphere Express for iSeries: List Price, $49.00
    iSeries Express Web Implementer's Guide: List Price, $59.00
    Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
    Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
    Getting Started with WebSphere Express for iSeries: List Price, $49.00
    WebFacing Application Design and Development Guide: List Price, $55.00
    Can the AS/400 Survive IBM?: List Price, $49.00
    The All-Everything Machine: List Price, $29.95
    Chip Wars: List Price, $29.95

    Global Hires Former Infor Manager IBM Ready to Announce Power6-Based System i Box

    Leave a Reply Cancel reply

Volume 7, Number 27 -- July 25, 2007
THIS ISSUE SPONSORED BY:

Help/Systems
ProData Computer Services
Guild Companies

Table of Contents

  • Avoid Large Local Variables in Modules
  • Memory Management: It’s Your Fault, Now Fix It
  • Admin Alert: Getting Around System i Default Passwords, Part 1

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle