Skip to content

gh-155742: Optimize json float parsing - #156897

Open
vstinner wants to merge 2 commits into
python:mainfrom
vstinner:json_float
Open

gh-155742: Optimize json float parsing#156897
vstinner wants to merge 2 commits into
python:mainfrom
vstinner:json_float

Conversation

@vstinner

@vstinner vstinner commented Sep 3, 2026

Copy link
Copy Markdown
Member

Add internal _PyFloat_FromString(): similar to PyFloat_FromString(),
but don't require a Python object.

Use _PyFloat_FromString() in _json to avoid creating a temporary bytes objects.

_Py_string_to_number_with_underscores() can now be called with NULL object and float_from_string_inner() can now be called with NULL data; if needed they create a temporary bytes objects to format the error message.

Similar to PyFloat_FromString(), but don't require a Python object.

Use _PyFloat_FromString() in _json to avoid creating a temporary
bytes objects.

_Py_string_to_number_with_underscores() can now be called with NULL
object and float_from_string_inner() can now be called with NULL
data; if needed they create a temporary bytes objects to format the
error message.
@vstinner vstinner changed the title gh-155742: Add internal _PyFloat_FromString() gh-155742: Optimize json float parsing Sep 3, 2026
@vstinner

vstinner commented Sep 3, 2026

Copy link
Copy Markdown
Member Author

Benchmark:

import pyperf
import json
import random

NUMBERS = 10 ** 4
ITEMS = [str(random.random()) for _ in range(NUMBERS)]
DATA = '[' + ', '.join(ITEMS) + ']'

runner = pyperf.Runner()
runner.bench_func('json.loads', json.loads, DATA)

Result: [ref] 2.91 ms +- 0.34 ms -> [change] 2.49 ms +- 0.06 ms: 1.17x faster.

Fix the compiler warning:

    {'file': 'Objects/floatobject.c', 'line': '174', 'column': '5',
    'message': 'label followed by a declaration is a C23 extension',
    'option': '-Wc23-extensions'}
@eendebakpt

Copy link
Copy Markdown
Contributor

@vstinner This overlaps with #150639, I still have to loop at the differences.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants