在 XSLT 中对记录进行分组时如何避免 O(n^2) 复杂度?
当我将 XSL 大量数据转换为 HTML 时,我经常遇到性能问题。这些数据通常只是几个非常大的表,大致是这种形式:
<table>
<record>
<group>1</group>
<data>abc</abc>
</record>
<record>
<group>1</group>
<data>def</abc>
</record>
<record>
<group>2</group>
<data>ghi</abc>
</record>
</table>
在转换期间,我想像这样对记录进行可视化分组
+--------------+
| Group 1 |
+--------------+
| abc |
| def |
+--------------+
| Group 2 |
+--------------+
| ghi |
+--------------+
这是一个愚蠢的实现(集来自 http://exslt.org。实际的实现有点不同,这只是一个例子):
<xsl:for-each select="set:distinct(/table/record/group)">
<xsl:variable name="group" select="."/>
<!-- This access needs to be made faster : -->
<xsl:for-each select="/table/record[group = $group]">
<!-- Do the table stuff -->
</xsl:for-each>
</xsl:for-each>
很容易看出这往往有 O(n^ 2)复杂性。更糟糕的是,因为每条记录中有很多字段。操作的数据可达几十MB,记录数可达5000条。最坏的情况下,每条记录都有自己的组和50个字段。更糟糕的是,还有另一个层次的分组可能,使得这个
O(n^3)
现在会有很多选择:
- 我可以找到一个 Java 解决方案来解决这个问题,其中涉及映射和嵌套数据结构。但我想提高我的 XSLT 技能,所以这实际上是最后的选择。
- 我可能忘记了 Xerces/Xalan/Exslt 中的一个很好的功能,它可以更好地处理分组
- 我可以为
/table/record/group
构建某种索引 - 你可以向我证明在此用例中,
方法明显比
方法更快。
您认为如何降低这个 O(n^2)
复杂度?
I'm frequently running into performance issues when I XSL transform large amounts of data into HTML. This data is usually just a couple of very large tables of roughly this form:
<table>
<record>
<group>1</group>
<data>abc</abc>
</record>
<record>
<group>1</group>
<data>def</abc>
</record>
<record>
<group>2</group>
<data>ghi</abc>
</record>
</table>
During transformation, I want to visually group the records like this
+--------------+
| Group 1 |
+--------------+
| abc |
| def |
+--------------+
| Group 2 |
+--------------+
| ghi |
+--------------+
A silly implementation is this one (set is from http://exslt.org. the actual implementation is a bit different, this is just an example):
<xsl:for-each select="set:distinct(/table/record/group)">
<xsl:variable name="group" select="."/>
<!-- This access needs to be made faster : -->
<xsl:for-each select="/table/record[group = $group]">
<!-- Do the table stuff -->
</xsl:for-each>
</xsl:for-each>
It's easy to see that this tends to have O(n^2)
complexity. Even worse, as there are lots of fields in every record. The data operated on can reach several dozens of MB, the number of records can go up to 5000. In the worst case, every record has its own group and 50 fields. And to make things even much worse, there is yet another level of grouping possible, making this O(n^3)
Now there would be quite a few options:
- I could find a Java solution to this involving maps and nested data structures. But I want to improve my XSLT skills, so that's actually the last option.
- I'm maybe oblivious of a nice feature in Xerces/Xalan/Exslt, that can handle grouping much better
- I can maybe build an index of some sort for
/table/record/group
- You can prove to me that the
<xsl:apply-templates/>
approach is decidedly faster in this use-case than the<xsl:for-each/>
approach.
What do you think how this O(n^2)
complexity can be reduced?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在 XSLT 1.0 中使用众所周知的 Muenchian 分组方法 - 无需探索排序数据并实现更复杂和更慢的算法:
当此转换应用于您提供的文本时(这甚至不是一个格式良好的 XML 文档!!!)将其纠正为格式良好后,
3 个
记录
元素需要 80 毫秒。对于具有 1000 个
记录
元素的类似文本,转换在 136 毫秒内完成。对于 10000 个
记录
元素,花费的时间为 284 毫秒。对于 100000 个
记录
元素,花费的时间为 1667 毫秒。观察到的复杂性显然是次线性的。
在 XSLT 1.0 中找到比 Muenchian 分组更有效的解决方案是非常困难的(如果可能的话)。
You can just use the wellknown Muenchian grouping method in XSLT 1.0 -- no need to explore sorted data and implement more complicated and slower algorithms:
When this transformation is applied on your provided text (that isn't even a well-formed XML document!!!) after correcting it to well-formedness,
it takes 80ms for 3
record
elements.With similar text having 1000
record
elements the transformation finishes in 136ms.With 10000
record
elements the time taken is 284ms.With 100000
record
elements the time taken is 1667ms.The observed complexity is clearly sublinear.
It would be very difficult (if possible at all) to find a more efficient solution than Muenchian grouping in XSLT 1.0.
如果数据是按组预先排序的(如您的示例),您可以循环记录集并检查该记录的组是否与前面的记录组不同。如果组发生更改,您可以添加组标题。这将以 O(n) 时间复杂度执行。
If the data is presorted by groups (as in your example), you can loop the record set and check if the group of the record is different from the preceding record group. If the group changes, you can add a group header. This will perform in O(n) time complexity.
您当前的算法:
我假设如果您对所有元素执行简单迭代并且
对于组表示,您可以使用树或地图。
正如你所看到的,该算法的复杂度为 O(n)
Your current algorithm:
I assume that if you perform simple iteration through all elements and
For group representation you can use trees or maps.
As you can see, this algorithm has complexity O(n)
推荐的分组方法是 XSLT 2.0 中的 xsl:for-each-group 和 XSLT 1.0 中的 Muenchian 分组。对于任何半像样的处理器,这两者都将具有 (n*log(n)) 性能。
或者您可以简单地将
"/table/record[group = $group]"
替换为对 key() 函数的调用。如果您准备支付企业级 XSLT 处理器(例如 Saxon-EE)的费用,那么这些优化很可能会自动为您完成,因此您不必担心它们。
The recommended grouping methods are xsl:for-each-group in XSLT 2.0, and Muenchian grouping in XSLT 1.0. With any half-decent processor, both of these will have (n*log(n)) performance.
Or you could simply replace
"/table/record[group = $group]"
with a call to the key() function.If you're prepared to pay for an enterprise-class XSLT processor such as Saxon-EE, there's a good chance these optimizations will be done for you automatically so you don't have to worry about them.