Skip to content

use default allocator #662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 8, 2023
Merged

use default allocator #662

merged 6 commits into from
Jun 8, 2023

Conversation

adriangb
Copy link
Member

@adriangb adriangb commented Jun 7, 2023

No description provided.

@codspeed-hq
Copy link

codspeed-hq bot commented Jun 7, 2023

CodSpeed Performance Report

Merging #662 default-allocator (ac54212) will degrade performances by 15.0%.

Summary

🔥 0 improvements
❌ 80 regressions
✅ 45 untouched benchmarks

🆕 0 new benchmarks
⁉️ 0 dropped benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main default-allocator Change
test_complete_core_lax 1.3 ms 1.5 ms -14.35%
test_complete_core_strict 1.4 ms 1.5 ms -10.76%
test_complete_core_error 7.9 ms 9.8 ms -24.38%
test_complete_core_isinstance 7.7 ms 9.8 ms -26.25%
test_complete_core_json 2.8 ms 3.3 ms -19.80%
test_build_schema 3.9 ms 4.6 ms -15.67%
test_core_python_fs 345 µs 409.3 µs -18.65%
test_core_python 512.8 µs 572.4 µs -11.64%
test_core_json_fs 667.8 µs 861.1 µs -28.95%
test_bool_core 49.4 µs 54.5 µs -10.18%
test_small_class_core_dict 26.1 µs 32.5 µs -24.53%
test_small_class_core_model 45.2 µs 57.3 µs -26.83%
test_core_string_lax_wrong 35.2 µs 42.9 µs -21.89%
test_core_string_strict_wrong 34.9 µs 42.5 µs -21.89%
test_core_string_strict_wrong_str_e 64.4 µs 79.1 µs -22.68%
test_definition_model_core 1.9 ms 2.1 ms -15.58%
test_list_of_dict_models_core 486.4 µs 556 µs -14.32%
test_list_of_ints_core_json 5.5 ms 6.6 ms -20.74%
test_set_of_ints_core_duplicates 2.4 ms 2.8 ms -19.18%
test_set_of_ints_core_json 6.8 ms 7.7 ms -13.29%
test_set_of_ints_core_json_duplicates 4.9 ms 5.8 ms -17.11%
test_frozenset_of_ints_core 1.3 ms 1.5 ms -11.57%
test_frozenset_of_ints_duplicates_core 993.9 µs 1,182.8 µs -19.00%
test_dict_of_any_core 4.2 ms 4.9 ms -16.93%
test_dict_of_ints_core_json 13.9 ms 16.3 ms -17.33%
test_many_models_core_dict 5 ms 6.1 ms -22.28%
test_many_models_core_model 14.3 ms 16.1 ms -12.31%
test_list_of_nullable_core 637.4 µs 809.4 µs -26.98%
test_core_python 39.8 µs 49.2 µs -23.63%
test_model_core_json 52.3 µs 62.7 µs -19.87%
test_core_str 25.4 µs 31.1 µs -22.19%
test_core_future 41.4 µs 48.5 µs -17.23%
test_core_future_str 26.1 µs 31.7 µs -21.42%
test_date_from_str 20.7 µs 27.7 µs -33.81%
test_date_from_datetime 33 µs 41 µs -24.23%
test_date_from_datetime_str 23.8 µs 30.5 µs -28.09%
test_core_future 24.3 µs 30.3 µs -24.92%
test_core_future_str 20.9 µs 27.1 µs -29.25%
test_strict_union_error_core 41.2 µs 46.6 µs -13.06%
test_raise_error_value_error 54.8 µs 62.6 µs -14.13%
test_raise_error_custom 56.4 µs 63.6 µs -12.70%
test_positional_tuple 26.3 µs 31.4 µs -19.53%
test_variable_tuple 26.6 µs 31.6 µs -18.87%
test_tuple_many_variable 29.4 µs 35.5 µs -21.03%
test_tuple_many_positional 30 µs 37.2 µs -23.98%
test_arguments 36.8 µs 44.7 µs -21.45%
test_with_default 36.4 µs 42.6 µs -17.00%
test_chain_list 37.1 µs 41.9 µs -13.12%
test_chain_function 36.3 µs 41.5 µs -14.21%
test_chain_two_functions 45.7 µs 51.8 µs -13.47%
test_chain_nested_functions 45.5 µs 51.8 µs -13.90%
test_generator_python 43.8 µs 48.9 µs -11.58%
test_generator_rust 27.3 µs 32.3 µs -18.16%
test_isinstance_json 26.5 µs 30.4 µs -14.95%
test_int_error 68.8 µs 80.5 µs -17.16%
test_definition_in_tree 6 ms 7.3 ms -21.48%
test_definition_out_of_tree 9.8 ms 11.2 ms -13.91%
test_model_instance 48 µs 55.7 µs -16.19%
test_model_instance_abc 48.9 µs 66.7 µs -36.49%
test_validate_literal[python-few_str_enum] 18.2 µs 22.6 µs -24.46%
test_validate_literal[python-few_mixed] 19.8 µs 24.3 µs -22.69%
test_validate_literal[json-few_small_strings] 22.7 µs 26.7 µs -17.47%
test_validate_literal[json-few_large_strings] 24 µs 27.5 µs -14.27%
test_validate_literal[json-few_str_enum] 25.6 µs 29.7 µs -16.09%
test_validate_literal[json-many_small_strings] 22.8 µs 41.6 µs -82.18%
test_validate_literal[json-many_large_strings] 24.1 µs 28.5 µs -18.30%
test_validate_literal[json-few_mixed] 21.2 µs 26.3 µs -24.01%
test_core_root_model 96.3 µs 121 µs -25.62%
test_int_range 18.8 µs 20.7 µs -10.30%
test_core_dict 292.7 µs 380 µs -29.84%
test_core_dict_filter 302.5 µs 390 µs -28.92%
test_core_json 386.9 µs 453 µs -17.08%
test_json_direct_list_str 928.8 µs 1,088.4 µs -17.18%
test_python_json_list_str 716.6 µs 1,007 µs -40.53%
test_json_any_list_str 1.6 ms 1.8 ms -10.83%
test_json_direct_list_int 907 µs 1,063.3 µs -17.23%
test_json_any_list_int 1.6 ms 1.7 ms -11.12%
test_python_json_list_int 712.6 µs 1,003.2 µs -40.79%
test_python_json_list_none 692.3 µs 984.9 µs -42.25%
test_model_list_core_json 975.1 µs 1,131.4 µs -16.03%

