From 44cc6cd1dad11f110e262b1a6fb46d97a1fcc013 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Wed, 4 Dec 2024 13:25:29 +0000 Subject: [PATCH 1/2] [mypyc] Document optimized bytes ops and additional str ops --- mypyc/doc/bytes_operations.rst | 40 ++++++++++++++++++++++++++++++++++ mypyc/doc/index.rst | 1 + mypyc/doc/str_operations.rst | 16 ++++++++++++-- 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 mypyc/doc/bytes_operations.rst diff --git a/mypyc/doc/bytes_operations.rst b/mypyc/doc/bytes_operations.rst new file mode 100644 index 000000000000..8cdea5d8da52 --- /dev/null +++ b/mypyc/doc/bytes_operations.rst @@ -0,0 +1,40 @@ +.. _bytes-ops: + +Native bytes operations +======================== + +These ``bytes`` operations have fast, optimized implementations. Other +bytes operations use generic implementations that are often slower. + +Construction +------------ + +* Bytes literal +* ``bytes(x: list)`` + +Operators +--------- + +* Concatenation (``b1 + b2``) +* Indexing (``b[n]``) +* Slicing (``b[n:m]``, ``b[n:]``, ``b[:m]``) +* Comparisons (``==``, ``!=``) + +Methods +------- + +* ``b.decode()`` +* ``b.decode(encoding: str)`` +* ``b.decode(encoding: str, errors: str)`` +* ``b.join(x: Iterable)`` + +Formatting +---------- + +A subset of % formatting operations are optimized (``b"..." % (...)``). + +Functions +--------- + +* ``len(b: bytes)`` +* ``ord(b: bytes)`` diff --git a/mypyc/doc/index.rst b/mypyc/doc/index.rst index 5b1cc48fab3d..584d6739e803 100644 --- a/mypyc/doc/index.rst +++ b/mypyc/doc/index.rst @@ -36,6 +36,7 @@ generate fast code. bool_operations float_operations str_operations + bytes_operations list_operations dict_operations set_operations diff --git a/mypyc/doc/str_operations.rst b/mypyc/doc/str_operations.rst index a8f2cf43a991..4d5cc889838d 100644 --- a/mypyc/doc/str_operations.rst +++ b/mypyc/doc/str_operations.rst @@ -25,6 +25,9 @@ Operators Methods ------- +* ``s.encode()`` +* ``s.encode(encoding: str)`` +* ``s.encode(encoding: str, errors: str)`` * ``s1.endswith(s2: str)`` * ``s.join(x: Iterable)`` * ``s.replace(old: str, new: str)`` @@ -34,8 +37,17 @@ Methods * ``s.split(sep: str, maxsplit: int)`` * ``s1.startswith(s2: str)`` +Formatting +---------- + +A subset of these common string formatting expressions are optimized: + +* F-strings +* ``"...".format(...)`` +* ``"..." % (...)`` + Functions --------- - * ``len(s: str)`` - * ``ord(s: str)`` +* ``len(s: str)`` +* ``ord(s: str)`` From 4f98f5d1f3d5436503c76a505e92f3080df8e285 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Wed, 4 Dec 2024 13:39:29 +0000 Subject: [PATCH 2/2] Add cross references --- mypyc/doc/bytes_operations.rst | 6 ++++++ mypyc/doc/str_operations.rst | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/mypyc/doc/bytes_operations.rst b/mypyc/doc/bytes_operations.rst index 8cdea5d8da52..038da6391949 100644 --- a/mypyc/doc/bytes_operations.rst +++ b/mypyc/doc/bytes_operations.rst @@ -20,6 +20,8 @@ Operators * Slicing (``b[n:m]``, ``b[n:]``, ``b[:m]``) * Comparisons (``==``, ``!=``) +.. _bytes-methods: + Methods ------- @@ -28,6 +30,10 @@ Methods * ``b.decode(encoding: str, errors: str)`` * ``b.join(x: Iterable)`` +.. note:: + + :ref:`str.encode() ` is also optimized. + Formatting ---------- diff --git a/mypyc/doc/str_operations.rst b/mypyc/doc/str_operations.rst index 4d5cc889838d..9e94f1b6d7bb 100644 --- a/mypyc/doc/str_operations.rst +++ b/mypyc/doc/str_operations.rst @@ -22,6 +22,8 @@ Operators * Comparisons (``==``, ``!=``) * Augmented assignment (``s1 += s2``) +.. _str-methods: + Methods ------- @@ -37,6 +39,10 @@ Methods * ``s.split(sep: str, maxsplit: int)`` * ``s1.startswith(s2: str)`` +.. note:: + + :ref:`bytes.decode() ` is also optimized. + Formatting ----------