fhg
Volume 7, Number 27 -- July 25, 2007

Avoid Large Local Variables in Modules

Published: July 25, 2007

by 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


Sponsored By
HELP/SYSTEMS

SEQUEL can be used for
virtually ALL business intelligence functions
on the System i, including:

                                                    · Executive Dashboards
                                                    · Graphical Query & Reporting
                                                    · Drill-Down Data Analysis
                                                    · Multi-Platform Database Support
                                                    · E-Mail Report and File Distribution
                                                    · Secure Web Access

SEQUEL is the single solution for all
your business intelligence needs.

www.helpsystems.com


Senior Technical Editor: Ted Holt
Technical Editors: Howard Arner, Joe Hertvik, Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Brian Kelly, Marc Logemann, David Morris
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

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

 

The Four Hundred
IBM Creates New Power, SMB Server Divisions

Rumor Du Jour: i5/OS on Other Platforms? Not!

IBM Turns In Its Best Second Quarter in Six Years

As I See It: Lawyers, Lies, and Statistics

The Linux Beacon
Linux Distro Xandros Buys Email Specialist Scalix

HP Buys System Management Tool Maker Opsware for $1.6 Billion

IBM Creates New Power, SMB Server Divisions

As I See It: Lawyers, Lies, and Statistics

Four Hundred Stuff
IBM Upgrades High-End System i Server with Power6

IBM Previews i5/OS V6R1, Due in 2008

EMC Offers Hardware-Based HA Alternative

SugarCRM Now Available for i5/OS

Big Iron
Three-Digit z Boxes Head for History

Top Mainframe Stories From Around the Web

Chats, Webinars, Seminars, Shows, and Other Happenings

System i PTF Guide
July 21, 2007: Volume 9, Number 29

July 14, 2007: Volume 9, Number 28

July 7, 2007: Volume 9, Number 27

June 30, 2007: Volume 9, Number 26

June 23, 2007: Volume 9, Number 25

June 16, 2007: Volume 9, Number 24

The Windows Observer
Ballmer Talks Up 'Cloud Computing'

Opsware Locks Down Server Changes with SolidCore

Microsoft Ships Windows Home Server

Oracle Says 11g Database Is Better, Cheaper, and Faster

The Unix Guardian
IBM Creates New Power, SMB Server Divisions

IBM Turns In Its Best Second Quarter in Six Years

Intel Certifies Solaris on Its Carrier-Grade Servers

Mad Dog 21/21: To Avatar and Avatar Not

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

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

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
What's coalesce good for?

Duplicated printer files

Urgent Help Needed--Limit the result set in SQL stored procedure

Problem with "cpyfrmimpf"

FNDSTRPDM Output Member Name to *OUTFILE





 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement