#replacement for LinearFifo

1 messages · Page 1 of 1 (latest)

cunning burrow
#

i just want to be able to readItem() T and writeItem(T). seems like maybe the answer for dynamic would be to use Io.Writer.Allocating and mem.toBytes() or other similar methods from mem? and then for static use a Io.Writer.Fixed?

i haven't been following the Io changes too closely. is there a generic LinearFifo replacement already or in the works?

#

i know i could use an array list but was hesitant since orderedRemove(0) is O(N)

#

and i'm hoping for something thats more efficient.

light egret
#

if your not using the reader/writer it still works due to zigs lazy evaluation

cunning burrow
#

you mean LinearFifo still works? i'm using 0.15.0-dev.1149+4e6a04929 and its been removed.

light egret
#

i just updated my master to the same version, its still there

#

std.fifo.LinearFifo

cunning burrow
#

i guess you're right. anyway i think LinearFifo will be removed soon.

#

and if thats the case, i'd prefer to use something else.

light egret
#

you can just copy the implementation and update the parts that break

cunning burrow
#

thats not what i want to do. thanks

#

i'm more curious about learning to use the new Io api to do this.

elfin kiln
#

isn't the new way reader.takeStruct(T, .endian) ?

cunning burrow
#

hmm maybe. likely i need to sit down and just trial and error try to implement some of this stuff to learn more.

#

that works for structs. but they need to have well defined memory layout i think. and then i'd need to use takeInt(T) if that exists.

#

but i'm still looking for ideas if anyone has them. and hoping for news when/if std will get a generic ring buffer.

tired monolith
finite onyx
#
Similarly, std.RingBuffer is removed since it was only used by the zstd implementation which has been upgraded to use New std.Io.Writer and std.Io.Reader API.
#

Thinking about it, I think if you wanted to write some data to somewhere and then do something with it you can call .buffered() on something to get what you wrote

#

The writer I'm assuming?

#

What did you have before?

tired monolith
#

Here's where I was using LinearFifo:

    event_queue: std.fifo.LinearFifo(sdl3.events.Event, .{ .Static = event_queue_size }),
    frame_times: std.fifo.LinearFifo(u64, .{ .Static = frame_time_logging_interval }),


...
    var paint_total: u64 = 0;
    while (app_state.frame_times.readItem()) |interval| {
        paint_total += interval;
    }
...
app_state.frame_times.writeItemAssumeCapacity(frame_time);

finite onyx
#

So a queue right?

tired monolith
#

Yeah

finite onyx
#

While I too am unclear on how exactly you would use Writer as specifically a RING buffer, a queue I think is possible via Writer.Allocating or Writer.fixed(&buf).

#

Haven't tried it though

tired monolith
#

Okay ty for the tips! I'll take a look at those and post back if I get something working

tough crypt
#

maybe with a fixed writer and a fixed reader on its buffer?? but the seek positions wouldn’t move right

elfin kiln
#

i mean you could always copy it into your project

tired monolith
#

First draft 😆 :

var w = std.io.Writer.fixed(&frame_time_buffer);

...

    const frame_time = sdl3.timer.getNanosecondsSinceInit() - current_ns_since_init;
    // RingBuffer
    w.writeInt(u64, frame_time, .little) catch |err| switch (err) {
        error.WriteFailed => {
            w.end = 0;
        },
    };
    try w.flush();

Is this dumb or reasonable?:

    // RingBuffer
    if (w.end + @sizeOf(u64) >= frame_time_buffer.len) {
        w.end = 0;
    }
    try w.writeInt(u64, frame_time, .little);
elfin kiln
#

can't you get that with a []u64 and an index?

tired monolith
# elfin kiln can't you get that with a ``[]u64`` and an index?

Yeah, that's what I did originally, then I discovered std.fifo.LinearFifo and it removed the responsibility of me having to worry about wrapping around, I just wrote and it did that part. My frame time example is dumb since it's easy to just use frame % frame_time_buffer.len, but the events are nice to have as a ring buffer or deque.

I think I'm just confused since RingBuffers are called out explicitly (in https://ziglang.org/download/0.15.1/release-notes.html#New-stdIoWriter-and-stdIoReader-API), it's mentioned how the std had like 10 of them, so I assumed there's a replacement. Or if there's not, and this is something we need to do, I'd like to know that is the direction of the language.

Maybe it's being cheeky?

elfin kiln
#

There’s a Deque PR that might be the future story

cunning burrow
#

its here incase anyone wants to follow https://github.com/ziglang/zig/pull/24968. i was thinking about suggesting to add Io.Reader/Writer to this. not sure how welcome that would as is be since this only supports push/popping one item at a time and lacks something like pushSliceFront(items) which might make adding a Writer api more efficient.

#

but i did manage go get isaac to add iterator() and at() methods.