Tips and Techniques: A Hidden Performance Issue

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

One of the most intriguing aspects of writing code for clients that have huge/enormous databases is that you get to test the performance of RPG IV features. One feature that continues to annoy me is the fact that a character return value from a procedure that is VARYING sucks from a performance perspective.




For example:

     D ToUpper         PR         32766A   Varying    
     D  inString                  32766A   VALUE Varying  

Seems logical, right? If you convert a character string to uppercase, you want to return the same size/length value to the caller, so the following would be a valid call to TOUPPER:

     D CompName        S             25A   Inz('The Spacecraft Co.')
    
     C                   eval      CompName = ToUpper(CompName)

Obviously, this converts the COMPNAME field to all uppercase letters. But what we don't see is that that the long 32K return value is causing a performance issue. Even through it is varying, the system still returns the entire 32K of data and throws away the extra bytes, a waste of resources.

When the load is light or when TOUPPER is called only once or perhaps a few times per transaction, then this overhead isn't important. But when you call TOUPPER hundreds, thousands, or perhaps millions of times, there could be problems.

To resolve this problem, you need to avoid returning a value and instead pass an additional parameter that is updated.

For example:

     D ToUpper         PR            10I 0
     D  InString                  32766A   Const Varying 
     D  OutString                 32766A   OPTIONS(*VARSIZE)

The second parameter now becomes the target of the conversion, and the OPTIONS(*VARSIZE) allows you to pass a parameter whose length is 1 to 32766 bytes. You simply use %LEN(InString) inside the subprocedure to determine how much data was actually passed to your subprocedure. Then, be sure to "touch" only that many characters in the OUTSTRING parameter.

One problem that arises using this technique, however, is that you can no longer use a VARYING field to receive the output, and this does present restriction. One solution is to have two subprocedures, one that supports fixed-length output and a second one that supports varying-length fields.

For example:

     D ToUpperV        PR            10I 0
     D  InString                  32766A   Const Varying       
     D  OutString                 32766A   VARYING OPTIONS(*VARSIZE)

Bob Cozzi has been programming in RPG since 1978. Since then, he has written many articles and several books, including The Modern RPG Language--the most widely used RPG reference manual in the world. Bob is also a very popular speaker at industry events such as RPG World and is the author of his own Web site and of the RPG ToolKit, an add-on library for RPG IV programmers.

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$