-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Add a circular buffer cirbuf
module
#1062
Conversation
The cirbuf module provides a simple, in memory, [circular buffer](https://en.wikipedia.org/wiki/Circular_buffer). This simplifies the design of typical workloads like the sampling of sensors at regular intervals. The memory usage is efficient, apart from ~20 bytes for administration of the circular buffer no additional overhead is required to store the data.
I had some troubles with git, so I deleted my hve/nodemcu tree and reforked in order to get a single commit. so this is related to pull request #1060 sorry for that. |
Also contemplating to introduce read handles... which permit multiple independent readers on the same buffer. For example when we have multithreading in nodemcu. h = cirbuf.new(num,len)
rh, buf = cirbuf.readstart(h [, maxnum])
buf = cirbuf.readnext(rh, [,maxnum]) state of the reader is maintained in the rh handle... or we could use this:
rh = cirbuf.open(h)
buf = cirbuf.read(rh, [,maxnum]) Maybe this is even better... |
|
||
## cirbuf.readnext() | ||
|
||
Reads from the beginning of buffer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be next position (and the syntax suffers from block-copy-itis as well)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes you're right I will fix it.
I'm not very happy about the API -- it seems that there is a confusion between strings and numbers, and, in order to use it, you need to understand how it works. You can push in one or more elements at a time. This is fine, but they don't come out like that. They come out in a blob (and it isn't clear that you are guaranteed to get a whole number of elements out). What happens with float values? It seems that this code is combining the circular buffer aspect with the structure packing aspect, and that seems wrong to me. There is the |
In my view the idea of a circular buffer is a good one, but I don't like this implementation. Better in my view just to have string argument (not a fixed length) with |
@pjsg The backend converts numbers to 4 bytes in LE format. I wanted to be able to combine the use of strings and numbers. As numbers seemed to be more easily manipulated in Lua:
I do agree that the current cirbuf has to many shortcuts in order to fit my use case .... |
@hvegh, your case is probably at the extreme end of the possible applications. If we limit the Lua string size to 256 then this is only a 1-byte overhead per string or we can go for a simple high bit multi-byte encoding allowing longer strings with an overhead of 2 bytes for strings over 128 bytes, and the possible usecases would be far broader. We really need a broader application for it to be a standard library, IMO. |
Fair enough, it was purely intended as a sensor buffer.
The circular buffer objects are fixed size, variable size buffer objects would add a level of complexity which the current code does not cover.
Wouldn't it be more convenient to handle the writing to SPIFFS in lua (Otherwise you might risk including SPIFFS specifics in your buffer module...)?? I would be totally willing to put some effort in changing the module, but for me it is unclear what for you guys a minimum acceptable product is. What would be the first acceptable increment of the code? |
After this length of time, I think that we should close this. |
@marcelstoer, I've got my own history library which is more flexible for my needs, and I am absolutely strapped for free time, so I don't want to spend any time on this. So this is really one for Phil to decide -- PS. Sorry missed Phil's comment so +1 to closing. |
If I look at your previous comments (Philips' and Terry's) I conclude that you both think it'd be worthwhile to have this module. However, you weren't happy with the initial implementation. Henk offered to update the code so that you'd accept it. I'd feel bad for Henk to just close it like that. |
@marcelstoer, OK, but Henk rejected my comments, so I just decided that it was easier to write my own library rather than have an argument, which I did. Now I've done that and got it working fine. I don't mind keeping this open, but my priority on this is still low, now that I have my own version. Sorry. |
Regarding:
#1062 (comment)
@terry, Don't want to spoil the party, looking forward to your "history"
module.
Cheers
|
Terry, I only just realized that your PR #1199 is a substitute for this one, sorry. |
The cirbuf module provides a simple, in memory, circular buffer. This simplifies the design of typical workloads like the sampling of sensors at regular intervals. The memory usage is efficient, apart from ~20 bytes for administration of the circular buffer no additional overhead is required to store the data.