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

initial stab at docs for typed-list #7

Merged
merged 5 commits into from
Jul 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/source/reference/deprecation.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _deprecation:

===================
Deprecation Notices
===================
Expand Down
82 changes: 82 additions & 0 deletions docs/source/reference/pysupported.rst
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,16 @@ The following operations are supported:
list
----


.. warning::
As of version 0.45.x the internal implementation for the list datatype in
Numba is changing. Until recently, only a single implementation of the list
datatype was available, the so-called *reflected-list* (see below).

Choose a reason for hiding this comment

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

Perhaps reference the documentation on reflection?

Copy link
Owner Author

Choose a reason for hiding this comment

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

The documentation for the that is almost exactly below, I am hoping this relative reference should suffice.

Copy link
Owner Author

Choose a reason for hiding this comment

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

What I can do, is reference the deprecation schedule.

However, it was scheduled for deprecation from version 0.44.0 onwards due
to its limitations. As of version 0.45.0 a new implementation, the
so-called *typed-list* (see below), is available as an experimental
feature. For more information, please see: :ref:`deprecation`.

Creating and returning lists from JIT-compiled functions is supported,
as well as all methods and operations. Lists must be strictly homogeneous:
Numba will reject any list containing objects of different types, even if
Expand Down Expand Up @@ -260,6 +270,78 @@ of this limitation.
List sorting currently uses a quicksort algorithm, which has different
performance characterics than the algorithm used by Python.

Typed List
''''''''''

.. note::
``numba.typed.List`` is an experimental feature, if you encounter any bugs in
functionality or suffer from unexpectedly bad performance, please report
this, ideally by opening an issue on the Numba issue tracker.

As of version 0.45.0 a new implementation of the list data type is available,
the so-called *typed-list*. This is compiled library backed, type-homogeneous
list data type that is an improvement over the *reflected-list* mentioned
above. Additionally, lists can now be arbitrarily nested. Since the
implementation is considered experimental, you will need to import it
explicitly from the `numba.typed` module::

In [1]: from numba.typed import List

In [2]: from numba import njit

In [3]: @njit
...: def foo(l):
...: l.append(23)
...: return l
...:

In [4]: mylist = List()

In [5]: mylist.append(1)

In [6]: foo(mylist)
Out[6]: ListType[int64]([1, 23])


.. note::
As the typed-list stabilizes it will fully replace the reflected-list and the
constructors `[]` and `list()` will create a typed-list instead of a
reflected one.


Here's an example using ``List()`` to create ``numba.typed.List`` inside a
jit-compiled function and letting the compiler infer the item type:

.. literalinclude:: ../../../examples/typed_list_usage.py
:language: python
:caption: from ``ex_inferred_list_jit`` of ``examples/typed_list_usage.py``
:start-after: magictoken.ex_inferred_list_jit.begin
:end-before: magictoken.ex_inferred_list_jit.end
:dedent: 4
:linenos:

Here's an example of using ``List()`` to create a ``numba.typed.List`` outside of
a jit-compiled function and then using it as an argument to a jit-compiled
function:

.. literalinclude:: ../../../examples/typed_list_usage.py
:language: python
:caption: from ``ex_inferred_list`` of ``examples/typed_list_usage.py``
:start-after: magictoken.ex_inferred_list.begin
:end-before: magictoken.ex_inferred_list.end
:dedent: 4
:linenos:

Finally, here's an example of using a nested `List()`:

.. literalinclude:: ../../../examples/typed_list_usage.py
:language: python
:caption: from ``ex_nested_list`` of ``examples/typed_list_usage.py``
:start-after: magictoken.ex_nested_list.begin
:end-before: magictoken.ex_nested_list.end
:dedent: 4
:linenos:

.. _pysupported-comprehension:

List comprehension
Expand Down
75 changes: 75 additions & 0 deletions examples/typed_list_usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Contents in this file are referenced from the sphinx-generated docs.
# "magictoken" is used for markers as beginning and ending of example text.


def ex_inferred_list_jit():
# magictoken.ex_inferred_list_jit.begin
from numba import njit
from numba.typed import List

@njit
def foo():
# Instantiate a typed-list
l = List()
# Append a value to it, this will set the type to int32/int64 (depending on platform)
l.append(42)
# The usual list operations, getitem, pop and length are supported
print(l[0]) # 42
l[0] = 23
print(l[0]) # 23
print(len(l)) # 1
l.pop()
print(len(l)) # 0
return l

mylist = foo()

# magictoken.ex_inferred_list_jit.end


def ex_inferred_list():
# magictoken.ex_inferred_list.begin
from numba import njit
from numba.typed import List

@njit
def foo(mylist):
for i in range(10, 20):
mylist.append()
return mylist

# Instantiate a typed-list, outside of a jit context
l = List()
# Append a value to it, this will set the type to int32/int64 (depending on platform)
l.append(42)
# The usual list operations, getitem, pop and length are supported
print(l[0]) # 42
l[0] = 23
print(l[0]) # 23
print(len(l)) # 1
l.pop()
print(len(l)) # 0

# And you can use the typed-list as an argument for a jit compiled function

l = foo(l)
print(len(l)) # 10

# magictoken.ex_inferred_list.end


def ex_nested_list():
# magictoken.ex_nested_list.begin
from numba.typed import List

# typed-lists can be nested in typed-lists
mylist = List()
for i in range(10):
l = List()
for i in range(10):
l.append(i)
mylist.append(l)
# mylist is now a list of 10 lists, each containing 10 integers
print(mylist)

# magictoken.ex_nested_list.end