10.26.05

Java Bytecode BCEL vs ASM

Posted in PL Research at 3:25 pm by site admin

My research on the NextGen Generic Java Compiler has progressed on to supporting mixins. A Mixin is a parametric class that extends its parametric type parameter, for example “Mixin extends T”. One advantage of Mixins is that they provide an abstract mechanism to specifiy uniform class extensions. For example I can define a class TimeStamped<T> that can extend any given class, provide additional functionality, but still preserve the same functionality as its parent.

One of the most interesting issues with Mixins in Java concerns what kind of mechanism is used to instantiate Mixins. In Java, there is no uniform way to instantaniate classes. As a result, it is not straightfoward to create a Mixin that satisfies all the possible constructor types in its super class. In otherwords, the set of constructors must be determined based on its parent class.

Now with that basic introduction, I would like to discuss how I create a Mixin instantiation. Basically, I take a Mixin template and instantiate with respect to its parent class. Wihle this process is a straightfoward “fill-in the holes”, the main difficulty I have encountered is preserving the constructor methods. What I needed to do was create a constructor in the sub class for each constructor in the parent. In addition to “bridging”, the constructor must initialize any additional state from the template. Now to do this I would normally use BCEL. However, I know development on BCEL has waned. So I did some research and discovered that it is kind of in maintence mode. In the mailing list I found a Comparison between ASM and BCEL, as well as a few other comparisons between ASM, BCEL, JavaAssist, etc. On the ASM homepage, they boast a 700% performance gain over BCEL. This seemed too good to be true, so I decided to test the performance of ASM vs BCEL in my task of creating Mixins. I am providing a copy of my source asm_vs_bcel.tgz.

Now my simple test shows that ASM is about 350% faster than BCEl. My test does the following:
1. read both the Mixin template and the new super class (used for the mixin)
2. Parse out the available constructors in the parent
3. Use the init method found in the mixin as a template to generate the appropriate ‘bridge’ constructors found in 3. After each super call, I then append all the remaining initialization necessary for the mixin (found in the mixin’s init method)
4. Write out the newly instantiated class.

The bcel code follows a very procedural methodology. While there are some annoyances resulting from using JavaClass vs ClassGen and Method vs MethodGen, a top-down read can easily explain the method.

Now the ASM code is different. At first the ASM Tutorial did not seem to stick. It is however, very correct. The two intersting points of ASM are:

* ASM works through a series of visitors.
* ASM follows a SAX model for traversing a java class.

So a ClassVisitor works through a series of sequential calls:

visit [ visitSource ] [ visitOuterClass ] ( visitAnnotation | visitAttribute )* (visitInnerClass | visitField | visitMethod )* visitEnd.

Thus to perform any code manipulation, it is necessary to define a new ClassVisitor (and probably MethodVisitor). The ASM developers have conviently provided a ClassAdapter and MethodAdapter to faciliate development. Now the use of Visitors in Java is not very new. What is kinda weird is how the SAX-ish design of ASM plays in to everything. For example, to parse a class, the class reader must accept a subclass a ClassVisitor, namely ClassNode. ClassNode represents a class tree in ASM. Naturally, ClassNode also has an __visit__ method for ClassVisitors.


ClassReader super_cr = new ClassReader(super_class);
ClassNode cn = new ClassNode();
super_cr.accept(cn, true);

And then to add a method to parsed tree, I follow a similiar pattern:

newMethod.accept (cn);

Check out the provided source code if you are interested in how ASM code and BCEL code compare, or simply need an example how to write a small class transformation using either of these two languages. For my purposes, ASM is faster and smaller and thus is my choice for the job. I will however, be performing future metrics comparing ASM, BCEl, and also down and dirty manual byte hacking for the purpose of changing the Constant Pool. The issue of course, is that ASM does not provide direct access to the constant pool. The ASM SAX model seems cumbersome, and unintuitive, in this case. I leave this task for another day.

