480 likes | 600 Views
Smaller, Faster and More Efficient Applications - a Micro Focus Developer Tells All. Jeremy Wright, Micro Focus. Contents. Data Code Program Structure Application Structure. Data. Alignment Types typedefs and call prototypes Working-storage, local, or thread-local?. Data - alignment.
E N D
Smaller, Faster and More Efficient Applications - a Micro Focus Developer Tells All Jeremy Wright, Micro Focus
Contents • Data • Code • Program Structure • Application Structure
Data • Alignment • Types • typedefs and call prototypes • Working-storage, local, or thread-local?
Data - alignment • Memory organized into 4 or 8 byte words • Access across word boundaries is slow • at least twice as slow, up to 50 times slower • Access across boundaries gives larger code • 9 extra instructions for 4 byte item on Itanium
Data misalignment example • This comp-5 item count-a is aligned01 grp-item. 03 count-a pic x(4) comp-5. 03 ch pic x. • This comp-5 item count-b is not aligned01 grp2-item. 03 ch2 pic x. 03 count-b pic x(4) comp-5.
Data misalignment - tables • The stride of a table should be a multiple of 4. Pad the table to ensureThis is misaligned01 tab1 occurs 200. 03 addr pic x(4) comp-5. 03 ref pic x(2) comp-5. 03 loc pic x comp-5.
Data misalignment - tables • The stride of a table should be a multiple of 4. Pad the table to ensureThis is aligned01 tab1 occurs 200. 03 addr pic x(4) comp-5. 03 ref pic x(2) comp-5. 03 loc pic x comp-5.03 pic x.
Avoiding misalignment • 01 level items are aligned on 8 byte boundary • Align subordinate data items • 1 byte items anywhere • 2 byte items on 2 byte boundary • 4 byte items on 4 byte boundary • 8 byte and longer items on 8 byte boundary • all data items - not just numeric
Avoiding misalignment • Use the sync phrase and the mfsync directive • Introduced in SX 4.0 • Aligns data items correctly • Potentially changes record layout
Avoiding misalignment - linkage items • call parameters should be 01 level aligned • This ensures parameters passed to subprograms are aligned • Use the NCG directive lnkalign • Allows the NCG to generate optimal code assuming best alignment • calling program MUST call using aligned parameters
Types - Numeric data • Optimal is 4 byte integer comp-5 • Signed or unsigned, but try not to mix in same statement • 1, 2 or 8 byte OK if required • Advantages • Smaller faster code • Required for some APIs • Faster to generate
typedef • Define record structure once • Get the alignment correct • Consistency, reliability, re-use
typedef example 01 drec typedef.03 addr pic x(4) comp-5.03 ref pic x(2) comp-5.03 loc pic x comp-5.03 pic x. 01 cursor-item drec. 01 drt drec occurs 2000. 01 src-tgt. 03 src drec.03 tgt drec.
typedef example contined 01 cursor-item drec. 01 drt drec occurs 2000. move 0 to addr of cursor-item move loc of drt(i) to loc of cursor-item move cursor-item to drt(i + 1)
Call prototypes • Define the interface a sub program expects • Place in copy file - include in the defining sub-program, and any calling program • Define number and type of parameters, and result • Parameter type can be a typedef • Also define value vs reference, and call convention • And also define useful 78 level items • Detect run time problems at compile time
Prototype - example 1 identification division. program-id. “malloc” is external. data division. 01 res pointer. linkage section. 01 amt pic x(4) comp-5. procedure division using value amt returning res. end-program “malloc”.
Prototype - example 2 identification division. program-id. “printdrec” is external. environment division. special-names. call-convention 4 is void. procedure division void using reference drec. end-program “printdrec”.
Where to declare • Local-storage section • Fast, stack-based, thread-safe, memory efficient • Easier to optimize • Working-storage • Fast, initialized, not thread-safe, persistent • Space allocated at program start up • Thread-local-storage section • Slower, initialized, thread-safe, persistent within thread
Contents • Data • Code • Program Structure • Application Structure
Arithmetic • Use operands of same type • Two operand style always efficient • add a to bdivide c into d • Compute statement without divide operation is also efficient
Arithmetic - avoid these where possible • On size error • Multiple targets • Compute involving divide • split statement into a compute and a divide where possible
Subscripting and Reference Modification • Subscripts and lengths should be aligned comp-5 • Keep it simple • Fixed length operation faster than a shorter variable length operation • Do not use checker directive odoslide
Contents • Data • Code • Program Structure • Application Structure
A structured program • Program composed of independent procedures • An initial main procedure finishing with goback • Logical separation of functionality • Use a section as a procedure • Use a paragraph as a label within a procedure
Structure - how • Do: • Use sections • Perform sections • Use goback instead of exit program • Do not: • Use perform thru or perform paragraph • Use inter-section go to • Use alter, segments, OSVS or RM perform-style
Structure - miscellaneous • Coding style can help prevent problems • Only use ‘.’ at end of paragraph • Use scope delimiters - end-if, end-perform • Enforce with checker directivesnoimplicitscope change-message(1227 E)
In line perform • Excellent structured programming mechanism • Keep loop condition simple • Avoid go to out of loop • Code generator optimizes well structured, single exit loops • Easier to understand as well
Contents • Data • Code • Program Structure • Application Structure
Application Structure • Why use sub-programs • Invoking sub-programs • Building libraries and applications
Why use sub-programs • Quicker to check and generate individual sub-programs • Easier to understand and maintain • Logically distinct code and data separated • Use sub-programs in multiple applications
Perform vs. Call • Perform very fast • Acts locally, potentially on all data • Does not take parameters • Call is slower • Called code is external • Takes parameters • Only those parameters are affected
Calls • Use NCG directive litlink • On by default when creating objects • Use call “literal”, or set proc-ptr to entry “literal” • Do not use on overflow or on exception • Use call prototypes. Always supply all the parameters • if address of linkage-parameter = NULL is inefficient
Execution formats - .int to object • .int • slow, portable, dynamically loaded, not shared • .gnt • fast, dynamically loaded, not shared • object code • OS defined format, not directly executable • basis for native executable formats • more efficient than .gnt
Shared object / shared library • Interchangeable terms on Unix • Not an executable, but contains executable code. Bound into address space of process at run time • shared library conventionally used for file with name libmine.so which is included on link command line • shared object conventionally used for code loaded at run time
Executable • Create with cob –x • Features: • Loaded at process start - fixed memory footprint • Code shared by all instances of this executable • Executables can reference shared libraries
Shared library • cob –Z • Features: • Loaded at process start - fixed memory footprint • Multiple Cobol objects (and C and C++) • All entry points visible • must be referenced when linking CSO or executable • Code shared by all instances
Callable shared object ( CSO ) • Create with cob –z • Features: • Loaded at run time - variable memory footprint • Multiple Cobol, C, C++ objects linked into single CSO • Code shared by all instances • Only name of file visible as entry point before CSO is loaded. After loading, all names are visible • Can reference shared library from CSO
Loading a CSO • Ensure LD_LIBRARY_PATH is set correctly • call “name” or call “name.so” • “name.so” must contain entry “name” • “name” is executed • set proc-ptr to entry “name.so” • “name.so” must contain entry “name”, or –e used when CSO built • does not execute code - just loads the CSO
Self contained CSO ( scCSO ) • cob –y • Features: • Combines features of CSO and executable • Loaded by a non Cobol executable • Only one scCSO in a process at a time
Loading a third party shared object • Use LD_PRELOAD environment variable • Not available on AIX • Specify it on the command line when creating a shared library of executable • Create a dummy CSO referencing the shared object in order to dynamically load it • cob –z dummy.cbl converter.so • cob –z dummy.cbl –L/usr/packages/conv -lconverter
Example 1Standalone executable prog1 screen fileio
Example 2Library for common code prog1 prog2 prog2b screen fileio
Example 3Executable, CSOs and library menu prog1 prog2 prog2b screen fileio
Example 4Non-Cobol invoking scCSO 3rd party application menu prog1 prog2 prog2b screen fileio
Review of execution formats • Executable for stand alone code • scCSO for stand alone code invoked by 3rd party • Shared library for utility routines • CSO for dynamic loading
noalternoseg Improves checker speed noqualproc Improves checker speed. Catches use of identical, and confusing, paragraph names noimplicitscope Enforce use of scope delimitersCatch subtle bugs early nocheck Turn off run time checking Some useful checker directives
nocheck Turn off run time checking, including subscript, linkage and divide-by-0 checks lnkalign Assert that linkage items alignedGenerates faster code opt Turn on global optimisations. Generates faster code, but takes longer to generate litlink Ensure calls are linked rather than dynamically bound linkalias Specify aliasing of linkage items Some useful NCG directives
Conclusions • ALIGN your data • Use typedef and call prototypes • Keep code simple • Structure your program • Structure your application