Refactor parsing of numeric ASCII lists-of-lists#772
Refactor parsing of numeric ASCII lists-of-lists#772RamogninoF wants to merge 3 commits intogerlero:mainfrom
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+)
|
@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 |
| + _SKIP.pattern | ||
| + rb")?\)" | ||
| _SUB_LIST_LIKE = re.compile( | ||
| rb"(?:" + _POSSIBLE_INTEGER.pattern + rb")(?:" + _SKIP.pattern + rb")?\([^()]*?\)" |
There was a problem hiding this comment.
@RamogninoF problem to me is that this doesn't actually check that the sublist is well-formed. E.g. this will readily accept a list with a wrong count like 2 (1 2 3)...
There was a problem hiding this comment.
Unfortunately I have no background in parsing logics etc. and all this is far beyond my capabilities, I just hope this can be a useful starting point for you. At the current state parsing of meshes in ascii format is just straight impossible due to the time required for parsing (I gave up even on a 10k cells mesh after it was taking more then 10 minutes parsing the faces file). I would like to be able to support handling also ascii meshes rather then only binary
There was a problem hiding this comment.
A simple alternative could be just to add hardcoded parser for up to 10-vertex faces or so, which I think would be more then enough for most cases
There was a problem hiding this comment.
Or what if the string is parsed twice via regex, one to retrieve the list and one to get the prefix marking it's length, and these quantities are compared to validate the parsed data before returning?
|
@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? |
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