2 Comments »

  1. Abdullah Kauchali said,

    December 21, 2006 at 9:23 am

    Hi James,

    Thanks for this article. Exactly what I was looking for.

    I am battling to get ClassReader to instantiate with the bytearray constructor:

    ClassReader super_cr = new ClassReader(bytesofClass);

    where bytesofClass is a java.lang.Class converted to bytearray.

    Throws an error:

    java.lang.ArrayIndexOutOfBoundsException: 49
    at org.objectweb.asm.ClassReader.(Unknown Source)

    Any suggestions will be greatly appreciated!

    Kind regards,

    A

  2. Eugene Kuleshov said,

    April 19, 2007 at 7:40 pm

    Abdullah, why don’t you ask your question in that ASM project mailing list? The error you are seeing is usually mean that bytearray you are passing to the ClassReader is either invalid or incomplete.

Leave a Comment

Java Bytecode BCEL vs ASM

Posted in Unfiled at 3:06 pm by site admin

My research on the NextGen Generic Java Compiler has progressed on to supporting mixins. A Mixin is a parametric class that extends its parametric type parameter, for example “Mixin extends T”. One advantage of Mixins is that they provide an abstract mechanism to specifiy uniform class extensions. For example I can define a class TimeStamped that can extend any given class, provide additional functionality, but still preserve the same functionality as its parent.

One of the most interesting issues with Mixins in Java concerns what kind of mechanism is used to instantiate Mixins. In Java, there is no uniform way to instantaniate classes. As a result, it is not straightfoward to create a Mixin that satisfies all the possible constructor types in its super class. In otherwords, the set of constructors must be determined based on its parent class.

Now with that basic introduction, I would like to discuss how I create a Mixin instantiation. Basically, I take a Mixin template and instantiate with respect to its parent class. Wihle this process is a straightfoward “fill-in the holes”, the main difficulty I have encountered is preserving the constructor methods. What I needed to do was create a constructor in the sub class for each constructor in the parent. In addition to “bridging”, the constructor must initialize any additional state from the template. Now to do this I would normally use BCEL. However, I know development on BCEL has waned. So I did some research and discovered that it is kind of in maintence mode. In the mailing list I found a Comparison between ASM and BCEL, as well as a few other comparisons between ASM, BCEL, JavaAssist, etc. On the ASM homepage, they boast a 700% performance gain over BCEL. This seemed too good to be true, so I decided to test the performance of ASM vs BCEL in my task of creating Mixins. I am providing a copy of my source asm_vs_bcel.tgz.

Now my simple test shows that ASM is about 350% faster than BCEl. My test does the following:
1. read both the Mixin template and the new super class (used for the mixin)
2. Parse out the available constructors in the parent
3. Use the init method found in the mixin as a template to generate the appropriate ‘bridge’ constructors found in 3. After each super call, I then append all the remaining initialization necessary for the mixin (found in the mixin’s init method)
4. Write out the newly instantiated class.

The bcel code follows a very procedural methodology. While there are some annoyances resulting from using JavaClass vs ClassGen and Method vs MethodGen, a top-down read can easily explain the method.

Now the ASM code is different. At first the ASM Tutorial did not seem to stick. It is however, very correct. The two intersting points of ASM are:

* ASM works through a series of visitors.
* ASM follows a SAX model for traversing a java class.

So a ClassVisitor works through a series of sequential calls:


visit [ visitSource ] [ visitOuterClass ] ( visitAnnotation | visitAttribute )* (visitInnerClass | visitField | visitMethod )* visitEnd.


ClassReader super_cr = new ClassReader(super_class);
ClassNode cn = new ClassNode();
super_cr.accept(cn, true);

Leave a Comment

