[New-bugs-announce] [issue44194] Folding ''.join(<known number of elts>) into f-strings

Batuhan Taskaya report at bugs.python.org
Thu May 20 15:03:34 EDT 2021


New submission from Batuhan Taskaya <isidentical at gmail.com>:

Since strings are immutable types, we could fold some operation on top of them. Serhiy has done some work on issue 28307 regarding folding `str % args` combination, which I think we can extend even further. One simple example that I daily encounter is that, there is this pattern

'/'.join([something, something_else, user_arg])

where people would join N number of elements together with a separator, where the N is something that we know. Just a search over some locally cloned PyPI projects (a couple thousand, showed 5000+ occurrences where this optimization applies).

The preliminary benchmarks indicate that the speedup is about %40. Though there are multiple issues that might concern others:
   - type checking, f-strings cast automatically but .join() requires each element to be a string subclass. The current implementation introduces a new conversion called 'c' which actually does the type checking instead of converting the value.
   - preventing a call to a runtime function, I belive that this work is not that different than issue 28307 which prevents str.__mod__ though I understand the concern

Here is the implementation if anybody wants to take a look at it: https://github.com/isidentical/cpython/commit/d7ea8f6e38578ba06d28deb4b4a8df676887ec26

I believe that the implementation can be optimized further (etc if a continuous block of elements are a string, then we can load all of them in one go!). And some cases proved that the f-strings might be faster than the join by 1.7-1.8x.

----------
messages: 394049
nosy: BTaskaya, eric.smith, pablogsal, serhiy.storchaka
priority: normal
severity: normal
status: open
title: Folding ''.join(<known number of elts>) into f-strings

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44194>
_______________________________________


More information about the New-bugs-announce mailing list