Skip to content

is_printable_as_block_string checks each character in a Python loop #312

Closed

Description

@dexhunter

print_schema() and print_ast() call is_printable_as_block_string() once for each description. The function walks the string one character at a time in a Python for loop. It keeps four boolean flags for each character. For long descriptions, this loop is a large part of the time that print_schema() uses.

Measured with Python 3.12 on one isolated CPU core (best of 7 runs):

Situation Now With str methods Change
One-line description (30 chars) 1405 ns 184 ns 87% less time
Description with 12 lines 19.2 µs 2.7 µs 86% less time
print_schema() of the GitHub schema fixture 25.5 ms 16.3 ms 36% less time
String that starts with a new line 106 ns 427 ns 0.3 µs slower

The last row is the only slower case. The old loop stops at the first character there. Such strings are rare in real schemas.

Proposed fix: keep the same checks, but use str methods instead of the character loop. A str.isprintable() pre-check and a regex find control characters. Then str.split("\n") gives the lines for the leading, trailing and indent checks. The result stays the same for all inputs, and tests/language/test_block_string.py passes.

This is a Python-specific change only. It keeps the four rules of isPrintableAsBlockString in GraphQL.js as four short checks with the same comments, so a later sync with upstream stays easy.

Environment: graphql-core main (1d505c1), CPython 3.12.13, Linux x86_64.

Activity

  1. added a commit that references this issue on Nov 15, 2026
    2b45e83
  2. Cito commented on Nov 15, 2026

    @Cito
    Member

    Fixed by #313.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions