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

Request (NOT bug): Nim Iterator export to Python Generator #103

Closed
UNIcodeX opened this issue Mar 19, 2019 · 1 comment
Closed

Request (NOT bug): Nim Iterator export to Python Generator #103

UNIcodeX opened this issue Mar 19, 2019 · 1 comment

Comments

@UNIcodeX
Copy link

UNIcodeX commented Mar 19, 2019

Would it be possible to add iterator support, writing a Python "generator" as a Nim "iterator"? In my estimation, this would allow for more efficient Nim based Python libraries to be exported, rather than having to completely iterate and return a full dataset at instantiation, the iterator can lazily load when the next item is needed.

Proposal of requested functionality:

# test_lib.nim
import nimpy, tables, json

iterator myRange*(s, e: int): int {.exportpy.} =
  for num in s .. e:
    yield num

iterator loadJSON*(s: string): Table[string, string] {.exportpy.} =
  var t = initTable[string, string]()
  for i in parseJson(s):
    for key, val in i:
      t[key] = $val
    yield t
from test_lib import myRange, loadJSON

for i in myRange(1, 50):
print(i)

strJSON = """
[{
    "userId": 1,
    "name": "Ada Lovelace"
  },
  {
    "userId": 2,
    "name": "Blaise Pascal"
  }
]"""

for i in loadJSON(strJSON):
  print(i)
>>> [num for num in myRange(1, 50) if num % 2 == 0]
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50]

>>> [i for i in loadJSON(strJSON)]
[{'userId': 1, 'name': 'Ada Lovelace'}, {'userId': 2, 'name': 'Blaise Pascal'}]
@UNIcodeX
Copy link
Author

UNIcodeX commented Apr 2, 2019

Awesome!!! Great work!!

Something I noticed in my testing on my PCPC repo is that it comes in as type class whereas a native Python generator comes in as type generator. A Nuitka generator shows type compiled_generator. This could be only due to semantics, but I'm wondering if this affects performance.

Do you know?

Implementing a generator / iterator in each of these solutions yields the following:

Running benchmark 'json_loads'.
---------------------------------------------------------
Python : 0.7985s                                    1.00x
Nuitka : 0.7754s                                    1.03x
Cython : 0.7697s                                    1.04x
Numba  : 2.9050s                                    0.27x
Nim    : 2.5040s                                    0.32x

Example with pre-built x86_64 Linux binaries (built on Arch).
json_loads.zip

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

No branches or pull requests

1 participant