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

Various memory optimizations #94

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Commits on Nov 24, 2024

  1. Configuration menu
    Copy the full SHA
    fb7056b View commit details
    Browse the repository at this point in the history
  2. feat!: use Cow whenever possible instead of owned values

    A lot of strings and arrays used in nftables structures are static, but
    currently we need to do allocations everywhere using owned String and Vec.
    Replace them with Cow so we can pass &'static str or &'static [_] whenever
    possible, while returning owned value when deserializing.
    hack3ric committed Nov 24, 2024
    Configuration menu
    Copy the full SHA
    7922a4a View commit details
    Browse the repository at this point in the history
  3. feat!: reduce stack usage by selectively wrapping large values in Box

    This evens out some of the large enums' sizes (e.g. Expression and Statement's)
    from over 200 bytes to <=144. Further reduction could be done, as this commit
    only changes the obvious ones for now.
    
    Also moves BinaryOperation's Box up one level so it only uses one Box.
    hack3ric committed Nov 24, 2024
    Configuration menu
    Copy the full SHA
    6f5f339 View commit details
    Browse the repository at this point in the history
  4. feat(expr)!: make range fixed-sized array, not slice

    Since we only expect two elements from Range, we can just use array instead.
    hack3ric committed Nov 24, 2024
    Configuration menu
    Copy the full SHA
    ec094c7 View commit details
    Browse the repository at this point in the history
  5. fix(expr)!: revert recursive Cow<[Expression]> back to Vec

    This fails to build on 1.65:
    
    ```
    error[E0277]: the trait bound `[SetItem]: ToOwned` is not satisfied in `Expression`
       --> src/stmt.rs:245:14
        |
    245 |     pub dev: Option<Expression>,
        |              ^^^^^^^^^^^^^^^^^^ within `Expression`, the trait `ToOwned` is not implemented for `[SetItem]`, which is required by `Expression: Sized`
        |
        = help: the trait `ToOwned` is implemented for `[T]`
    note: required because it appears within the type `Expression`
       --> src/expr.rs:10:10
        |
    10  | pub enum Expression {
        |          ^^^^^^^^^^
    note: required by an implicit `Sized` bound in `std::option::Option`
       --> /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/option.rs:572:1
    
    ...and many, many similar errors
    ```
    
    It compiles successfully on Rust >=1.79, so this commit can be reverted once our MSRV reaches it.
    hack3ric committed Nov 24, 2024
    Configuration menu
    Copy the full SHA
    a1c6392 View commit details
    Browse the repository at this point in the history

Commits on Nov 26, 2024

  1. feat!: replace Cow<'static, _> with 'a

    This adds a bunch of lifetimes, but it works with `.into()` when a borrowed value's array container is stored in another place, without the need of `Cow::Borrowed`:
    
    ```
    let objects = [
        NfObject::ListObject(NfListObject::Table(Table {
            family: NfFamily::INet,
            name: "some_inet_table".into(), // here
            handle: None,
        })),
        // ...
    ];
    let expected = Nftables {
        objects: objects.into(); // and here
    };
    ```
    
    However, `Cow::{Borrowed, Owned}` still needs to be used when you want to write the above in one statement.
    hack3ric committed Nov 26, 2024
    Configuration menu
    Copy the full SHA
    f037160 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    dece884 View commit details
    Browse the repository at this point in the history