b518ab296337b8a0ef6ecaa0303dec27.ppt
- Количество слайдов: 55
DEV-21: Optimising Your ABL For Performance Making your code go faster Gus Björklund Wizard, Progress Software Corporation
"More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. ” W. A. Wulf 2 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
“The First Rule of Program Optimisation: Don't do it. The Second Rule of Program Optimisation (for experts only!): Don't do it yet. ” Michael A. Jackson 3 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Things § Why optimise § When to optimise § What to optimise § How to optimise 4 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Why optimise 5 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Why (not) optimise § Optimising is work § Processors get faster and faster § Databases get faster and faster § Disks get faster and faster (and bigger) § Memory gets faster and faster (and bigger) Why spend time optimising when we don’t need to, and you just warned me not to do it ? ? 6 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Why optimise § Performance is never good enough § Can’t ask customer to buy a new machine § Faster machines won’t solve every problem § No machine bigger than the biggest § Smaller machines cost less than big ones § You can put more users on a machine § Better performance improves customer satisfaction But: Don’t do work that is not needed! 7 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
When to optimise 8 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
When to optimise § After you have • • • 9 spent enough time thinking chosen the right algorithms working code measured performance of your application determined you have a problem DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
What to optimise 10 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
The Pareto principle § 80 % of the consequences stem from 20 % of the causes 11 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
"We should forget about small efficiencies, say about 97% of the time: premature optimisation is the root of all evil. Yet we should not pass up our opportunities in that critical 3%. ” D. E. Knuth You have to find that 3 % ! 12 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Finding the 3 % 13 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Measure § ALWAYS measure effects § Measure more than once • different data gives different results § Sometimes the “improvements” you make won’t be worthwhile or will make things worse. • Take those out § You need a performance regression test suite 14 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
“Measure twice, cut once” carpenter’s adage 15 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Measuring performance § 4 GL profiler § a watch § the time, timex commands § instrument your code § log files 16 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
The 4 GL profiler § See $DLC/src/samples/profiler/readme. doc § Get the “profiler control tool” from PSDN § No code changes needed § Tells you: • Time spent in various parts of your program • Call hiearchy • Number of times each part is executed § Learn how to use it! § Profile your application 17 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Measuring with etime() def var start. Time as int 64 no-undo. def var end. Time as int 64 no-undo. start. Time = etime(false). for each customer: end. Time = etime(false). display end. Time - start. Time. 18 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Measuring with datetime def var start. Time as datetime no-undo. def var end. Time as datetime no-undo. def var elapsed. Time as int 64 no-undo. start. Time = now. for each customer: end. Time = now. elapsed. Time = interval (end. Time, start. Time, "milliseconds"). display elapsed. Time. 19 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
How to optimise 20 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
How to optimise § Start with working code and tests § You DON’T know where the problem is § The problem will move § Try one thing at a time § Remove changes that didn’t work § Know when to stop 21 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
How to optimise: various tips and techniques in no particular order 22 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Comments and white space do not impede performance. What is the most important thing? 23 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Which algorithm is faster? def var n as integer no-undo. def var sum as integer no-undo. sum = 0. do n = 1 to 1000: sum = sum + n. end. 24 DEV-21: Optimizing Your ABL for Performance n = 1000. sum = (n * (n + 1)) / 2. © 2007 Progress Software Corporation
Avoid Unnecessary XML § Parsing XML uses many cpu cycles § XML is a memory hog § XML documents are often big 25 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Clean your room § Delete dynamic objects and widgets you don’t need anymore • dynamic object tracker • session: system: first-procedure, etc § Use widget pools § Get rid of temp tables you don’t need § Get rid of objects you don’t need 26 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Temp tables § Pass BY-REFERENCE or pass handle § Raise -Bt • -Bt sets number of temp-table buffers • default is low § Use EMPTY TEMP-TABLE when possible, § instead of FOR EACH tt: DELETE tt. END. Use MIN-SCHEMA-MARSHAL or NOSCHEMA-MARSHAL attribute when passing TTs to/from app servers 27 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Use NO-UNDO Variables def var foo as integer no-undo. def var bar as character no-undo. (would have been nice if this had been the default behaviour, but is not) 28 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Group assigns assign i=3 j=5 c = “abcdef”. Yes 29 DEV-21: Optimizing Your ABL for Performance i = 3. j = 5. c = “abcdef”. No © 2007 Progress Software Corporation
BUFFER-COPY beats ASSIGN BUFFER-COPY source TO target options EXCEPT list USING list NO-LOBS 30 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Avoid redundant code § Do calculations once and reuse result wherever possible j = some. Function (). do i = 1 to 10: stuff end. 31 DEV-21: Optimizing Your ABL for Performance do i = 1 to 10: stuff j = some. Function (). end. © 2007 Progress Software Corporation
Redundant code do n = 1 to some. Function. Or. Other(): … stuff. end. i = some. Function. Or. Other(). do n = 1 to i: … stuff. end. 32 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Eliminate unnecessary code § Code that isn’t there executes in zero time “If we wish to count lines of code, we should not regard them as lines produced but as lines spent. ” Edsger Dijkstra “Inside every large program there is a small program trying to get out. ” Sir Tony Hoare 33 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Loop hoisting do I = 1 to 50: if expression 1 then run bar(). else do: if expression 2 then do: run foobar(). end. else run foo(). end. 34 DEV-21: Optimizing Your ABL for Performance if expression 1 then do I = 1 to 50: run bar(). end. else do: if expression 2 then do I = 1 to 50: run foobar(). end. else do I = 1 to 50: run foo(). end. © 2007 Progress Software Corporation
DO instead of REPEAT sum = 0. repeat n = 1 to 10000: sum = sum + n. end. 63 milliseconds 35 DEV-21: Optimizing Your ABL for Performance sum = 0. do n = 1 to 10000: sum = sum + n. end. 57 milliseconds © 2007 Progress Software Corporation
Minimise block nesting IF expression THEN statement. instead of IF expression THEN DO: statement. END. 36 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Use CASE instead of nested IF CASE status: WHEN 1 THEN DO: END. WHEN 2 THEN DO: END. WHEN 3 THEN DO: END. OTHERWISE DO: END. 37 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Increase sort buffers § Set -TB to 31 (buffer size) § Set -TM to 24 (number of merge buffers) 38 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Avoid deep object hierarchies § constructors have to be run for each class you § inherit from destructors have to be run for each class you inherit from CLASS A: END CLASS. new (D) runs 4 constructors CLASS B INHERITS A: END CLASS C INHERITS B: END CLASS D INHERITS C: END CLASS. 39 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Minimise creation of subprocesses § Creating a process is one of the most expensive system calls 40 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Duff’s Device switch (count % 8) { case 0: do { *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; } while ((count -= 8) > 0); } 41 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Use sequences § Does not require locking records § Can cause gaps in numbering though § You can use UUID’s for unique keys instead of sequences § 10. 1 B has 64 -bit sequences 42 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Queries § Avoid unindexed CAN-FIND § Use word indexes for status indicators § Avoid queries on boolean values § Careful with OR predicates in WHERE § § clauses Add more indexes Get rid of unused indexes COMPILE XREF Exchange sessions on indexing 43 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
HIDE screen data while calculating § See recent PEG thread on Lock. Window. Update • search for “window update” to find it. 44 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Avoid CONNECT § CONNECT is somewhat slow CONNECT at runtime requires r/w access to database files (insecure) 45 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Propath § use -q option • propath search is done only once § Keep propath as short as possible • The more entries in propath the longer the searches can take § Order entries by frequency of use • Put most frequently used in front 46 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Shared Procedure libraries § Use when same code run by multiple § § sessions ON SAME MACHINE Whole library is mapped in one call Saves memory due to single copy • Memory can then be used to improve performance – Bigger database buffer pool – More users – Can reduce paging (if you are) § Marginally faster due to opening fewer. r files 47 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Cost of RUN type relative time (ymmv) internal procedure 1 persistent procedure 1. 5 external procedure 2. 3 web service invocation ~ 1, 000 to 15, 000 ( ~ 5 microseconds) web service invocation time is highly variable and depends on: processor speed network speed number of hops distance time of day of week load on server which wsdl and soap options are used 48 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
More stuff § Compile with -min-size option § Use shared memory database connections § when possible (avoid networking) Single user mode usually isn’t fastest • compared to multi-user with 1 user 49 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Measure § Sometimes the changes you make won’t be § § worthwhile ALWAYS measure effects Measure more than once • with different data § Batch jobs should always write to a log file • Watch for changes in duration over time 50 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Code Reviews § You don’t know everything § You might have overlooked something § There might be a better way 51 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Other things to do 52 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Other things to do § § § Add more memory Simplify your application Tune your database • See “Open. Edge® RDBMS Performance Tuning Made Simple” on PSDN or PEG § Data. Servers • “Building High Performance Applications with the Progress Oracle Data. Server” on PSDN • “Data. Server Best Practices” on PSDN 53 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Where to learn more § The PEG (www. peg. com) § PSDN (psdn. progress. com) § 4 GL Programming Handbook § Past Exchange sessions § Progress course on ABL performance 54 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
Questions 55 DEV-21: Optimizing Your ABL for Performance © 2007 Progress Software Corporation
b518ab296337b8a0ef6ecaa0303dec27.ppt