You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
The new JSON parsing in catch_discover_tests() is really slow. #3168
Describe the bug
The project I'm working on have thousands of tests and each test does have a tag used for grouping purposes. Since we've upgraded to the latest version of Catch, we noticed that our pipeline is slower with several minutes. Tracing the issue lead us to the changes inside catch_discover_tests_impl() which switched from text parsing to JSON parsing. Unfortunately CMake JSON parsing API is really slow as it reparses everything on invocation...
Expected behavior
Have a better performance. I know that if we keep the JSON parsing it will be fore sure slower compared to before but I see that with a few changes we can have some big wins(about 20-30%). For example, even though we have tags on our tests, we currently don't use them as labels, thus with few adjustments a lot of parsing(per iteration) can be skipped.
Reproduction steps
Use catch_discover_tests() on a test executable with thousands of tests where each tests also have a tag.
Platform information:
OS: Windows 11
Compiler+version: MSVC 19.51.36248
Catch version: v3.15.1
Additional context
Opened PR #3169. This PR is simple fix for removing unneeded JSON parsing step. However, maybe a better solution would be to check if converting the tags to labels is not needed, then perhaps we can restore the original pure texting parsing logic?
However, maybe a better solution would be to check if converting the tags to labels is not needed, then perhaps we can restore the original pure texting parsing logic?
No, exchanging information with the outside via structured formats is here to stay. Labels weren't the only motivation for using JSON as the interchange format, they just made it less avoidable.
The big issue with the older format was that because it relied on line-based plaintext, it didn't have good answer to listing tests with whitespace characters like TEST_CASE("A\nB\r\nC"). I do wish that CMake was less... CMake... about parsing JSON though.
@horenmar I totally agree that having a structured format is the correct way to go, but also CMake JSON support is really terrible in terms of both API usage and performance. I think it was mostly added just to aid extracting simple information in a one-time shot. Doing loops is really heavy and time consuming. While my fix took some of the pressure, the performance gap is still HUGE(orders of magnitude) compared to the line based parsing.
Regarding the issue with whitespace characters - this can be easily fixed by escaping them during printing using the very same escape patterns which we use in C++ with backslashes. I still humbly ask you to reconsider having both the line parser and the JSON one side by side and using JSON only when the user requests functionality which the line-based parser cannot provide.
P.S. Another idea which just came to my mind about line-based parsing. Perhaps a CSV format can be introduced where each column will contain all the information you would need to have extended functionality?
P.S. Another idea which just came to my mind about line-based parsing. Perhaps a CSV format can be introduced where each column will contain all the information you would need to have extended functionality?
Are you ready to maintain a CSV parser in CMake for at least a year?
Anyway, latest release sped-up catch_discover_tests a bunch. If it is still too slow for you (annoyingly, it still has quadratic scaling, so binaries with more than 1k tests are still painful), my recommendation is to
P.S. Another idea which just came to my mind about line-based parsing. Perhaps a CSV format can be introduced where each column will contain all the information you would need to have extended functionality?
Are you ready to maintain a CSV parser in CMake for at least a year?
@horenmar Thanks a lot for all the work you did to optimize the JSON parsing!
Regarding your question about CMake CSV parser, I think it is way simpler than all the stuff you did. Here's the super simple solution which handles CSV:
file(STRINGS"data.csv"rows)
foreach(row INLISTS rows)
# Replace commas with semicolons to convert the row into a native CMake liststring(REPLACE","";" row_list "${row}")
list(GET row_list 0 col_1)
list(GET row_list 1 col_2)
message(STATUS"Column 1: ${col_1} | Column 2: ${col_2}")
endforeach()
Of course this cannot handle commas which are part of the test name and also empty columns. However the AI crafted a really good solution which is not that much complex and it handles CSVs the proper way:
file(STRINGS"data.csv"rows)
foreach(row INLISTS rows)
set(row_list "")
set(current_row "${row}")
# Regex logic: Match text inside quotes OR text without commas OR an empty cell before a comma/EOLwhile(current_row MATCHES"^([^,\"]*|\"[^\"]*\")(,|$)")
set(match "${CMAKE_MATCH_1}")
# Strip surrounding quotes from the cell value if they existif(match MATCHES"^\"(.*)\"$")
set(match "${CMAKE_MATCH_1}")
endif()
# Safety escape: Temporarily replace literal semicolons to prevent CMake list corruptionstring(REPLACE";""[SEMICOLON]"match"${match}")
# Append the cell value to our list (quoted to preserve empty values)list(APPEND row_list "${match}")
# Remove the processed match from the row string and continue parsingstring(LENGTH"${CMAKE_MATCH_0}" match_len)
string(SUBSTRING"${current_row}"${match_len} -1 current_row)
endwhile()
# --- AT THIS POINT, row_list IS A RELIABLE CMAKE LIST ---# Extract columns by their exact indexlist(GET row_list 0 col_1)
list(GET row_list 1 col_2)
list(GET row_list 2 col_3)
# Restore any literal semicolons that were originally inside the CSV textstring(REPLACE"[SEMICOLON]"";" col_1 "${col_1}")
string(REPLACE"[SEMICOLON]"";" col_2 "${col_2}")
string(REPLACE"[SEMICOLON]"";" col_3 "${col_3}")
# Debug print to verify the outputmessage(STATUS"Col 1: '${col_1}' | Col 2: '${col_2}' | Col 3: '${col_3}'")
endforeach()
So yep, I would maintain that code and I think it will be easier compared to all the JSON optimization workarounds. :)
Activity
TheStormN commentedon Jul 7, 2026
DO NOT try to open or run the attached file by gabenonoko. It is a an executable with a virus.
horenmar commentedon Jul 8, 2026
No, exchanging information with the outside via structured formats is here to stay. Labels weren't the only motivation for using JSON as the interchange format, they just made it less avoidable.
The big issue with the older format was that because it relied on line-based plaintext, it didn't have good answer to listing tests with whitespace characters like
TEST_CASE("A\nB\r\nC"). I do wish that CMake was less... CMake... about parsing JSON though.TheStormN commentedon Jul 8, 2026
@horenmar I totally agree that having a structured format is the correct way to go, but also CMake JSON support is really terrible in terms of both API usage and performance. I think it was mostly added just to aid extracting simple information in a one-time shot. Doing loops is really heavy and time consuming. While my fix took some of the pressure, the performance gap is still HUGE(orders of magnitude) compared to the line based parsing.
Regarding the issue with whitespace characters - this can be easily fixed by escaping them during printing using the very same escape patterns which we use in C++ with backslashes. I still humbly ask you to reconsider having both the line parser and the JSON one side by side and using JSON only when the user requests functionality which the line-based parser cannot provide.
P.S. Another idea which just came to my mind about line-based parsing. Perhaps a CSV format can be introduced where each column will contain all the information you would need to have extended functionality?
horenmar commentedon Jul 28, 2026
Are you ready to maintain a CSV parser in CMake for at least a year?
Anyway, latest release sped-up
catch_discover_testsa bunch. If it is still too slow for you (annoyingly, it still has quadratic scaling, so binaries with more than 1k tests are still painful), my recommendation is todiscover_testshelper.--list-tests --verbosity quietanddiscover_testsfrom 1.TheStormN commentedon Jul 28, 2026
@horenmar Thanks a lot for all the work you did to optimize the JSON parsing!
Regarding your question about CMake CSV parser, I think it is way simpler than all the stuff you did. Here's the super simple solution which handles CSV:
Of course this cannot handle commas which are part of the test name and also empty columns. However the AI crafted a really good solution which is not that much complex and it handles CSVs the proper way:
So yep, I would maintain that code and I think it will be easier compared to all the JSON optimization workarounds. :)