@adriangb adriangb marked this pull request as ready for review June 7, 2023 18:58
@adriangb
Copy link
Member Author

adriangb commented Jun 7, 2023

In my opinion, reliable benchmarks, more consistent performance, and saving developer time chasing down weirdness (see #656 (comment) for context) are well worth a mean 15% performance regression on micro benchmarks. There is also the option of using the default allocator only for benchmarks but keeping MiMalloc for the prod builds which technically might give us the best of both worlds but has the con that benchmark performance would not be reflective of real-world performance (although I think a 10-25% variation between them would be fine as long as it's relatively consistent).

@samuelcolvin what do you think?

@adriangb adriangb force-pushed the default-allocator branch from 7325d42 to dbdf001 Compare June 8, 2023 14:15
@codecov-commenter
Copy link

codecov-commenter commented Jun 8, 2023

Codecov Report

Merging #662 (ac54212) into main (e7108e8) will not change coverage.
The diff coverage is n/a.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #662   +/-   ##
=======================================
  Coverage   93.90%   93.90%           
=======================================
  Files          99       99           
  Lines       13756    13756           
  Branches       25       25           
=======================================
  Hits        12917    12917           
  Misses        833      833           
  Partials        6        6           
Impacted Files Coverage Δ
src/lib.rs 100.00% <ø> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e7108e8...ac54212. Read the comment docs.

- name: Install pydantic-core
# build-prod does not include MiMalloc because it bypasses Maturin
run: |
make build-prod
Copy link
Collaborator

@dmontagu dmontagu Jun 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it more and realized I think the make build-dev thing doesn't work for me locally if it isn't already pip installed in editable mode. (Which is often an issue for me because I switch between released version and locally installed versions sometimes.) Maybe try:

Suggested change
make build-prod
pip install -e .
make build-prod

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How will we know which version is installed then 🥲

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, I guess we need to confirm that the MiMalloc version is what wheels are using

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if it's worth it, but I would imagine you could add an attribute to the pydantic_core._pydantic_core module conditionally based on the mimalloc feature flag? (And then check for it when uploading the wheel if necessary?)

diff --git a/pydantic_core/_pydantic_core.pyi b/pydantic_core/_pydantic_core.pyi
index 6859a16..517d507 100644
--- a/pydantic_core/_pydantic_core.pyi
+++ b/pydantic_core/_pydantic_core.pyi
@@ -21,6 +21,7 @@ from _typeshed import SupportsAllComparisons
 
 __all__ = (
     '__version__',
+    'allocator',
     'build_profile',
     'SchemaValidator',
     'SchemaSerializer',
@@ -35,6 +36,7 @@ __all__ = (
     'list_all_errors',
 )
 __version__: str
+allocator: str
 build_profile: str
 
 T = TypeVar('T', default=Any, covariant=True)
@@ -167,7 +169,8 @@ class Url(SupportsAllComparisons):
     def query(self) -> 'str | None': ...
     @property
     def fragment(self) -> 'str | None': ...
-    def __init__(self, url: str) -> None: ...
+    @staticmethod
+    def __new__(cls, url: str) -> Url: ...
     def unicode_host(self) -> 'str | None': ...
     def query_params(self) -> 'list[tuple[str, str]]': ...
     def unicode_string(self) -> str: ...
diff --git a/src/lib.rs b/src/lib.rs
index 613220a..9e9229e 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -42,9 +42,20 @@ pub fn get_version() -> String {
     version.replace("-alpha", "a").replace("-beta", "b")
 }
 
+#[cfg(feature = "mimalloc")]
+fn get_allocator() -> String {
+    "mimalloc".to_owned()
+}
+
+#[cfg(not(feature = "mimalloc"))]
+fn get_allocator() -> String {
+    "default".to_owned()
+}
+
 #[pymodule]
 fn _pydantic_core(_py: Python, m: &PyModule) -> PyResult<()> {
     m.add("__version__", get_version())?;
+    m.add("allocator", get_allocator())?;
     m.add("build_profile", env!("PROFILE"))?;
     m.add_class::<PySome>()?;
     m.add_class::<SchemaValidator>()?;

(Haven't tested; also maybe it's overkill)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it’s using the default allocator for benchmarks now based just on the slowdowns 😂.

I was thinking something similar but it’s complex. How do you feel about moving forward without that and add ing it later if it’s really needed?

@adriangb adriangb merged commit d2f4226 into main Jun 8, 2023
@adriangb adriangb deleted the default-allocator branch June 8, 2023 21:46
@dmontagu
Copy link
Collaborator

dmontagu commented Jun 8, 2023

@samuelcolvin I'll merge this now since @adriangb and I agree it's good and it sounded earlier like you were okay with this, but just comment if you want more work done or whatever

Edit: oh looks like @adriangb beat me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants