Bug 142335 - Perf Fix for: org.eclipse.swt.internal.image.PngHuffmanTable.generateTable
Summary: Perf Fix for: org.eclipse.swt.internal.image.PngHuffmanTable.generateTable
Status: CLOSED FIXED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.2.1   Edit
Assignee: Carolyn MacLeod CLA
QA Contact:
URL:
Whiteboard:
Keywords: contributed, performance
Depends on:
Blocks:
 
Reported: 2006-05-17 16:17 EDT by Bill Tracey CLA
Modified: 2007-06-05 15:23 EDT (History)
3 users (show)

See Also:


Attachments
Uses shellsort instead of bubblesort, internally implemented (5.39 KB, patch)
2006-07-19 14:52 EDT, Zachary Calvert CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Bill Tracey CLA 2006-05-17 16:17:22 EDT
org.eclipse.swt.internal.image.PngHuffmanTable.generateTable has a bubble sort of the Huffman table in the top of the routine.    Profiling an application opening a  window with a moderate number of PNG images (around a dozen) shows this method to be accounting for 43% of the the instructions executed in the open window scenario.  Changing the code to sort via java.util.Arrays.sort()  results in reducing the instruction counts for this method to less than 5% of the scenario total.  (email me if you want the chaged code) For the specfic scenario I'm looking at (window w/ about a dozen PNGs) the improved code is noticeably (to an end user) quicker.
Comment 1 Carolyn MacLeod CLA 2006-05-18 11:55:24 EDT
Ken, is java.util.Arrays.sort() CLDT compliant?
Comment 2 Ken Walker CLA 2006-05-19 12:08:19 EDT
No, java.util.Arrays is not part of CLDC 1.0 or CLDC 1.1.  I'll see if I can find someone to look at your sort algorithm.
Comment 3 Carolyn MacLeod CLA 2006-05-19 14:18:11 EDT
Thanks, Ken - much appreciated!

Bill, SWT must be CLDC compliant, so unfortunately, we cannot use java.util.Arrays.sort(). Perhaps we can improve out time, however. Thanks very much for pointing this out.
Comment 4 Bill Tracey CLA 2006-05-19 14:32:44 EDT
Can you detect if java.util.Arrays is available and use it if it is?  If not I'd strongly advocate using a better sort Algorithm -- this make a noticiable difference for UI panels with a moderate number of PNG files. 

Regards,

Bill 
Comment 5 Carolyn MacLeod CLA 2006-05-19 15:22:40 EDT
We're going to go the "better algorithm" route.

In comment #3, I meant to say "Perhaps we can improve our time... by using a better sort algorithm".

That way, we fix it for everybody.
Comment 6 Zachary Calvert CLA 2006-07-19 14:52:03 EDT
Created attachment 46527 [details]
Uses shellsort instead of bubblesort, internally implemented

In researching the typical number of elements to be sorted in the Huffman table, I chose to use Shellsort algorithm.  In doing some testing, it reduced load time of a 13.3 MB image by about 2 seconds.  This class was modified for the org.eclipse.swt.win32.win32.x86 fragment and I have not tested it in the org.eclipse.swt plugin.

This is the first time I have created an Eclipse patch, please let me know if I configured the patch incorrectly.  I would like to get it right.
Comment 7 Carolyn MacLeod CLA 2006-07-21 15:09:40 EDT
Released to HEAD 20060721.

Thank-you for the patch - it reduces load time for a typical PNG by about 3.6%

I took the shellsort method and put it inline in generateTable.

The patch was almost fine, but next time please do not reformat the code
(i.e. don't change tabs into spaces or modify where carriage returns go).
Also, I deleted the new 'max' & 'min' - they were not referenced anywhere.

Thanks again!
Comment 8 Bill Tracey CLA 2006-07-25 11:33:23 EDT
I went back and measured the shellsort fix and compared it to the original implementation and the java.util.Arrays.sort implementation with the app that caused me to write this defect. 

The scenario contains 101 calls to PngHugffmanTable.generateTable()  Avg instructions/call are: 

Original Implementation:  4381 k 
ShellSort patch:           497 k 
java.util.Arrays.sort      294 k 

The ShellSort patch is about 11% of the original, but java.util.Arrays.sort is almost twice as quick shellsort.

Would we consider adding code to detect if java.util.Arrays is available and use it if so? 
 
Comment 9 Silenio Quarti CLA 2006-09-07 15:52:08 EDT
backport to 3.2.1
Comment 10 Bill Tracey CLA 2006-10-03 13:09:14 EDT
Test's good for me on 3.2.1