Refactor parsing of numeric ASCII lists-of-lists#772
Open
RamogninoF wants to merge 3 commits into
Open
Conversation
…y length of sub-lists (instead of hardcoded 3 and 4 length values). This is required to parse faceLists in ascii format which can have arbitrary number of vertices for poly meshes (often 5+)
Author
Owner
|
@RamogninoF thanks! I'll take a look. In principle this shouldn't be possible with regular expressions alone (which foamlib's parser uses to be fast enough), but I'll look at the code to see what it's doing |
gerlero
requested changes
Apr 10, 2026
Author
|
@gerlero I tried to update the logic considering your comments. Nevertheless, I tried to preserve a balance between safety and performances. I think that for practical production meshes the chances to have a silent digestion with a faulty file are virtually zero. Moreover, OpenFOAM should always write the number of elements in the list (of lists) with 20+ elements, so the double check should always take place. What do you think? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Extends the ASCII numeric list-of-lists parser to support sub-lists of arbitrary length, fixing slow parsing of
faceListfields in polyMesh files that contain polygonal faces with 5 or more vertices.Addresses the reviewer concern about missing inline count validation by introducing a hybrid validation strategy that keeps the performance of the fast path for large production meshes.
Background
OpenFOAM stores face connectivity in ASCII format as a list-of-lists where each entry is prefixed by its element count:
The previous parser only recognised sub-lists of fixed length 3 or 4 (matching vector and tensor shapes), causing any face with 5+ vertices to fall through to the generic slow-path parser — making even small poly meshes extremely slow to parse.
Changes
Arbitrary sub-list length (original fix,
ae73c6f)Replaced the hardcoded shape check with a general loop that reads each inline count
nand slices the nextnvalues from the flattened array.Inline count validation (this update)
The reviewer correctly noted that the flat-array approach silently accepts malformed input like
2(1 2 3)(count says 2, but 3 values are present). After stripping parentheses, the extra value bleeds into the stream and is misread as the count of the next sub-list, potentially returning wrong data without raising an error.The fix uses a hybrid strategy based on whether an outer count is present in the file:
count is None— per-sublist path (fully validating)When no outer element count is written, the parser iterates with
_SUBLIST_CAPTURE.finditerand validates eachn(...)block individually:Any mismatch between the declared count and the actual content raises immediately.
count is not None— fast flat-array path (production path)When an outer count is present, the original flat-array approach is kept for performance. Two in-loop guards cover crash scenarios; the outer count check acts as the primary correctness net:
Rationale for the split
OpenFOAM omits the outer count only for short lists (roughly ≤ 20 elements). For all large production face lists the outer count is always present, so:
Tests added
Eleven new tests in
tests/test_files/test_parsing/test_poly_face_list.py, split by path:0()2(1 2 3)ParseError4(1 2 3)ParseError2(1 2 3) 4(10 20 30 40)ParseError-1(0 1 2)ParseErrorParseErrorParseErrorParseErrorParseError