Skip to content

The new JSON parsing in catch_discover_tests() is really slow. #3168

Closed

Description

@TheStormN

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?

Activity

  1. TheStormN commented on Jul 7, 2026

    @TheStormN
    ContributorAuthor

    DO NOT try to open or run the attached file by gabenonoko. It is a an executable with a virus.

  2. deleted a comment from on Jul 8, 2026
  3. added
    Extras
    on Jul 8, 2026
  4. horenmar commented on Jul 8, 2026

    @horenmar
    Member

    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.

  5. TheStormN commented on Jul 8, 2026

    @TheStormN
    ContributorAuthor

    @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?

  6. horenmar commented on Jul 28, 2026

    @horenmar
    Member

    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

    1. Use CMake's new discover_tests helper.
    2. Limit your test names into something that can be easily parsed via default repoter's --list-tests --verbosity quiet and discover_tests from 1.
  7. TheStormN commented on Jul 28, 2026

    @TheStormN
    ContributorAuthor

    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 IN LISTS rows)
        # Replace commas with semicolons to convert the row into a native CMake list
        string(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 IN LISTS 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/EOL
        while(current_row MATCHES "^([^,\"]*|\"[^\"]*\")(,|$)")
            set(match "${CMAKE_MATCH_1}")
            
            # Strip surrounding quotes from the cell value if they exist
            if(match MATCHES "^\"(.*)\"$")
                set(match "${CMAKE_MATCH_1}")
            endif()
    
            # Safety escape: Temporarily replace literal semicolons to prevent CMake list corruption
            string(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 parsing
            string(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 index
        list(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 text
        string(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 output
        message(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. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Extras

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions