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

Use prefix increment in make_iterator #247

Merged
merged 2 commits into from
Jun 21, 2016
Merged

Conversation

aldanor
Copy link
Member

@aldanor aldanor commented Jun 17, 2016

This patch switches to using prefix increment for iterators returned by make_iterator. This is generally a good idea since postfix increment involves creating a temporary object which may sometimes be expensive.

Btw I've just noticed there's been another related PR (#183) which seems to have been abandoned.

@wjakob
Copy link
Member

wjakob commented Jun 21, 2016

Both implementations (pre and postfix) will make a copy of the object (rather than returning a temporary), since the return type of the lambda function is ValueType. When the iterable stores pointers, this means a copy of the pointer (i.e. no costly copy of the pointee).

(So it's not clear to me how this is different/better than the prior approach -- can you clarify?)

@aldanor
Copy link
Member Author

aldanor commented Jun 21, 2016

@wjakob

  1. The assumption that copying pointee is always cheaper than pointing the iterator is not always true. I'm currently working on a codebase where the yielded value type is simple (like an int), but the iterator itself contains a shared_ptr around a handle that wraps a C-level FFI pointer. This way, copying the iterator is more costly and is completely uncalled for.
  2. pybind11 is the first library I've encountered (STL, Streams, cppitertools, a bunch of others) that requires implementing postfix++, anywhere else (including language constructs like range for, etc) just implementing a prefix increment is entirely sufficient.

@wjakob
Copy link
Member

wjakob commented Jun 21, 2016

ok -- I think those are good reasons. Thanks!

@wjakob wjakob merged commit 407c292 into pybind:master Jun 21, 2016
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.

2 participants