order generic Flomax online download zen v plus software discount mountain software buy generic Prednisone online windows xp live eddition pdf creator prtable purchase online prescription Lasix without buy autocad software accounting software pricing sas software clinical trials price cheap wp x3 software cheap internet security software ipaq software downloads buy rtg software final cut software download student price language software download software for making price comparison chart buy software at academic prices discount financial software pda software download online purchase prednisone 40 mg Lasix diskwarrior software download xenical online perscription xenical non rx fedex overnight free Zithromax non prescription for next day delivery Maxalt espana order generic Maxalt Zithromax cheap mexican where to purchase Valtrex no prescription no fees buy Valtrex from india no prescription valtrex with fedex valtrex propecia online gt buy valtrex tablets (no prescriptions needed for Buspar|buy Buspar with no prescription|online pharmacies Buspar|Buspar cheap|buy Buspar without rx|purchase rx Buspar without|Buspar purchase online|purchase Buspar online without rx|purchase Buspar free consultation|buy Buspar Online|buy Buspar american express|buy Buspar Online|buy cheap Buspar with dr. prescription|Buspar side effects|fedex Buspar without priscription|overnight Buspar without a rx|order cheap overnight Buspar|Buspar toronto|uk order Buspar|Buspar no doctors prescription|Buspar mexico|Buspar order|no prescription Buspar with fedex|order generic Buspar|buy Buspar without rx from us pharmacy|prezzo Buspar|Buspar 10mg|Buspar from canada|purchasing Buspar without a script|buy Buspar australia|purchase Buspar visa without prescription|online purchase Buspar|buy Buspar no perscription cod|buy Buspar drugs|buy Buspar with visa|buy Buspar without rx needed|buy Buspar without prescription|buy Buspar no prescription low cost|purchase buy Buspar in the uk diskeeper2010 pro premier can you buy synthroid from boots buy cheap Nolvadex cod buy synthroid synthroid mumbai generic Nolvadex online buy Nolvadex in internet mastercard in Kentucky Stanton buy Accutane with without rx Accutane 20mg mexico delivered zithromax purchase zithromax without prescription pay cod zithromax overnight delivery fed ex buy zithromax paypal without rx cheap zithromax next day shipping overnight zithromax without a rx zithromax No Prescription Overnight COD Delivery buy Nolvadex 10 mg buy zithromax overnight fedex zithromax Cheap C.O.D buy 20mg Nolvadex fast delivery Nolvadex without persription where can i buy Nolvadex without a rx free Nolvadex purchase Nolvadex pay pal online without rx order Paxil cheap overnight cheap Paxil no rx buy Paxil online cod order Paxil amex online without prescription Paxil online overnight delivery cod Paxil from canada Paxil non rx fedex overnight free buy discount Paxil online buy Paxil mastercard buy Paxil without rx Paxil u.p.s shipping cod order cheap overnight Paxil purchase cheap Paxil cod free fedex cheapest Paxil available online how to order Paxil online without a prescription purchase Paxil without rx needed buy prednisone amex online without rx buy prednisone 40 mg on-line prednisone delivered overnight fedex prednisone overnight without a rx purchase prednisone without prescription to ship overnight watson prednisone cod overnigh how to buy prednisone online without a rx prednisone cheap overnight fedex buy no perscription prednisone prednisone no rx needed where to buy generic prednisone online without a rx online accutane buy 40 mg prednisone fast delivery prednisone with cod purchase cheap Flomax online buy Lasix pay cod apple itunes download software reader rabbit math 6 9 discount software non generic Nolvadex no prescription Lasix buy cod Flomax buy generic Crestor canada where can i buy Orlistat without prescription order generic finpecia buy generic Orlistat pills Orlistat online purchase where can i buy Valtrex without a rx no prescription Orlistat buy maxalt without rx from us pharmacy buy genuine Maxalt online buy cheap Finpecia under without rx finpecia with overnight fedex purchase finpecia overnight purchase finpecia free consultation buy Valtrex without rx needed buy generic Buspar online Buspirone 10 mg how to buy prednisone online without a rx prednisone no script fedex buy valtrex shipped cod Valtrex online without prescription purchase Valtrex online without prescription Zithromax free consultation fedex overnight delivery buy Zithromax tablets without rx purchase Buspar without prescription buy Arimidex online cheap buy 200 mcg Cytotec online can you buy Cytotec from boots buy 10 mg Crestor teacher software discount Autocad 2011 Demo adobe premiere denoise adobe indesign plugins where to buy Proscar buy windows xp software adobe systems incorporated no rx Crestor purchase accutane 40 mg no scams window in chicago Learning Microsoft Word And Excel Adobe Creative Suite 2.0 buy generic Tamsulosin online adobe buy valtrex without doctor purchase Buspar without prescription needed adobe photoshop elements v6 no prescription finpecia cod delivery online pharmacy Crestor Crestor cheap purchase Crestor without a rx online Fincar tablets adobe acrobat publishing purchase Orlistat online without rx how to purchase Buspar online without rx adobe photoshop fx Microsoft Office 2010 Genuine Windows 7 Premium 64 Bit descargar autocad 9 Flomax wholesale avg interne Windows 7 Drivers purchase Valtrex online with overnight delivery buy cheap Crestor no prescription how to order Prednisone online without prescription purchase cheap prescription Orlistat adobe reader for windows millenium order generic Flomax online Buspar uk buy Buspar legally Valtrex buy on line buy generic Buspar from india getdataback ntfs legacy download buy Flomax online without prescription super cheap software additional license for indesign cs4 ashampoo uninstaller 4 tom tom maps australia cost oem adobe creative suite 5.5 design premium software Flomax rezept mcafee siteadvisor for google chrome buspar fedex download adobe page maker 7.0 portabel buy cheap Valtrex online free consult student discount for norton ghost microsoft visual studio 2005 serial valtrex buy adobe x professional military buy Flomax now portal half price logic pro 9 best Prednisone online pill herman cain wiki adobe premiere pro cs3 ftp buy Tamsulosin online no prescription buy cheap Valtrex line buy Valtrex online cod Bupropion best buy buy Amitriptyline online now order online Zithromax without prescription prednisone without a perscription buy prednisone without rx Valacyclovir 1000 mg cheap adobe flash builder 4.5 premium oem cad electrical editor Program Word 2007 adobe flash cs3 professional video studio Windows Server 2003 64 Bit Microsoft Windows Update Auto Update buy Orlistat epharmacist purchase Valtrex usa cod Buy Windows 7 Upgrade Online buy Valtrex without a rx overnight shipping Microsoft Computers order Buspar overnight prescription Prednisone non rx cheap Zithromax accutane 40 mg fedex no prescription adobe acrobat 8 adobe slide show adobe x pro full microsoft office student and home edition carpentry all furniture coreldraw software ns virtual dj 6.0 oem software distribution license agreements buy in Valtrex uk order rx free Cytotec prednisone on line no perscription purchase Buspar amex online without prescription where can i buy valtrex online without a prescription Discount Microsoft WindowsLightroom 2 Windows 7Ms Office StandardPhotoshop Cs5 UpgradeComputer Monitors For SaleWindows Xp InstallSuite Microsoft OfficeAutocad Version 2007Adobe Acrobat 9.0 Standard DownloadIe8 Download For Windows 7Adobe Paint ShopMicrosoft Service Pack 2Free Download Adobe AcrobatStudent And Teacher EditionManage ImageAdobe Acrobat 7 Pro DownloadVista Home Premium To Windows 7 UltimateWindows 7 Home Premium Upgrade OemAdobe Creative Suite 5 Master Collection Student And Teacher EditionBuy Adobe Photoshop Lightroom 3Ms Office 2010 Home And StudentCompare Photo SoftwareMicrosoft Office Word Viewer 2010Windows 7 Upgrade Student Discount ProfessionalWindows 7 Updates DownloadCreative Suite WebAdobe Reader VistaMicrosoft Windows 7 Home Premium Upgrade 64 BitPhotoshop 2Ie8 Download For Windows 7Photoshop 2009Suite Microsoft OfficeMicrosoft Office 2007 VersionUpgrade Windows Vista To 7Autocad Lt 2010Autocad 2010 Best PriceDownload Acrobat Reader 8 purchase Valtrex money purchase Valtrex side effects buy Buspar without a prescription overnight delivery buy Valtrex c o d what does Valtrex look like buy Proscar amex Proscar buy cod Cytotec prices buy Cytotec money buy where can i buy Prednisone online buy Prednisone no prescription Proscar online no rx overnight buy Cytotec online from canada buy Cytotec over the counter cod overnight buying finpecia online without rx buy finpecia no visa online without prescription buy finpecia us Buy Finpecia fda approved canada windows xp codec audio open in new window html microsoft lifecam discount locked out windows xp buy finpecia cheap software dreamweaver 8 bank software discount tax act software dreamweaver cs3 serial code window frosting buy cheap online pharmacy prednisone no prescription Lasix with fedex dscount software prices roxio purchase no perscription Cytotec victorian window windows 7 wallpaper dreamweaver validate Rosuvastatin no prescription overnight online Orlistat purchase buy Zithromax discount windows antique windows hacker microsoft window update recording software