Passing small CHAR parameters to functions
Function parameters of most data types are passed by value (i.e. the value of the caller variable is copied on the stack, and then copied back into a local variable of the called function.) When large data types are used, this can introduce a performance issue.
For example, the following code defines a logging function that takes a CHAR(2000) as parameter:
FUNCTION log_msg( msg )
DEFINE msg CHAR(2000)
CALL myLogChannel.writeLine(msg)
END FUNCTION
If you call this function with a string having 19 bytes:
CALL log_msg( "Start processing..." )
When doing this call, the runtime system copies 19 characters on the stack, calls the function,
and then copies the value into the local variable. Since the values in CHAR
variables must always have a length matching the variable definition size, the runtime system
fills the remaining 1981 positions with blanks. As result, each time you call this function, a
2000 characters long variable is created on the stack.
By using a VARCHAR(2000)
(or a
STRING
) data type in this
function, you optimize the execution because no trailing blanks need to be added.