Writing More Efficient Code Examples
These examples demonstrate more efficiently written code:
-
Beware of the rowset Fill method. (Or, "What not to do in a Application Engine PeopleCode step.")
Sometimes you need to examine the algorithm you are using. The following example is a PeopleCode program that adopts this approach: read all the data into a rowset, process it row by row, and then update as necessary. One of the reasons this is a bad approach is because you lose the general advantage of set-based programming that you get with Application Engine programs.
Local Rowset &RS; Local Record &REC; Local SQL &SQL_UPDATE; &REC_NAME1 = "Record." | SOME_AET.SOME_TMP; &RS = CreateRowset(@(&REC_NAME1)); &LINE_NO = 1; &NUM_ROWS = &RS.Fill("WHERE PROCESS_INSTANCE = :1 AND BUSINESS_UNIT = :2 AND⇒ TRANSACTION_GROUP = :3 AND ADJUST_TYPE = :4 ", SOME_AET.PROCESS_INSTANCE, SOME_⇒ AET.BUSINESS_UNIT, SOME_AET.TRANSACTION_GROUP, SOME_AET.ADJUST_TYPE); For &I = 1 To &NUM_ROWS &REC = &RS(&I).GetRecord(@(&REC_NAME1)); &REC.SOME_FIELD.Value = &LINE_NO; &REC.Update(); &LINE_NO = &LINE_NO + 2; End-For;This code has the following problems:
-
You might run out of memory in the Fill method if the Select gathers a large amount of data.
-
The Fill is selecting all the columns in the table when all that is being updated is one column.
You can change this code to read in the data one row at a time using a SQL object or using a similar algorithm, but chunking the rowsets into a manageable size through the use of an appropriate Where clause.
The following are some approximate numbers you can use to see how large a rowset can grow. The overhead for a field buffer (independent of any field data) is approximately 88 bytes. The overhead for a record buffer is approximately 44 bytes. The overhead for a row is approximately 26 bytes. So a rowset with just one record (row) the general approximate formula is as follows:
memory_amount = nrows * (row overhead + nrecords * ( rec overhead + nfields * ( field overhead) + average cumulative fielddata for all fields))
-
-
The following are some code examples to show isolating common expressions.
In this example, a simple evaluation goes from happening three times to just once–
&RS_Level2(&I).PSU_TASK_EFFORT. In addition, the rewritten code is easier to read.Example of code before being rewritten:
Local Rowset &RS_Level2; Local Boolean &TrueOrFalse = (PSU_TASK_RSRC.COMPLETED_FLAG.Value = "N"); For &I = 1 To &RS_Level2.ActiveRowCount &RS_Level2(&I).PSU_TASK_EFFORT.EFFORT_DT.Enabled = &TrueOrFalse; &RS_Level2(&I).PSU_TASK_EFFORT.EFFORT_AMT.Enabled = &TrueOrFalse; &RS_Level2(&I).PSU_TASK_EFFORT.CHARGE_BACK.Enabled = &TrueOrFalse; End-For;Example of code after being rewritten:
Local Boolean &TrueOrFalse = (PSU_TASK_RSRC.COMPLETED_FLAG.Value = "N"); For &I = 1 To &RS_Level2.ActiveRowCount Local Record &TaskEffort = &RS_Level2(&I).PSU_TASK_EFFORT; &TaskEffort.EFFORT_DT.Enabled = &TrueOrFalse; &TaskEffort.EFFORT_AMT.Enabled = &TrueOrFalse; &TaskEffort.CHARGE_BACK.Enabled = &TrueOrFalse; End-For;In the next example, the following improvements are made to the code:
Shorthand is used: &ThisRs(&J) instead of &ThisRs.GetRow(&J).
Eliminated all the autodeclared messages by declaring all the local variables. This action can improve your logic and possibly give you better performance.
Notice the integer declaration. If you know your variables will fit in an integer (or a float), then declare them that way. Runtime performance for Integers can be better than for variables declared as Number.
Fewer evaluation expressions.
Example of code before being rewritten:
Local Row &CurrentRow; &TrueOrFalse = (GetField().Value = "N"); &CurrentRow = GetRow(); For &I = 1 To &CurrentRow.ChildCount For &J = 1 To &CurrentRow.GetRowset(&I).ActiveRowCount For &K = 1 To &CurrentRow.GetRowset(&I).GetRow(&J).RecordCount For &L = 1 To &CurrentRow.GetRowset(&I).GetRow(&J).GetRecord(&K).FieldCount &CurrentRow.GetRowset(&I).GetRow(&J).GetRecord(&K).GetField(&L).Enabled⇒ = &TrueOrFalse; End-For; End-For; End-For; End-For;Example of code after being rewritten:
Local Row &CurrentRow; Local integer &I, &J, &K, &L; Local boolean &TrueOrFalse = (GetField().Value = "N"); &CurrentRow = GetRow(); For &i = 1 To &CurrentRow.ChildCount /* No specific RowSet, Record, or Field is mentioned! */ Local Rowset &ThisRs = &CurrentRow.GetRowset(&i); For &J = 1 To &ThisRs.ActiveRowCount Local Row &ThisRow = &ThisRs(&J); For &K = 1 To &ThisRow.RecordCount Local Record &ThisRec = &ThisRow.GetRecord(&K); For &L = 1 To &ThisRec.FieldCount &ThisRec.GetField(&L).Enabled = &TrueOrFalse; End-For; End-For; End-For; End-For; -
Concatenating a large number of strings into a large string. Sometimes you need to do this.
The simplest approach is to do something like:
&NewString = &NewString | &NewPiece;In itself this is not a bad approach but you can do this much more efficiently using an application class below.
class StringBuffer method StringBuffer(&InitialValue As string); method Append(&New As string) returns StringBuffer; // allows &X.Append⇒ ("this").Append("that").Append("and this") method Reset(); property string Value get set; property integer Length readonly; property integer MaxLength; private instance array of string &Pieces; end-class; method StringBuffer /+ &InitialValue as String, +/ &Pieces = CreateArray(&InitialValue); &MaxLength = 2147483647; // default maximum size &Length = Len(&InitialValue); end-method; method Reset &Pieces.Len = 0; &Length = 0; end-method; method Append /+ &New as String +/ Local integer &TempLength = &Length + Len(&New); If &Length > &MaxLength Then throw CreateException(0, 0, "Maximum size of StringBuffer exceeded(" | &Max⇒ Length | ")"); End-If; &Length = &TempLength; &Pieces.Push(&New); return %This; end-method; get Value /+ Returns String +/ Local string &Temp = &Pieces.Join("", "", "", &Length); /* collapse array now */ &Pieces.Len = 1; &Pieces[1] = &Temp; /* start out with this combo string */ Return &Temp; end-get; set Value /+ &NewValue as String +/ /* Ditch our current value */ &Pieces.Len = 1; &Pieces[1] = &NewValue; /* start out with this string */ &Length = Len(&NewValue); end-set;Use this code as follows:
Local StringBuffer &S = create StringBuffer(""); .... &S.Append(&line); /* to get the value of string simply use &S.Value */