RE: [xsl] String conversion problem when string is large

Subject: RE: [xsl] String conversion problem when string is large
From: "Bulgrien, Kevin" <Kevin.Bulgrien@xxxxxxxxxxxx>
Date: Wed, 21 Mar 2012 17:18:38 -0500
And this variant seems to balance speed and memory usage, but still kills all
the engines I have except the Saxonica engine.

Execution time: 1.039s (1039ms)
Memory used: 30306880

<xsl:call-template name="HexToDec">
  <xsl:with-param name="HexData" select="." />
  <xsl:with-param name="Hex" select="'0123456789ABCDEF'" />
</xsl:call-template>

<xsl:template name="HexToDec">
  <xsl:param name="HexData" />
  <xsl:param name="Hex" />
  <xsl:if test="string-length($HexData) &gt; 0">
    <xsl:text>,</xsl:text>
    <xsl:value-of
      select="string-length(
                substring-before(
                  $Hex, substring($HexData, 3, 1))) * 16 +
              string-length(
                  substring-before(
                  $Hex, substring($HexData, 4, 1)))" />
    <xsl:call-template name="HexToDec">
      <xsl:with-param name="HexData"
        select="substring-after($HexData, ',')" />
      <xsl:with-param name="Hex" select="$Hex" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

To let other XSLT engines that do not optimize recursion, a divide and conquer
approach was simple enough to concoct.  The chunk size was a bit arbitrary,
but is about half of what it took to break another engine at its default stack
depth.  xsltproc took about 2.8 seconds.  sablotron took 5.8 seconds.  I won't
bother to show times for the Windows engines as the platform difference makes
them hard to compare fairly.

Execution time: 832ms
Memory used: 26545336

<xsl:call-template name="HexToDecChunker">
  <xsl:with-param name="HexData" select="." />
  <xsl:with-param name="Hex" select="'0123456789ABCDEF'" />
</xsl:call-template>

<xsl:template name="HexToDecChunker">
  <xsl:param  name="HexData" />
  <xsl:param name="Hex" />
  <xsl:variable name="ChunkLen" select="5120" />
  <xsl:variable name="InputLen" select="string-length($HexData)" />
  <xsl:choose>
    <xsl:when test="$InputLen &gt; $ChunkLen">
      <xsl:call-template name="HexToDec">
        <xsl:with-param name="HexData"
          select="substring($HexData, 1, $ChunkLen)" />
        <xsl:with-param name="Hex" select="$Hex" />
      </xsl:call-template>
      <xsl:call-template name="HexToDecChunker">
        <xsl:with-param name="HexData"
          select="substring($HexData, $ChunkLen + 1)" />
        <xsl:with-param name="Hex" select="$Hex" />
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="HexToDec">
        <xsl:with-param name="HexData" select="$HexData" />
        <xsl:with-param name="Hex" select="$Hex" />
      </xsl:call-template>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

What an adventure.  There are probably a lot more ways to tackle this issue.

Kevin Bulgrien

This message and/or attachments may include information subject to GD
Corporate Policy 07-105 and is intended to be accessed only by authorized
personnel of General Dynamics and approved service providers.  Use, storage
and transmission are governed by General Dynamics and its policies.
Contractual restrictions apply to third parties.  Recipients should refer to
the policies or contract to determine proper handling.  Unauthorized review,
use, disclosure or distribution is prohibited.  If you are not an intended
recipient, please contact the sender and destroy all copies of the original
message.

Current Thread