• 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
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    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

  • Positive News From The Kyndryl Mainframe Modernization Report
  • NAViGATE, inPower 2025 On Tap for September 2025
  • Guru: WCA4i And Granite – Because You’ve Got Bigger Things To Build
  • As I See It: Digital Coup
  • IBM i PTF Guide, Volume 27, Number 37
  • AI Is Coming for ERP. How Will IBM i Respond?
  • The Power And Storage Price Wiggling Continues – Again
  • LaserVault Adds Multi-Path Support To ViTL
  • As I See It: Spacing Out
  • IBM i PTF Guide, Volume 27, Numbers 34, 35, And 36

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