-
Notifications
You must be signed in to change notification settings - Fork 2.5k
problems with _foreach #1384
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
Comments
Check out the Regarding the other cases, usually we fall back to foreach style iterators if externalizing state as an index value is too expensive or complicated. In the case of refs for example, you may be iterating through compressed refs or directory entries on disk where the state cannot easily be presented as just a number. That's not to say it can't be fixed. For example, you could create a ref iterator object that encapsulated the in-progress state and allowed a non-callback based interface (although the recent work to allow plug in ref stores will probably make that much harder /cc @ethomson ). PRs are always welcome! |
Well the iterator does not have to be indexed-based, a |
Thinking about the refs... both the hash implementation and the filesystem support iterating through them, so even though we don't quite know the amount we have on the filesystem, we could create an iterator, as both khash and the syscalls let us implement Presumably any sensible database implementation you'd use as a backend would let you have a similar iterator. |
These iterations would have to be supported by each backend, but that shouldn't be that much of an issue. I keep being confused about yield in C#, @nulltoken would C# also benefit from such an iteration? |
Sir. Yes, Sir!
Implementing yield with I can think of only one potential issue related to the Current |
I may not have communicated clearly that I understand your complaint about the _foreach pattern, but I think if you look at it, the
Iterating over the diff can be index based because we have definitive knowledge of the number of changed files (and the number of hunks and lines in the text diff). Regarding the many other uses of
Making a Looking over the notes code, I think it could be improved by creating an iterator object. Currently it is doing allocated for each invocation of the callback function and the natural iterator implementation would reuse the data buffer for each successive output, I think. From what I can see, it would not be too hard to implement. Anyhow... That's just a few items. Sounds like some great small-ish projects in there. BTW, I think most of the usage of the |
As the note iterator is already merged and carlos is working on a reference iterator, I think we can close this issue. |
Hey,
like a year ago or more there were some design changes in the way iterations are handled in libgit2. Now there exist two ways of getting all the entries of a container for example like reflogs, trees or references. For reflog you do it the old for-loop style with finding out the size of the container and then accessing the entry itself with an index. For notes in contrast you have to provide callback function and libgit2 iterates through the container and calls for each iteration step the provided callback. In the following some examples:
*_entrycount
/*_entry_by_index
*_foreach
In pygit2 the latter iteration handling is gettig us into trouble. As some may know in python, in comparison for example to ruby, you do not have blocks. For iterating you use for-loops or list comprehensions. So generally you do not use anonymous functions (callbacks) for each iteration step. Therefor it is really hard to provide a binding for these new
_foreach
-functions efficiently. We have to iterate through the whole list, make a PyList-object out of it and give it back to python. So if a user only wants the first two note entries this is a huge disadvantage (it is not lazy). However for_entrycount
/_entry_by_index
we can build a generator object because here we only have to provide anext()
method. This is not possible for these foreach-iterators because we can not jump out of our called callback to our caller function. For an implemenation example you can have a look atRefLogIter_iternext()
in src/reflog.c andDiff_changes__get__
in src/diff.c.So the resulting question is: Is there any possibility to use these foreach-iterators in an old fashioned way? (Even with setjmp.h it seems not possible - not that I would want to abuse them for this...) Or is it possible to provide as well
_entrycount
-/_entry_by_index
- besides_foreach
-functions?I think several bindings could run into this issue (except they are callback based or have yield in the binding level layer)
Greeting
Nico
The text was updated successfully, but these errors were encountered: