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

are there workarounds to avoid copying function non-array arguments? #2012

Open
gdementen opened this issue Aug 2, 2022 · 2 comments
Open

Comments

@gdementen
Copy link
Contributor

gdementen commented Aug 2, 2022

Hello. Thanks for the great library.

I have code that looks like this:

#pythran export look_pythran(int[], int:int dict)
#pythran export look_pythran(int list, int:int dict)
def look_pythran(key, mapping):
    if isinstance(key, np.ndarray):
        return np.array([mapping[k] for k in key])
    else:
        return [mapping[k] for k in key]

Using small mappings, the performance is very nice, especially for large keys... but for large mappings, Pythran is much slower than the pure Python version. I suppose this is because Pythran copies the arguments (https://pythran.readthedocs.io/en/latest/MANUAL.html#limitations) but I haven't found a way to workaround this.

For example,

  • is there a way to prepare a "Pythranized" mapping once and either pass that as argument or via a global value, so that the mapping is copied only once instead of for every function call?
  • is there a way to pass mapping.__getitem__ as argument instead of the mapping itself (but I don't know how to express that type in Pythran)?
  • is there a way to mark the mapping as read-only so that Pythran can use it as-is instead of first copying it?

Thanks for any help

@serge-sans-paille
Copy link
Owner

Interesting question, sorry for not answering earlier. There's currently no straight-forward way, but it would be great to have one, indeed. Among the three solution you propose, the first one makes most sense to me. I wonder what would be the best syntax to support this... probably a dedicated type?

@gdementen
Copy link
Contributor Author

Hmm. FWIW, I never answered this because I agree with you... But sometimes an explicit answer is better than an implicit one 😉.

IMO, the dedicated type is indeed the best you could offer, as it would make it possible to control when the conversion actually happens and/or when a conversion needs to happen again, whereas something like a "cached" modifier for the argument in the pythran function annotation (the only other option I see -- but I might be short-sighted on this) would only support caching when first-called. But maybe the "cached" argument modifier could lead to more optimizations being possible?? In that case, you could support both.

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

2 participants