Skip to content

Commit

Permalink
improve: cache textwrap.TextWrapper objects
Browse files Browse the repository at this point in the history
  • Loading branch information
hukkin committed Dec 17, 2024
1 parent 61cc2a5 commit 3dd45d2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
7 changes: 2 additions & 5 deletions src/mdformat/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
from pathlib import Path
import shutil
import sys
import textwrap

import mdformat
from mdformat._conf import DEFAULT_OPTS, InvalidConfError, read_toml_opts
from mdformat._util import detect_newline_type, is_md_equal
from mdformat._util import cached_textwrapper, detect_newline_type, is_md_equal
import mdformat.plugins
import mdformat.renderer

Expand Down Expand Up @@ -408,9 +407,7 @@ def wrap_paragraphs(paragraphs: Iterable[str]) -> str:
wrap_width = terminal_width
else:
wrap_width = 80
wrapper = textwrap.TextWrapper(
break_long_words=False, break_on_hyphens=False, width=wrap_width
)
wrapper = cached_textwrapper(wrap_width)
return "\n\n".join(wrapper.fill(p) for p in paragraphs) + "\n"


Expand Down
13 changes: 13 additions & 0 deletions src/mdformat/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from collections.abc import Iterable, Mapping
from contextlib import nullcontext
import functools
import re
import textwrap
from types import MappingProxyType
from typing import Any, Literal

Expand Down Expand Up @@ -114,3 +116,14 @@ def detect_newline_type(md: str, eol_setting: str) -> Literal["\n", "\r\n"]:
if eol_setting == "crlf":
return "\r\n"
return "\n"


@functools.lru_cache
def cached_textwrapper(width: int) -> textwrap.TextWrapper:
return textwrap.TextWrapper(
break_long_words=False,
break_on_hyphens=False,
width=width,
expand_tabs=False,
replace_whitespace=False,
)
6 changes: 4 additions & 2 deletions src/mdformat/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import argparse
from collections.abc import Callable, Mapping
from typing import Any, Protocol
from typing import TYPE_CHECKING, Any, Protocol

from markdown_it import MarkdownIt

from mdformat._compat import importlib_metadata
from mdformat.renderer.typing import Postprocess, Render

if TYPE_CHECKING:
from mdformat.renderer.typing import Postprocess, Render


def _load_entrypoints(
Expand Down
10 changes: 2 additions & 8 deletions src/mdformat/renderer/_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
from contextlib import contextmanager
import logging
import re
import textwrap
from types import MappingProxyType
from typing import TYPE_CHECKING, Any, Literal, NamedTuple

from markdown_it.rules_block.html_block import HTML_SEQUENCES

from mdformat import codepoints
from mdformat._conf import DEFAULT_OPTS
from mdformat._util import cached_textwrapper
from mdformat.renderer._util import (
RE_CHAR_REFERENCE,
decimalify_leading,
Expand Down Expand Up @@ -344,13 +344,7 @@ def _wrap(text: str, *, width: int | Literal["no"]) -> str:
if width == "no":
return _recover_preserve_chars(text, replacements)

wrapper = textwrap.TextWrapper(
break_long_words=False,
break_on_hyphens=False,
width=width,
expand_tabs=False,
replace_whitespace=False,
)
wrapper = cached_textwrapper(width)
wrapped = wrapper.fill(text)
wrapped = _recover_preserve_chars(wrapped, replacements)
return wrapped
Expand Down

0 comments on commit 3dd45d2

Please sign in to comment.