fhg
Volume 9, Number 30 -- September 30, 2009

Subprocedure Return Values--Food for Thought

Published: September 30, 2009

by Jon Paris

Those of us who have adopted subprocedures as a way of life often use their ability to return a result without thinking too much about the possible implications. But no technology should be thrown blindly into production without some understanding of the underlying tradeoffs.

Ted Holt has previously written tips on the performance aspects of parameter passing (Parameter Passing and Performance) and on the basics of subprocedure performance (Performance of Function Subprocedures). In this article, I want to take that discussion one step further because I find that many people still fail to appreciate the potential performance implications of certain types of subprocedure return values.

While the primary determinant for our coding style should always be readability and maintainability, nevertheless, it is important to understand that there are ramifications to our design choices. Failure to understand this can result in significant performance issues as the application evolves over time. So in this tip we are going to review the performance implications of some of the choices that we might make. In order to do that we need to consider some of the underlying mechanics of returning a value from a subprocedure.

When a value is returned by a subprocedure, just how does that value get back to the caller? The answer is that it is first copied into a temporary storage area normally referred to as the stack. Once control is returned to the caller, it will typically have to be copied again, this time from the stack into its final destination. For example, when our subprocedure codes "RETURN TheAnswer;" the contents of the variable TheAnswer are copied onto the stack. If the caller originally invoked the subprocedure by coding "TheAnswer = MySubProc( Parm1, Parm2);" then that data must them be copied from the stack into TheAnswer. The overhead of doing this is negligible when dealing, for example, with an account number validation routine that returns an indicator, or even when using a routine that returns a single record. But it has the potential to cause problems when returning with, say, 50 records at a time. It may not be a performance issue when the application is first deployed, but what if a subsequent programmer decides to increase the number to 100 or 500? An apparently trivial change, but what about the performance issues? Will they even think about it? I doubt it.

I tend to only use return values for small amounts of data. Of course the term "small" is somewhat vague, so what do I mean by it? The rule of thumb I use is:

  • Use a return value when returning a field or record
  • Otherwise return the data as a parameter, just as in a program call scenario. Use the return value to return a count of the number of records returned, something that is often difficult to determine from a returned result set

To check the speed differences, I tested using three small subprocedures. The first simply returns a field of 32,000 characters. The second has no return value, and returns 32,000 characters as a parameter. The third simulates the return of a count of the number of items in the result set as well as returning the actual data as a parameter. You can see the prototypes below:

D TestProc1       Pr         32000a

D TestProc2       Pr
D   result                   32000a

D TestProc3       Pr            10i 0
D   result                   32000a

Within the subprocedures, no attempt is made to actually calculate any values. Rather, the intent is simply to measure the overhead of the different call mechanisms.

So is there a significant difference in the performance of the two approaches? My initial tests with 1,000 iterations showed that it took over 650 times as long to return the 32,000 characters as a return value, compared with returning them as a parameter. I must confess that while I was expecting the difference to be significant, I was surprised by its magnitude. Testing with larger sample sizes further emphasized the difference.

I should also mention that the size factors that impact the performance of return values, also apply to passing parameters to subprocedures by value. VALUE is a tempting keyword to use in parameter definition because passing by value guarantees that the called procedure receives a copy of the data. As a result there is no possible way that code in the subprocedure can unexpectedly change the original value. This is not true when using CONST, as in that case no copy of the data is made when the data type and size of the parameter match the requirements defined in the prototype. So when considering how to pass parameters, the same criteria (i.e., OK for small parameters, but think before using big ones) should be applied when considering your choice of parameter passing methods.

So there you have it. While the power of the hardware we use today means that we rarely need to consider performance to the extent that we used to, ignoring it completely when designing procedure interfaces is not a good idea. A poor design for a routine that is called thousands of times a second can leave you in a world of pain.

If you would like to experiment yourself with the code used to perform these tests, contact me via my Web site (http://www.Partner400.com) and I will be more than happy to send you a copy to play with.


Jon Paris is one of the world's most knowledgeable experts on programming on the System i platform. Paris cut his teeth on the System/38 way back when, and in 1987 he joined IBM's Toronto software lab to work on the COBOL compilers for the System/38 and System/36. He also worked on the creation of the COBOL/400 compilers for the original AS/400s back in 1988, and was one of the key developers behind RPG IV and the CODE/400 development tool. In 1998, he left IBM to start his own education and training firm, a job he does to this day with his wife, Susan Gantner--also an expert in System i programming. Paris and Gantner, along with Paul Tuohy and Skip Marchesani, are co-founders of System i Developer, which hosts the new RPG & DB2 Summitconference. Send your questions or comments for Jon to Ted Holt via the IT Jungle Contact page.


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
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
E-mail: software@worksright.com
Web site: www.worksright.com


Senior Technical Editor: Ted Holt
Technical Editor: Joe Hertvik
Contributing Technical Editors: Erwin Earley, Brian Kelly, Michael Sansoterra
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

System i Developer:  RPG & DB2 Summit in Minneapolis, October 13-15; 3 days of serious training
Manta Technologies:  Fall Sale on i training courses! Order by October 15 and SAVE 25%
Halcyon Software:  Automated operations software for IBM i i5/OS - for as little as $25 a day!


 

IT Jungle Store Top Book Picks

Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
The iSeries Express Web Implementer's Guide: List Price, $49.95
The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
The System i Pocket RPG & RPG IV Guide: List Price, $69.95
The iSeries Pocket Database 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
Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
Getting Started with WebSphere Express for iSeries: List Price, $49.00
Can the AS/400 Survive IBM?: List Price, $49.00
Chip Wars: List Price, $29.95


 
The Four Hundred
IBM to Mothball a Whole Bunch of Stuff with Power7

IBM, VMware Cooking Up vSphere 4.0 Support for i

What Apple Did That IBM Must Emulate

As I See It: After You're Gone (.com)

IBM Says Microsoft 'Grossly Exaggerated' Exchange Sales Data

Four Hundred Stuff
LANSA Gives aXes Screen Modernization Tool a Makeover

JAMS Brings Scheduling and File Transfer Capabilities to i OS

Trucking News: TMW Brings More Applications to i OS

VAI Adds Desserts to Food Distribution Package

Lawson Finds Search Software a Good Fit for M3

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

System i PTF Guide
September 26, 2009: Volume 11, Number 39

September 19, 2009: Volume 11, Number 38

September 12, 2009: Volume 11, Number 37

September 5, 2009: Volume 11, Number 36

August 29, 2009: Volume 11, Number 35

August 22, 2009: Volume 11, Number 34

August 15, 2009: Volume 11, Number 33

TPM at The Register
Microsoft, nVidia tag team on HPC

New accounting rules to juice IT numbers

HP-UX gets biannual face-lift

Microsoft munches super startup carcass

HP: We will grow faster than IT

Novell forces customers to pay for maintenance

Mainframe emulator goes commercial

Fujitsu battles WMDs with online survey

Red Hat mocks Meltdown in Q2

Super Micro gets dense with blades

Citrix ships virtual NetScaler accelerator

Mellanox kicks off race to 40 Gigabit Ethernet

THIS ISSUE SPONSORED BY:

WorksRight Software
Help/Systems
Halcyon Software


Printer Friendly Version


TABLE OF CONTENTS
Subprocedure Return Values--Food for Thought

Mass Rename of IFS Files

When PC5250 Run the Same Doesn't Run

Four Hundred Guru

BACK ISSUES




 
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-2009 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement