Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 29, 2025

The cache backend and models were calling textwrap.dedent() and collapse_spaces() at import time to format SQL strings and expressions, adding unnecessary overhead to module loading.

Changes

  • Removed textwrap.dedent import and call from cache.py
  • Removed collapse_spaces() function from utils.py
  • Replaced all collapse_spaces() calls (18 total) with plain strings across:
    • cache.py: 9 SQL query strings
    • models/expressions.py: 6 SQL expressions
    • models/transforms.py: 1 SQL expression
    • management commands: 2 help text strings
  • Replaced dedent() call on create_table_sql with parenthesized string literal containing explicit newlines
  • Used # fmt: off and # fmt: on comments to preserve original indentation structure for readability

Before:

from textwrap import dedent
from django_mysql.utils import collapse_spaces

create_table_sql = dedent(
    """\
    CREATE TABLE `{table_name}` (
        cache_key varchar(255) CHARACTER SET utf8 COLLATE utf8_bin
                               NOT NULL PRIMARY KEY,
        ...
    );
"""
)

_add_query = collapse_spaces(
    """
    INSERT INTO {table} (cache_key, value, value_type, expires)
    VALUES (%s, %s, %s, %s)
    ON DUPLICATE KEY UPDATE
        value=IF(expires > @tmp_now:=%s, value, VALUES(value)),
        ...
"""
)

After:

# fmt: off
create_table_sql = (
    "CREATE TABLE `{table_name}` (\n"
    "    cache_key varchar(255) CHARACTER SET utf8 COLLATE utf8_bin\n"
    "                           NOT NULL PRIMARY KEY,\n"
    ...
)
# fmt: on

# fmt: off
_add_query = (
    "INSERT INTO {table} (cache_key, value, value_type, expires) "
    "VALUES (%s, %s, %s, %s) "
    "ON DUPLICATE KEY UPDATE "
        "value=IF(expires > @tmp_now:=%s, value, VALUES(value)), "
        ...
)
# fmt: on

Impact

  • Performance: Removed 18 function calls from module import time
  • Readability: Original indentation preserved in SQL strings for better readability
  • Compatibility: All SQL strings are functionally equivalent (verified by normalization comparison)
  • No breaking changes: Works with existing .format() and .splitlines() usage in tests and management commands
Original prompt

remove import-time dedent calls from cache backend, to speed up import time. Use plain strings instead, wrapped in parentheses.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Remove import-time dedent calls from cache backend Remove import-time dedent call from cache backend Oct 29, 2025
Copilot AI requested a review from adamchainz October 29, 2025 15:27
Copilot finished work on behalf of adamchainz October 29, 2025 15:27
@adamchainz
Copy link
Owner

@copilot do the same for the collapse_spaces() calls, and see if you can remove collapse_spaces from the repo

Copy link
Contributor Author

Copilot AI commented Oct 29, 2025

@copilot do the same for the collapse_spaces() calls, and see if you can remove collapse_spaces from the repo

Done! I've replaced all collapse_spaces() calls with plain strings and removed the function from the repo.

Changes made in commit 2af3a00:

  • Replaced 9 collapse_spaces() calls in cache.py
  • Replaced 6 collapse_spaces() calls in models/expressions.py
  • Replaced 1 collapse_spaces() call in models/transforms.py
  • Replaced 2 collapse_spaces() calls in management commands
  • Removed collapse_spaces function from utils.py
  • Removed all imports of collapse_spaces

The SQL strings are functionally equivalent (verified by normalizing and comparing). All linting checks pass. Net reduction: 126 lines of code.

Copilot AI changed the title Remove import-time dedent call from cache backend Remove import-time string processing from cache backend and models Oct 29, 2025
Copilot finished work on behalf of adamchainz October 29, 2025 16:40
@adamchainz
Copy link
Owner

@copilot rather than smoosh everything together into one string, preserve the original indentation, even if it needs something like:

# fmt: off
x = (
   "f("
       "x"
    ")"
)
# fmt: on

Copy link
Contributor Author

Copilot AI commented Oct 30, 2025

@copilot rather than smoosh everything together into one string, preserve the original indentation, even if it needs something like:

...

Done! I've updated all SQL strings to preserve the original indentation using # fmt: off and # fmt: on comments.

Changes in commit c9c88fd:

  • Reverted previous changes that smooshed strings together
  • Replaced dedent() and collapse_spaces() calls with plain strings
  • Preserved original indentation structure in all SQL strings
  • Added # fmt: off / # fmt: on markers to prevent formatters from changing indentation
  • All SQL strings maintain their original readability

The SQL strings now look like this:

# fmt: off
_add_query = (
    "INSERT INTO {table} (cache_key, value, value_type, expires) "
    "VALUES (%s, %s, %s, %s) "
    "ON DUPLICATE KEY UPDATE "
        "value=IF(expires > @tmp_now:=%s, value, VALUES(value)), "
        "value_type=IF(expires > @tmp_now, value_type, VALUES(value_type)), "
        "expires=IF("
            "expires > @tmp_now, "
            "IFNULL("
                "LEAST(LAST_INSERT_ID(444), NULL), "
                "expires"
            "), "
            "VALUES(expires)"
        ")"
)
# fmt: on

Copilot finished work on behalf of adamchainz October 30, 2025 00:21
@adamchainz adamchainz marked this pull request as ready for review October 30, 2025 07:51
@adamchainz adamchainz merged commit ef0d28c into main Oct 30, 2025
46 checks passed
@adamchainz adamchainz deleted the copilot/remove-import-time-dedent-calls branch October 30, 2025 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants