Solved

Sax Parser / Performance Question...

Posted on 2003-11-27
11
577 Views
Last Modified: 2013-11-23
I've been working on a card game applet.  This kind of sent me off on a tangent; I was working on the GUI, and I had nice data/code separation.. So I thought, hey why not store the data in a file?  Long story short, I've come up with a neat little swing markup language.  I had originally implemented it with an org.w3c.dom.DocumentBuilder.  Of course, once you start reading the JAXP spec, you can't help but notice how SAX is ending world hunger..</sarcasm>  Anyhow, I've gone to a validating SAX parser, complete with DTD..

I have 25 different tags, 10 of which are container tags; and 15 different attribute types...

As you might imagine, cramming all these into 2 methods - startElement() and endElement(); it wasn't pretty...  I was doing all kinds of string compares - in a linear fashion.  I wanted to go to a binary search, only I had to do something different (but similar)for each of the 25 tags.  My programming teacher suggested I use a hashing algorithm to elimate the string compares altogether..

So much for long story short!!!

Basically, I've created 2 simple nested interfaces:
protected interface Builder { Object make( Attributes attr ); }
protected interface Packager { void make( Tag t, SimpleStack temp ); }

Tag is just an Object and an int hashcode; SimpleStack is just that (32 lines of code). The idea is that I have an array for each interface - 2 function tables.  This enables me to do a binary search of the hashcode of each tag.  The result of the binary search indexes the function table, and the appropriate Builder/Packager is called.

Only now, my class is broken up into 40 files!!! arrgghh.  It creates a .class file for each interface in the table - XMLWindowFactory$23.class and so forth..

Please tell me there's a way to trick the compiler into leaving my tables in one file.. Like putting the interfaces in a Vector, or using a nested class instead...  Something...

A mouselistener doesn't create an extra .class file does it??  I'm basically doing the same thing..  Dang.. guess it does...

Think the binary search is worth the extra files??
0
Comment
Question by:ShawnCurry
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 9830576
You can package your classes up into a jar file...

Unfortunately, you can't stop java producing a class file for each inner/anonymous class :-(

But by making a jar out of them, you will only have to distribute one file instead of 40...
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9830577
> Think the binary search is worth the extra files??

It does sound nice :-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9830588
If you create a manifest file like this:

----------

Manifest-Version: 1.0
Main-Class: classname

----------

save it as "MyManifest.txt", then make the jar file by:

jar -cvfm Cards.jar MyManifest.txt classes

(assuming all your classes are in a "classes" folder, and you want to make a jar file called "Cards.jar")
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9830589
you can then run the application by:

java -jar Cards.jar
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9830765
Have a look at the powerful commons Digester. This can take an xml file of Cards and return you a collection of Java objects

http://jakarta.apache.org/commons/digester/
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 92

Expert Comment

by:objects
ID: 9833703
one class file per class, dats the rules :)
0
 
LVL 3

Author Comment

by:ShawnCurry
ID: 9839036
I apologize for the long explanation; I think most of it was necessary to see where I'm coming from..

I guess my real question for you experts is whether or not you think the binary search is worth the extra files...

Tim Yates..  Thanks for the nice explanation!  But uhh..  I already know all about jar files!!

I think having all these anonymous classes is increasing the size of the overall class (in bytes) significantly.  Correct me if i'm wrong, but there must be some kind of header information stored with each class file, right??  It creates 35k of class files from 18k of java code.

For example:

new Builder() {//combo
      public Object make( Attributes attr ) {
            JComboBox out = new JComboBox();
            ctrl[2].add( out );
            return out;
      }
},
new Builder() {//glass                                <-- THIS ONE IS 412 BYTES!!
      public Object make( Attributes attr ) {
            return null;//no object is created for glass pane
      }
},
new Builder() {//label
      public Object make( Attributes attr ) {
            JLabel out = new JLabel();
            for( int i = 0; i < attr.getLength(); i++ )
            {
                  if( attr.getQName(i).hashCode() == aname[13] )//title
                        out.setText( attr.getValue(i) );
            }
            ctrl[3].add( out );
            return out;
      }
},

Now, given the fact that many of these classes have only 3-4 lines of code to run, and the fact that I'm searching an array of 25 hashcodes (this will definately expand); is it worth it??  Keep in mind - this is an APPLET...  I'm trying to optimize the download performance as well (*I* have a dialup modem)

Granted, this question is probably better suited for the "Philosophy and Religion" "technology channel", but a little expert opinion would be greatly appreciated..

PS:  If someone were to come up with a little o-notation breakdown, bonus points would definately be involved!! :)

Thanks,
Shawn
0
 
LVL 92

Expert Comment

by:objects
ID: 9839902
jarring the files will decrease the size.
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 9839918
If you are using an inner class with the same functionality in >1 spots then you could make it a class in it's own right to avoid multiple copies of the same class being created.
0
 
LVL 92

Expert Comment

by:objects
ID: 9840257
0
 
LVL 3

Author Comment

by:ShawnCurry
ID: 9840323
I think that's what it'll utimately boil down to.  CheckBoxes and RadioButtons are virtually identical(title,tooltip,value); so are Labels,Menus,Menuitems, and Buttons(title,tooltip)...

I've already combined a couple tags...

Hmm...  Oh well..  Guess I'll actually sit down and decide once and for all what my tag/attribute sets will look like before I rewrite this too many more times..

Just added the tooltips, and I thought I'd add Icons and Images.  I think I can sqeeze toolbars in there too...  I really have my work cut out for me!!

Thanks,

Shawn
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now