Pre-installed object Zhang Jianzi 01-6-22 03:17:13
In PowerBuilder 5.0, full-compiled code is supported, but there is a shortcomings for the presence of long compilation time and have a large amount of compiled files, so we still use pseudo-compilation methods in many cases, which is to load objects and sources. The PBL file of the code is compiled into a dynamic link library (.pbd) of PowerBuilder. The PowerBuilder's dynamic link library is loaded is a binary representation that matches the source code in the source sequence. At runtime, objects (including functions) in accordance with the principle of "need to call", from .pbd into memory, this makes the number of bytes of executable programs greatly narrowed, and the execution efficiency will increase, and because only those need to use The object is installed instantly into the memory, so that the system needs to be stored more, so the application is faster.
But sometimes we will also find that the user's response speed is slower when performing an open window, especially in the case where the client is configured, especially in this situation. We know that when the program calls a new object, the system is to find this object in each .pbd file, if this object is inherited by other objects, then all the ancestors need to load memory. If the application is very large, this lookup and loading is clearly very time consuming.
Here we introduce a way to pre-installed objects to solve this problem to a certain extent. Pre-installation into the object is the time to change the initial loading of the object, that is to say that the object is loaded when the user does not have the response speed, not when the user needs the object (if the window is turned on). The pre-installed object can significantly improve performance, and this technique is impressed by the user that all objects are accelerated when the user really requires the response speed of the system. The most suitable place to do these pre-installed is in the OPEN event of Application. There is no such function in the PowerBuilder 5.0 development tool, but we can use some simple skills to implement it, which is using a non-visual object.
We should first know: some objects, such as non-visual objects, etc., all loaded into memory, while others, such as functions, only part of the required part. So use a non-visual object may be more faster than using a full-class function, of course, is also related to its size and function.
You don't have to pre-install all objects in the OPEN events, but pre-installed those objects that are most often used as ancestors. The specific approach is to define this non-visualized object into a variable. This non-visual object remains in memory until the app is ended, which makes the app more compact. Another benefit of doing this is that once you define an object's pointer as a global variable, you can use this pointer anywhere in the software, reference this object's constant, functions, and other features.
The specific steps can be made:
Step 1: Create a pre-installed object
Create a user object of a non_visual_object type, which will use the object you selected. In this example, NVO_Object_PRE_LOADER is used.
In this object, an array of object arrays that build a PowerObject is called IPO_PRE_LOADED_OBJECTS []. PowerObject object is the highest level object in the Pow-Erbuilder Object (see "PowerBuilder Object-Oriented Programming"), so it can be assigned to any PowerBuilder standard or custom object. Also create an integer variable as an index of an array, we call it II_IDX and initialize it to 0. These two variables are example variables.
Private: / * Limit access to these two variables * /
/ * We assume that the number of objects pre-installed is not more than 10, and of course the developer can adjust * /
PowerObject IPO_PRE_LOADED_OBJECTS [10]
INTEGER II_IDX = 0
Note that we should predefine the size of the array in advance, which allows this object to preserve memory and can run faster when it is pre-installed.
Step 2: Create a pre-installed function
Now create a user object function called NVOF_PRE_LOAD_OBJECT. This function has a parameter: APO_OBJECT, it is also a PowerObject type. The code of the function is as follows.
/ *
Function: nvof_pre_load_Object
Function: to pre_load offen used Ancestor Objects
Parameters: Power Object APO_Object
Return value: Integer
1: Success, -1: Failure
* /
II_IDX
/ * Assignment to the object to be assigned to this array * / ipo_pre_loaded_Objects [II_IDX] = APO_OBJECT
/ * Introduction is successful * /
IF isvalid (IPO_PRE_LOADED_OBJECTS [II_IDX]) THEN
Return 1
Else
Return -1
END IF
Step 3: Create a user event that can be placed in call user object code
Establish a code that can be placed in the pre-installed object call on this object. We define the use of a "declaration" event that will be triggered in the CONSTRUctor event of the object. We can name this user event NVO_UE_DECLArations and place the following code into the constructor event of the object.
This.Post Event ("NVO_UE_DECLARATIONS")
In the NVO_UE_DECLATION event you can put into the call to the pre-installed object.
Step 4: Pre-installed objects
This code exists in the NVO_UE_DECLATION event. Maybe your application requires more or less pre-installed objects, and we only assume that there are several common ancestors object classes W_WindowBase, UDW_DataWindow, Uo_USEROBJECTBASE, and M_Menubase. These objects represent our window classes, user object data window, user object classes, and ancestral objects of the menu class, respectively.
The code in the NVO_UE_DECLARATON event is as follows:
/ * Statement points to local variables of these objects * /
Window Lwindow
UserObject Luo
DataWindow LDW
Menu Lmenu
/ * For each object that needs pre-installed, use the CREATE statement to create an instance of the object, and call the pre-installed function to save this instance in memory * /
/ * Create an instance of a window base class * /
LWindow = CREATE W_WINDOWSBASE
NVOF_PRE_LOAD_OBJECT (LWINDOW)
/ * Create an instance of the user object base class for a DataW-Indow * /
LDW = CREATE UDW_DATAWINDOWBASE
NVOF_PRE_LOAD_OBJECT (LDW)
/ * Create a base class instance of a user object * /
Luo = UO_USEROBJECTBASE
NVOF_PRE_LOAD_OBJECT (Luo)
/ * Create a menu instance * /
LMENU = CREATE M_MENUBASE
NVOF_PRE_LOAD_OBJECT (LMENU)
Generally speaking, the object that is dynamically called is those that are called by string variables, which will not be included in the .exe file. For example, if you open a window with Open (MyWIN, "My_Window), my_window window is not automatically included in your EXE file. If compiled into a .pbd file, it will be included in .pbd. But if you are pre-installed into this object with this method, the window will be included under .pbd and .exe compilation technology.
Step 5: Initialize pre-installed objects
In order to enable the object to enter the memory, the last step is left. We must put the pre-installed object itself into memory. You can choose to do this anywhere before you start working. Generally commonly used in the OPEN event of Applica-Tion. The code required is as follows:
ANVO_Object_pre_loader = create nvo_object_pre_loader.
This program will establish this non-visualized object, which will stimulate the statement of the object, thereby pre-installed into each object.
The last minute, please note, don't forget to add a statement such as DESTROY ANVO_OBJECT_PRE_LOADER in your Appli-CATION, otherwise this application will cause a so-called memory vulnerability (Memory Leak).