Skip to content
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

[stdlib] Add a bunch of new features to object (dict, tuple, __iter__, structs!) #3716

Draft
wants to merge 53 commits into
base: nightly
Choose a base branch
from

Conversation

rd4com
Copy link
Contributor

@rd4com rd4com commented Oct 26, 2024

Hello, it would be really nice to use our object a lot more,
it is the default type in def functions and makes programming easy,
hope this pr is good, it has a dict implementation for both K and V of object,
really looking forward to use the features of the pr everyday:

  • simplify a lot by removing structs that wrapped other structs
    uses Arc in in variant for ref count (list, dict, attributes, string)
  • add dict and tuple with ref count ❤️‍🔥
  • implement repr for RefCountDict and RefCountTuple
  • add hash for Self
  • implement eq for RefCountAttrsDict, RefCountDict and RefCountTuple
  • add _ObjectImpl.ref_count() for RefCounted* structs
  • add contains for list, dict and tuple
  • add WrappedStruct
  • add pop for list and dict
  • add len for dict and tuple
  • add int("1") conversion to int
  • str(x) returns "value" instead of " 'value' "
  • add object(List(Attr("values",[1, 2])))

Can anybody check a benchmark for the matmul example ?
because changes might be slower or faster (not tested)

With this that now works:

    a = object.dict()
    a["one"] = 1
    a[2] = "two"
    assert_equal(a["one"], 1)
    assert_equal(a[2], "two")

We can then implement type(x) is type(y) !
json could also be an exciting next step,
then we could have a hybrid between dynamic and typed:
a.typed[Int]() += SIMD[Int, 4]().reduce_add()

Hope you like this pr, if it's not good enough no worries lol
(no merge 👍 or we could even refine with huge reviews if needed)

usecase is to transition from
def event(inout self, props: PythonObject)
to
def event(inout self, props)
in a web-framework i'm trying to create

@rd4com rd4com requested a review from a team as a code owner October 26, 2024 21:12
Copy link
Contributor

@martinvuyk martinvuyk left a comment

Choose a reason for hiding this comment

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

Hi @rd4com this looks good. Left some nitpicks on the usages of dunders that can be changed to builtin functions.

stdlib/src/builtin/object.mojo Outdated Show resolved Hide resolved
stdlib/src/builtin/object.mojo Outdated Show resolved Hide resolved
stdlib/src/builtin/object.mojo Outdated Show resolved Hide resolved
stdlib/src/builtin/object.mojo Outdated Show resolved Hide resolved
stdlib/src/builtin/object.mojo Outdated Show resolved Hide resolved
stdlib/src/builtin/object.mojo Outdated Show resolved Hide resolved
stdlib/src/builtin/object.mojo Outdated Show resolved Hide resolved
stdlib/src/builtin/object.mojo Outdated Show resolved Hide resolved
stdlib/src/builtin/object.mojo Outdated Show resolved Hide resolved
stdlib/src/builtin/object.mojo Outdated Show resolved Hide resolved
@rd4com rd4com marked this pull request as draft October 30, 2024 15:57
@rd4com rd4com changed the title [stdlib] Add a bunch of new features to object (dict!) [stdlib] Add a bunch of new features to object (dict and tuple!) Oct 30, 2024
stdlib/src/builtin/object.mojo Outdated Show resolved Hide resolved
stdlib/src/builtin/object.mojo Outdated Show resolved Hide resolved
stdlib/src/builtin/object.mojo Show resolved Hide resolved
stdlib/src/builtin/object.mojo Outdated Show resolved Hide resolved
@rd4com rd4com changed the title [stdlib] Add a bunch of new features to object (dict and tuple!) [stdlib] Add a bunch of new features to object (dict, tuple, __iter__!) Nov 5, 2024
@rd4com
Copy link
Contributor Author

rd4com commented Nov 5, 2024

Hello @JoeLoser , last review is implemented !
Note that with this draft that uses ref, users can do:

my_list._value.get_as_list()[x._value.get_as_int()]._value.get_as_list()[y._value.get_as_int()] = z
#List.__getitem__[0]().__getitem__[1]() = z

# edit: with latest commits
my_list.as_list()[x.as_int()].as_list()[y.as_int()] = z

It is super important, because the lifetime/origin is propagated through,
list, dict, tuple, attrdict, are implemented as List[object],Dict.., so we can enjoy a ref to an object !
We might need our arc to share the PythonObject ref_count so objects can live in both world at the same time,
and the mojo side would be able to use a ref to an arc innervalue 👍
(ping to @ConnorGray )

Last commit also have __iter__ for both list and dict keys ❤️‍🔥
(it works by storing the iterator in a variant)

rd4com and others added 22 commits November 8, 2024 14:51
Signed-off-by: rd4com <[email protected]>
Signed-off-by: rd4com <[email protected]>
Co-authored-by: martinvuyk <[email protected]>
Signed-off-by: rd4com <[email protected]>
Co-authored-by: martinvuyk <[email protected]>
Signed-off-by: rd4com <[email protected]>
Co-authored-by: martinvuyk <[email protected]>
Signed-off-by: rd4com <[email protected]>
Co-authored-by: martinvuyk <[email protected]>
Signed-off-by: rd4com <[email protected]>
Co-authored-by: martinvuyk <[email protected]>
Signed-off-by: rd4com <[email protected]>
Co-authored-by: martinvuyk <[email protected]>
Signed-off-by: rd4com <[email protected]>
Co-authored-by: martinvuyk <[email protected]>
Signed-off-by: rd4com <[email protected]>
Co-authored-by: martinvuyk <[email protected]>
Signed-off-by: rd4com <[email protected]>
Co-authored-by: martinvuyk <[email protected]>
Signed-off-by: rd4com <[email protected]>
Co-authored-by: martinvuyk <[email protected]>
Signed-off-by: rd4com <[email protected]>
There is: `fn __init__[dt: DType](inout self, value: SIMD[dt, 1])`

Signed-off-by: rd4com <[email protected]>
rd4com and others added 24 commits November 8, 2024 14:51
…tring`, test cow for `__iadd__`

Signed-off-by: rd4com <[email protected]>
Signed-off-by: rd4com <[email protected]>
Co-authored-by: Joe Loser <[email protected]>
Signed-off-by: rd4com <[email protected]>
…_`, `__gt__`, `__lt__`, `__ge__`, `__le__`

Signed-off-by: rd4com <[email protected]>
@rd4com rd4com changed the title [stdlib] Add a bunch of new features to object (dict, tuple, __iter__!) [stdlib] Add a bunch of new features to object (dict, tuple, __iter__, structs!) Nov 11, 2024
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