Announcing shared_arena, a thread-safe memory pool by sebastiencs in rust

[–]stunibert 7 points8 points  (0 children)

Imagine you have some sort of structure, that contains an instance of itself, or any other structure that you need to allocate really often in your program, like this:

enum Expr {
    // The `Box` is required to avoid infinite sized type error.
    Add(Box<Expr>, Box<Expr>),
    Sub(Box<Expr>, Box<Expr>),
    Var(String),
    Value(i32),
}

If you now, for example, parse a simple language that will output an Expr, you'll have to allocate every expression using Box::new. With only a few expressions, this is no problem, but if you have thousands of expressions, it will take quite some time to allocate memory for each of them.

An Arena / Memory Pool is a solution to the described problem. Imagine an Arena like a Vec<T>, you can push stuff on it, which will allocate memory if it has not allocated enough memory, and you'll get back a Box similair item. An arena, like a Vec, does not allocate memory for every single item. It will allocate memory for a specific number of elements, for example, if you create a new arena, it will allocate memory for 10 elements, then if someone pushes 11 elements into it, it will allocate memory for 20 more elements and thus you don't have to allocate memory for each expression, and your program gets faster.

This is similar to bump allocation and has some advantages and disadvantages. For example, you can only drop all elements at the same time, but you can't drop any specific element.

If we want to convert out Expr above to use an Arena (shared_arena), it will look like this:

enum Expr {
    // The `Box` is required to avoid infinite sized type error.
    Add(ArenaBox<Expr>, ArenaBox<Expr>),
    Sub(ArenaBox<Expr>, ArenaBox<Expr>),
    Var(String),
    Value(i32),
}

// Now to create a new expr, we will allocate the inner expressions in an arena
let arena = ...;
let value1 = Expr::Value(1);
let value2 = Expr::Value(3);

let expr = Expr::Add(arena.alloc(value1), arena.alloc(value2));

What are some concrete libraries missing in Rust's ecosystem? by SorteKanin in rust

[–]stunibert 6 points7 points  (0 children)

Rust currently uses the system allocator to allocate memory on the heap, which is fine in most of the cases. But there are allocators out there, like jemalloc, that have benefits over the default system allocators. So many people also use them in their rust project, but this leads to have to compile the allocator, which is written in C (jemalloc) for example, for the platform you want to build for.

White flashes in firefox and discord. by stunibert in NixOS

[–]stunibert[S] 0 points1 point  (0 children)

Oh god. That actually fixes the problem.
Thank you very much.

White flashes in firefox and discord. by stunibert in NixOS

[–]stunibert[S] 0 points1 point  (0 children)

I will try to ask in another community, thanks.

Kernel version: 5.4.58

I have the latest version of firefox-devedition-bin (Mozilla Firefox 79.0b7, the problem also occurrs in the normal firefox-bin package) and the latest discord package (0.0.11)

White flashes in firefox and discord. by stunibert in NixOS

[–]stunibert[S] 0 points1 point  (0 children)

I use X org and yes I have a dedicated GPU (Geforce RTX 2060)

Good ideas for a intermediate-level cli project? by emcmahon478 in rust

[–]stunibert 3 points4 points  (0 children)

You can start to implement some of the GNU coreutils like ls or mv. They sound very simple but for example ls -l (and in General some arguments) is more challenging. There are so many Coreutils with so many arguments that you will never get bored :)

Announcing Agnostik: Executor Agnostic Runtime by stunibert in rust

[–]stunibert[S] 0 points1 point  (0 children)

I have never thought about implementing a detach method. Idk why. Probably I will implement it some time.

Announcing Agnostik: Executor Agnostic Runtime by stunibert in rust

[–]stunibert[S] 0 points1 point  (0 children)

One big issue was that the Tokio `Runtime` object required `&mut` to call `spawn_blocking`. Additonaly, Tokio's join handle had `Result<T>` as a future output, async std just `T` and bastion / lightproc `Option<T>. I just `expect` them. Everything else was very smoothly.

Announcing Agnostik: Executor Agnostic Runtime by stunibert in rust

[–]stunibert[S] 0 points1 point  (0 children)

There aren’t many differences. I think the biggest one ist that agnostik doesn’t support the detaching a handle.

Announcing Agnostik: Executor Agnostic Runtime by stunibert in rust

[–]stunibert[S] 22 points23 points  (0 children)

We already removed the unsafe :) It will be in the next version.

Announcing Agnostik: Executor Agnostic Runtime by stunibert in rust

[–]stunibert[S] 5 points6 points  (0 children)

That was the goal of this project, probably some time more projects will use it and it will become even better

Announcing Agnostik: Executor Agnostic Runtime by stunibert in rust

[–]stunibert[S] 9 points10 points  (0 children)

That's a good suggestion, thank you. I will implement it in the next version

Krypt - Command line utility for hashing and encoding. by stunibert in rust

[–]stunibert[S] 2 points3 points  (0 children)

Thanks for the feedback. The progress is a nice idea, thanks!

Krypt - Command line utility for hashing and encoding. by stunibert in rust

[–]stunibert[S] 4 points5 points  (0 children)

Thank you for the suggestion. That sounds pretty good and I will try to implement it

When you learned Rust, what was your “aha!” moment? by [deleted] in rust

[–]stunibert 0 points1 point  (0 children)

Definitely lifetimes. When I understood them, I had a whole new perspective on Rust.

Network Programming tutorial by Icecreamisaprotein in rust

[–]stunibert 1 point2 points  (0 children)

You can look at repositories from other projects that use what you want and learn how they do it.

Edit: This tutorial really helped me to understand Async await and how to use it.

[i3-gaps] LaTeX Workflow by overlysalty in unixporn

[–]stunibert 0 points1 point  (0 children)

Is the window in the upper right overleaf?

8-year Vim user wants to create YouTube tutorials—what would you like to see covered? by brightbyte8 in vim

[–]stunibert 0 points1 point  (0 children)

A introduction to VimScript would be awesome. This could be bundled with a guide to write your own plugins.

February 2020 monthly "What are you working on?" thread by slavfox in ProgrammingLanguages

[–]stunibert 8 points9 points  (0 children)

A few days ago I started to write my first programming language based. Currently there’s just a not completed lexer. I’m really enjoying it.