Title: Slow performance of XalanTransformer.transform in DLL

I am writing a JNI bridge which uses Xalan-C++ to speed up the XSLT processing in a Java application.  Everything is functional, but the XalanTransformer.transform method is perform exceptionally slow (slower than a native Java implementation using Xalan-J).  Strangely, I am copying the code from the XalanTransformer sample project, but when I use the packaged executable from the command line on the same file and stylesheet the performance is lightning fast.  The only major difference between my code and the sample project is that instead of compiling as an executable I am compiling as a DLL.  Could this be the source of my performance hit?  My DLL will convert a 1MB file in 44secs, the executable takes about .2secs.  What else might it be?

Code:

#include <xalanc/Include/PlatformDefinitions.hpp>


#include "NativeTransformer.h"
#include <jni.h>
#include <time.h>

#include <xalanc/XalanTransformer/XalanTransformer.hpp>

#if defined(XALAN_CLASSIC_IOSTREAMS)
//#include <iostream.h>
#include <strstream.h>
//#include <fstream.h>
#include <sstream.h>
#else
//#include <iostream>
#include <strstream>
//#include <fstream>
#include <sstream>
#endif

#include <xercesc/util/PlatformUtils.hpp>

//! Implementation of JNI method getTransform(String input, String xslt)
//! This method is declared and called from the NativeTransformer java class
//! @param env Pointer to JNI method table
//! @param obj Calling class instance object
//! @param input Input XML string or url string
//! @param xslt XML string of stylesheet for transform
//! @returns The transformed XML as a string
JNIEXPORT jstring JNICALL Java_com_endeca_edf_adapter_plugins_NativeTransformer_getTransform
  (JNIEnv *pEnv, jobject obj, jstring input, jstring xslt) {

        jboolean copy = JNI_FALSE;
        time_t currenttime = time(NULL); 
        printf( "Start Native %ld\n", currenttime );

        const char *theInputDocument = pEnv->GetStringUTFChars(input, &copy);
        if (theInputDocument == NULL) {
                return NULL; // Could be out of memory
        }
        currenttime = time(NULL); 
        printf( "Read Input String %ld\n", currenttime );

        const char *theStylesheet = pEnv->GetStringUTFChars(xslt, &copy);
        if (theStylesheet == NULL) {
                return NULL; // Could be out of memory
        }
        currenttime = time(NULL); 
        printf( "Read Stylesheet %ld\n", currenttime );

        XALAN_USING_STD(istrstream)
        XALAN_USING_STD(ostringstream)

#if defined(XALAN_STRICT_ANSI_HEADERS)
        using std::strlen;
#endif

        int     theResult = -1;

        try
        {
                XALAN_USING_XERCES(XMLPlatformUtils)
                XALAN_USING_XALAN(XalanTransformer)

                // Call the static initializer for Xerces.
                XMLPlatformUtils::Initialize();

                // Initialize Xalan.
                XalanTransformer::initialize();

                {
                        // Create a XalanTransformer.
                        XalanTransformer theXalanTransformer;

                        XALAN_USING_XALAN(XalanDOMString)
                        XALAN_USING_XALAN(XSLTInputSource)
                       
                        istrstream theXSLStream(theStylesheet, strlen(theStylesheet));
                        XSLTInputSource stylesheetSource(&theXSLStream);
                       
                        //Thought compiling stylesheet would speed things up... it didn't
                        //XALAN_USING_XALAN(XalanCompiledStylesheet)
                        //const XalanCompiledStylesheet*        compiledStylesheet = 0;
                        //theXalanTransformer.compileStylesheet(stylesheetSource, compiledStylesheet);

                        ostringstream theOutXMLStream;

                        // Do the transform.
                        currenttime = time(NULL); 
                        printf( "Prepping Input %ld\n", currenttime );
                        try {
                                // input is filepath
                                XSLTInputSource theInputSource(theInputDocument);
                                currenttime = time(NULL); 
                                printf( "Start Transforming %ld\n", currenttime );
                                theResult = theXalanTransformer.transform(theInputSource, stylesheetSource, theOutXMLStream);

                        } catch(...) {
                                // input was actual xml string
                                istrstream theInXMLStream(theInputDocument, strlen(theInputDocument));
                                currenttime = time(NULL); 
                                printf( "Start Transforming %ld\n", currenttime );
                                theResult = theXalanTransformer.transform(&theInXMLStream, stylesheetSource, theOutXMLStream);

                        }
                        currenttime = time(NULL); 
                        printf( "End Transforming %ld\n", currenttime );

                        pEnv->ReleaseStringUTFChars(input, theInputDocument);
                        pEnv->ReleaseStringUTFChars(xslt, theStylesheet);

                        if(theResult != 0)
                        {
                                std::stringstream error;
                                error << "NativeTransform Error: \n" <<"\n\n"<< theXalanTransformer.getLastError() << "\n";

                                return pEnv->NewStringUTF(error.str().c_str());
                        } else {
                                return pEnv->NewStringUTF(theOutXMLStream.str().c_str());
                        }
                }

                // Terminate Xalan...
                XalanTransformer::terminate();

                // Terminate Xerces...
                XMLPlatformUtils::Terminate();

                // Clean up the ICU, if it's integrated...
                XalanTransformer::ICUCleanUp();
        }
        catch(...)
        {      
                pEnv->ReleaseStringUTFChars(input, theInputDocument);
                pEnv->ReleaseStringUTFChars(xslt, theStylesheet);

                return pEnv->NewStringUTF("Xalan Initialization Failed\n");
        }

        return NULL;
}

This email message and any attachments are confidential to Endeca. If you are not the intended recipient, please notify Endeca immediately -- by replying to this message or by sending an email to: [EMAIL PROTECTED] -- and destroy all copies of this message and any attachments. Thank you.

Reply via email to