You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Sc...@lotus.com on 2001/06/07 04:17:44 UTC
[DTM_EXP] Redesign of variable stack frames, variable mechanism
I've been doing a *lot* of running benchmarks and profiles of Xalan
[DTM_EXP] the last two weeks. One of the real hotspots that has come up is
variable and parameter execution. Basically, our current mechanism sucks
right now, and has got to go.
What we do now is create a stack frame for each template, and use it as a
proper stack, pushing Arg objects on it (complications having to do with
params deleted for sake of brevity). When we execute a variable in an
expression, we do a sequential search in that stack for an Arg that holds
the variable name, doing an expensive comparison of the QName objects.
OK, so here's a redesign that I plan to implement tomorrow. Please let me
know if you have better ideas.
1) ElemTemplate will hold three variables: frameSize) which is the stack
frame size for the template, which is equal to the maximum number of params
and variables that can be declared in the template at one time.
inArgsSize) The size of the portion of the stack frame that can hold
parameter arguments, and argsQNameIDs, which is a list of
namespace/local-name expressed as integers (using the ExtendedNameTable
class in the DTM), that are unique qname identifiers for the arguments.
The position of a given qname in the list is the argument ID, and thus the
position in the stack frame.
1b) Treat the global space (stylesheet) with the same variables as in
ElemTemplate.
2) In ProcessorTemplate#startElement, begin maintaining a list of variable
qnames, and a maximum value.
3) Have ElemVariable hold a stack frame index ID.
4) For each ElemVariable (including ElemParam) add the qname to the list of
variable qnames. For ElemParam add the qname ID to the list of param names
in the Template. (Note that xsl:param is constrained to be the first
children of the xsl:template).
5) In XSLTAttributeDef#processEXPR, processPATTERN, and processAVT, call
new method fixUpVariableRefs(Vector qnames, int top) on each Expression.
In XPath land, this will be called recursivly down the expression tree, and
each Variable expression will search backwards to match the QName, and then
assign the position index to a Variable member variable. [This is where a
proper abstract syntax tree would be much better].
6) In each ProcessorTemplateElem#endElement, pop off the variables assigned
since ProcessorTemplateElem#startElement.
7) For xsl:call template, xsl:with-param (ElemWithParam) can assign it's
parameters based on index, since it knows which template it's going to
execute at stylesheet build time. [however, I'm a little worried that
xsl:include and xsl:import will complicate this enough that I'll have to do
the index assignment in the compose() method].
8) xsl:apply-templates is a bit nastier. The same apply-templates can end
up calling 20 different templates, all with different xsl:param setups. So
each xsl:with-param in apply-templates will be assigned a qname ID, and the
argsQNameIDs list of the template being called will be searched
sequentially to find the slot for that param. This is not perfect, but
since the comparison will be of integers, will only be done at call time
instead of variable execution time, it's not too bad. Also, you only have
to search again if the template being called changes.
9) Each execution of xsl:variable and xsl:param will stick its data via
index into the stack frame.
10) Each execution of Variable will be a straight, quick lookup into the
stack frame array.
10b) but I'll have to take into account globals. So the index will either
be into the local stack from or the global stack frame.
11) Each stack frame will be a seperate object, stored by pool, so we'll
only need to create new stack frames as the depth is increased the first
time. I think this is preferable to a single array that has to be resized,
and is how it is implemented now.
Does this sound reasonable? Can I do it without screwing things up? This
should improve performance a fair amount for certain classes of
stylesheets, and actually improve performance a little bit for all
stylesheets, even if they don't use variables (because we won't have to be
doing push/popContextMarker for every element). The variable mechanism
tends to be very delicate, but I think this should be pretty robust out of
the box, and shouldn't effect the actual variable evaluation (i.e. lazy
evaluation and stuff like that). Or am I deluding myself?
-scott
Re: [DTM_EXP] Redesign of variable stack frames, variable mechanism
Posted by Gary L Peskin <ga...@firstech.com>.
Scott_Boag@lotus.com wrote:
>
> I've been doing a *lot* of running benchmarks and profiles of Xalan
> [DTM_EXP] the last two weeks. One of the real hotspots that has come up is
> variable and parameter execution. Basically, our current mechanism sucks
> right now, and has got to go.
> ...
> Does this sound reasonable? Can I do it without screwing things up? This
> should improve performance a fair amount for certain classes of
> stylesheets, and actually improve performance a little bit for all
> stylesheets, even if they don't use variables (because we won't have to be
> doing push/popContextMarker for every element). The variable mechanism
> tends to be very delicate, but I think this should be pretty robust out of
> the box, and shouldn't effect the actual variable evaluation (i.e. lazy
> evaluation and stuff like that). Or am I deluding myself?
>
> -scott
Scott --
I think that resolving the VarStack location of variables and parameters
faster is a great idea. I'm sure the details may get a little
complicated as you proceed with the actual implementation, but you get a
+1 from me on the idea.
I'm fairly certain, as you state, that xsl:call-template will need to be
resolved at compose() time since you don't know what template you'll
actually be calling until then. I don't think this is resolved at
compose time now (though I may be wrong) so you'll need to figure that
out at compose time and perhaps that information, also, could be saved
so that locating the target template at runtime can avoid a runtime hash
lookup, which is, I think, the way it works now. This would be an added
bonus. If that turns out to be too much work for this go around, I
suppose you could fall back to xsl:apply-templates like processing for
xsl:call-template.
As far as the xsl:with-param stuff (for both xsl:call-template and
xsl:apply-templates), I guess you'll have to copy the value of the
parameter in the calling template slot into the called template slot.
Is that right? I say that because when a Variable occurs, it has a
choice of only the current template stack frame or the global stack
frame, as I understand your design (10b). However, when you copy it,
you'll have to remember from whence it came because the evaluation needs
to be done in the context of where the xsl:with-param occurred, not the
context of the xsl:parameter. I'm not sure where this is remembered now
since I haven't looked at it in a while. Also, does it then get copied
back to the invoking slot? If not, it will have to be reevaluated the
next time it's passed which is not a good thing. If the expression in
the xsl:with-param, in turn, references other parameters that get
passed, this will have to be represented/remembered somehow.
If I understand your design correctly (which is not likely :)), I think
you may run into problems with a situation like this:
<xsl:template match="a">
<xsl:apply-templates select="b">
<xsl:with-param name="a" select="b/c/d"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="b">
<xsl:param name="a"/>
<xsl:apply-templates select="c">
<xsl:with-param name="b" select="$a/foo | bar"/>
</xsl:apply-templates>
<xsl:template>
<xsl:template match="c">
<xsl:param name="b"/>
<xsl:copy-of select="$b"/>
</xsl:template>
When the xsl:copy-of gets executed, where is the slot that the
expression and result go in? In b or c? As we evaluate b in the
context of template b, we need to switch to context a to evaluate
variable a. Then, does the evaluated value of variable a remain in the
slot in a?
Anyway, I haven't really thought this through but you get the idea.
If you feel that your design resolves these issues, there is no need to
take the time to correct my understanding of it. Just do it and we'll
all be able to have a look. I only point these issues out to make sure
that you've thought of them, since you did ask.
Gary