Skip to content

Commit

Permalink
[mypyc] Intern string literals (#9960)
Browse files Browse the repository at this point in the history
This makes us more compatible with CPython, which interns (some)
string literals.

This speeds up some microbenchmarks slightly, by around 0.5-2%.

This may give a bigger speed win when calling functions using
vectorcalls and keyword arguments, but we'll see about that once that's
supported. Vectorcall argument parsing code has a fast path for interned
keyword argument names.
  • Loading branch information
JukkaL authored Jan 24, 2021
1 parent e978510 commit 9e1324d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mypyc/codegen/emitmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,8 @@ def generate_globals_init(self, emitter: Emitter) -> None:
emitter.emit_line(
'{} = CPyTagged_FromObject({});'.format(actual_symbol, symbol)
)
elif isinstance(literal, str):
emitter.emit_line('PyUnicode_InternInPlace(&{});'.format(symbol))

emitter.emit_lines(
'is_initialized = 1;',
Expand Down
3 changes: 3 additions & 0 deletions mypyc/test-data/run-strings.test
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ def match(x: str, y: str) -> Tuple[bool, bool]:

[file driver.py]
from native import f, g, tostr, booltostr, concat, eq, match
import sys

assert f() == 'some string'
assert f() is sys.intern('some string')
assert g() == 'some\a \v \t \x7f " \n \0string 🐍'
assert tostr(57) == '57'
assert concat('foo', 'bar') == 'foobar'
Expand Down

0 comments on commit 9e1324d

Please sign in to comment.