Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Implement Number Protocol#3507

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
youknowone merged 7 commits intoRustPython:mainfromqingshi163:number-protocol
May 29, 2022

Conversation

qingshi163
Copy link
Contributor

@qingshi163qingshi163 commentedDec 20, 2021
edited by youknowone
Loading

number protocol of#3244

moreal, youknowone, and Snowapril reacted with thumbs up emoji
@youknowone
Copy link
Member

@qingshi163 what have to be done to finish this PR?

@youknowone
Copy link
Member

awesome!

Copy link
Member

@youknowoneyouknowone left a comment
edited
Loading

Choose a reason for hiding this comment

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

I have questions about cow and OnceCell part, but it looks working!
This is the way to go and we can fix other stuff later.

Do you have remaining plans to do on this PR?

@@ -0,0 +1,242 @@
use std::borrow::Cow;

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

@@ -263,7 +265,10 @@ impl Constructor for PyInt {
val
};

try_int(&val, vm)
// try_int(&val, vm)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// try_int(&val, vm)

pub trait AsNumber: PyPayload {
#[inline]
#[pyslot]
fn slot_as_number(zelf: &PyObject, vm: &VirtualMachine) -> Cow<'static, PyNumberMethods> {
Copy link
Member

Choose a reason for hiding this comment

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

Does this slot need to return Cow? Which part prevent this to be&'static PyNumberMethods?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I think for heap object we need to use Cow::Owned witch implemented in as_number_wrapper

Copy link
Member

Choose a reason for hiding this comment

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

They are not decided by object, but decided by type. Then I think the type can hold a clonedPyNumberMethods with edited values. instead ofCow for every object whenas_number is called.

Copy link
Member

Choose a reason for hiding this comment

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

I found AsMapping has the same mechanism. Then it is ok for now.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Yes, it should be stored in the type struct, but we can fix it later

youknowone reacted with thumbs up emoji
@qingshi163
Copy link
ContributorAuthor

There is quite a lot of things not done yet, i think all the mathematic operations should be implemented via number protocol

@youknowone
Copy link
Member

How about finishing this PR as current state as core implementation of AsNumber trait, and working on other types in following other PRs?
Keeping a huge PR will be a burden to maintain (to rebase everything after other merge)

@youknowone
Copy link
Member

@qingshi163 are you going to make this ready for review? or do you want to do more work on it?

@qingshi163
Copy link
ContributorAuthor

I am still busy with it

@qingshi163qingshi163 marked this pull request as ready for reviewMay 29, 2022 06:53
@youknowoneyouknowone changed the title[WIP] Implement Number ProtocolImplement Number ProtocolMay 29, 2022
Copy link
Member

@youknowoneyouknowone left a comment

Choose a reason for hiding this comment

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

I think#3750 changes also be able to be adapt here.
They can be done later, but I attached a commit simplifyingAsNumber interfaces.

if let Some(f) = val.try_to_f64(vm)? {
f
if cls.is(vm.ctx.types.float_type) && val.class().is(PyFloat::class(vm)) {
unsafe { val.downcast_unchecked::<PyFloat>().value }
Copy link
Member

Choose a reason for hiding this comment

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

here is a regression. val object will not be reused even for same type.

negative: Some(|number, vm| {
Self::number_downcast(number)
.value
.clone()
Copy link
Member

Choose a reason for hiding this comment

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

https://docs.rs/num/latest/num/struct.BigInt.html#impl-Neg-1

Neg is also implemented for&BigInt. This clone is redundant.

absolute: Some(|number, vm| Self::number_downcast(number).value.abs().to_pyresult(vm)),
boolean: Some(|number, _vm| Ok(Self::number_downcast(number).value.is_zero())),
invert: Some(|number, vm| {
let value = Self::number_downcast(number).value.clone();
Copy link
Member

Choose a reason for hiding this comment

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

Comment on lines +14 to +13
/* Number implementations must check *both*
arguments for proper type and implement the necessary conversions
in the slot functions themselves. */
Copy link
Member

Choose a reason for hiding this comment

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

Does this mean we need to check the first argument(&PyNumber) is a correct PyNumber object?
In this case, I feel like the type will fit better in&PyObject rather than&PyNumber

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

The comment is from cpython but as the source shows that cpython only does the check for binary operations which could be called invertly but assume the type is correct for other functions

youknowone reacted with thumbs up emoji
@youknowone
Copy link
Member

+-----------+----------------------------------+-----------------------------------+----------------------------------------+| Benchmark | benches/out/cpython38/nbody.json | benches/out/rustpython/nbody.json | benches/out/rustpython-3507/nbody.json |+===========+==================================+===================================+========================================+| nbody     | 103 ms                           | 1.79 sec: 17.39x slower           | 1.82 sec: 17.70x slower                |+-----------+----------------------------------+-----------------------------------+----------------------------------------+

@youknowone
Copy link
Member

at least, no regression after using references

+-----------+----------------------------------+-----------------------------------+----------------------------------------+--------------------------------------------+| Benchmark | benches/out/cpython38/nbody.json | benches/out/rustpython/nbody.json | benches/out/rustpython-3507/nbody.json | benches/out/rustpython-3507/nbody-ref.json |+===========+==================================+===================================+========================================+============================================+| nbody     | 103 ms                           | 1.79 sec: 17.39x slower           | 1.82 sec: 17.70x slower                | 1.79 sec: 17.38x slower                    |+-----------+----------------------------------+-----------------------------------+----------------------------------------+--------------------------------------------+

@youknowoneyouknowoneforce-pushed thenumber-protocol branch 2 times, most recently from1b9b663 to7a61d07CompareMay 29, 2022 22:34
@youknowoneyouknowone merged commitc40bc62 intoRustPython:mainMay 29, 2022
@qingshi163qingshi163 deleted the number-protocol branchJuly 3, 2022 19:19
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@youknowoneyouknowoneyouknowone approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@qingshi163@youknowone

[8]ページ先頭

©2009-2025 Movatter.jp