Skip to content

Replace LinearFifo with builtin ArrayList API#113

Open
bomanaps wants to merge 2 commits intozen-eth:mainfrom
bomanaps:refactor/replace-linear-fifo
Open

Replace LinearFifo with builtin ArrayList API#113
bomanaps wants to merge 2 commits intozen-eth:mainfrom
bomanaps:refactor/replace-linear-fifo

Conversation

@bomanaps
Copy link
Copy Markdown
Contributor

#110. Replaced custom LinearFifo with simple buffer types using std.ArrayList for dynamic mode and direct slice management for fixed mode. Added bounds checking, unit tests, and reset functionality.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the internal buffer management by replacing a custom FIFO implementation with more idiomatic Zig approaches. It introduces dedicated fixed-size and dynamic buffer types built upon standard library components, enhancing the clarity, maintainability, and reliability of byte stream handling throughout the codebase. This change directly addresses a reported issue by adopting standard and well-tested mechanisms for buffer operations.

Highlights

  • Buffer Implementation Replacement: The custom LinearFifo implementation has been replaced with two new buffer types: SliceBuffer for fixed-size buffers using direct slice management, and DynamicBuffer for growable buffers leveraging std.ArrayList(u8).
  • Enhanced Buffer Functionality: The new buffer types include comprehensive methods for read, write, unget, discard, readableLength, readableSlice, and reset, ensuring robust FIFO operations.
  • Improved Robustness and Testing: Bounds checking has been added to buffer operations, and extensive unit tests are now in place for both SliceBuffer and DynamicBuffer to ensure correctness and prevent common buffer-related issues.
  • Integration Across Modules: Existing modules such as multistream/multistream.zig, protocols/mss.zig, and protocols/pubsub/semiduplex.zig have been updated to utilize the new SliceBuffer and DynamicBuffer types, replacing the deprecated LinearFifo.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/byte_buffer.zig
    • Added SliceBuffer for fixed-size FIFO buffering using direct slice management.
    • Added DynamicBuffer for growable FIFO buffering using std.ArrayList(u8).
    • Implemented init, deinit, readableLength, readableSlice, read, discard, unget, write, and reset methods for both buffer types.
    • Included comprehensive unit tests for SliceBuffer and DynamicBuffer.
  • src/linear_fifo.zig
    • Removed the LinearFifo module, as its functionality is now provided by SliceBuffer and DynamicBuffer.
  • src/multistream/multistream.zig
    • Updated import from linear_fifo.zig to byte_buffer.zig.
    • Replaced LinearFifo(u8, .Slice) with SliceBuffer in the Negotiator struct definition.
    • Updated Negotiator.init to use SliceBuffer.init.
    • Replaced linear_fifo.LinearFifo(u8, .Slice) with byte_buffer.SliceBuffer in the MultistreamSelectHandler struct definition.
    • Updated MultistreamSelectHandler.NegotiationSession.init to use byte_buffer.SliceBuffer.init.
  • src/protocols/mss.zig
    • Updated import from linear_fifo.zig to byte_buffer.zig.
    • Replaced *linear_fifo.LinearFifo(u8, .Slice) with *byte_buffer.SliceBuffer in the Negotiator struct.
    • Replaced linear_fifo.LinearFifo(u8, .Slice) with byte_buffer.SliceBuffer in the MultistreamSelectHandler.NegotiationSession struct.
  • src/protocols/pubsub/semiduplex.zig
    • Updated import from linear_fifo.zig to byte_buffer.zig.
    • Replaced linear_fifo.LinearFifo(u8, .Dynamic) with byte_buffer.DynamicBuffer for received_buffer initialization in PubSubPeerProtocolHandler.addStream.
    • Replaced linear_fifo.LinearFifo(u8, .Dynamic) with byte_buffer.DynamicBuffer in the PubSubPeerResponder struct.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a nice refactoring that replaces a custom LinearFifo implementation with SliceBuffer and DynamicBuffer, which are built on top of standard Zig features like slices and std.ArrayList. This simplifies the codebase and improves maintainability. The new implementations are well-tested.

My review includes a couple of suggestions for the new DynamicBuffer to make it even more robust and idiomatic by using more of the std.ArrayList public API, reducing the need to manually manage its internal state.

Comment thread src/byte_buffer.zig
Comment thread src/byte_buffer.zig Outdated
Comment thread src/byte_buffer.zig
@@ -0,0 +1,296 @@
const std = @import("std");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since linear_fifo removed from std, can you check the official release notes to see what can be used to instead it which probably not need introduce this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zig 0.15 recommends std.Io.Reader/Writer as LinearFifo replacements, but those serve stream I/O with underlying sources (files, sockets), not in-memory accumulation buffers requiring unget and readableSlice operations. We use std.ArrayList(u8) with fromOwnedSlice, replaceRange, and clearRetainingCapacity all public stdlib APIs which fits this protocol parsing use case idiomatically any idea on how to properly go about this considering this?

@anshalshukla
Copy link
Copy Markdown
Contributor

I think there are plans to reintroduce in zig 0.16, so maybe we can just wait for the release

@GrapeBaBa
Copy link
Copy Markdown
Member

I think there are plans to reintroduce in zig 0.16, so maybe we can just wait for the release

Great.

@GrapeBaBa
Copy link
Copy Markdown
Member

@bomanaps we may not need this change and wait until it back

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.

3 participants