Skip to content

Optimize xFilter function for better performance - #4

Open
tonygermano wants to merge 1 commit into
OpenIntegrationEngine:mainfrom
tonygermano:update-xfilter
Open

tonygermano wants to merge 1 commit into
OpenIntegrationEngine:mainfrom
tonygermano:update-xfilter

Conversation

@tonygermano

Copy link
Copy Markdown
Member

Open Integration Engine Discord user @Anibal reported slowness issues when working with very large lists, e.g., filtering HL7 messages with several thousand OBX segments.

One thing that was already known to me is that where list is an XMLList and node is an XML object, performing list += node will assign a new XMLList to list, which is a shallow copy of the original list with node appended to the end. Assigning list[index] = node where index is greater than or equal to the length of list will append to the list without copying it first.

A caveat to this is that when all elements of an XMLList share the same parent, assignments in this manner will not only update list, but will also attempt to update the parent's child list. To get around this, the would-be first element of the returned list is remembered, and an empty XML object with no parent is inserted into the list in its place. The remainder of the list is built as normal, and at the end, the first element in the list is replaced with the reserved object.

Through benchmarking shared in the discord chat, two additional optimizations were found. First, it is much faster to iterate over an XMLList using a for each..in loop than it is to use a normal for loop and access the list by index. Second, list.length() is much slower than expected, so the function scoped match_count is used to track the current length instead.

Open Integration Engine Discord user `@Anibal` reported slowness issues
when working with very large lists, e.g., filtering HL7 messages with
several thousand OBX segments.

One thing that was already known to me is that where `list` is an
`XMLList` and `node` is an `XML` object, performing `list += node` will
assign a new `XMLList` to `list`, which is a shallow copy of the
original `list` with `node` appended to the end. Assigning `list[index]
= node` where `index` is greater than or equal to the length of `list`
will append to the list without copying it first.

A caveat to this is that when all elements of an `XMLList` share the
same `parent`, assignments in this manner will not only update `list`,
but will also attempt to update the parent's child list. To get around
this, the would-be first element of the returned list is remembered, and
an empty XML object with no parent is inserted into the list in its
place. The remainder of the list is built as normal, and at the end, the
first element in the list is replaced with the reserved object.

Through benchmarking shared in the discord chat, two additional
optimizations were found. First, it is much faster to iterate over an
XMLList using a `for each..in` loop than it is to use a normal `for`
loop and access the list by index. Second, `list.length()` is much
slower than expected, so the function scoped `match_count` is used
to track the current length instead.

Signed-off-by: Tony Germano <tony@germano.name>
@tonygermano
tonygermano requested a review from ssrowe November 14, 2026 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant