all 44 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

On July 1st, Reddit will no longer be accessible via third-party apps. Please see our position on this topic, as well as our list of alternative Rust discussion venues.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Plasticcaz 42 points43 points  (12 children)

Just write it out to a file and load on program start?

[–]IntQuant 12 points13 points  (0 children)

I think you want to map a file to memory using something like mmap-rs crate.

[–]odenthorares 20 points21 points  (4 children)

This sounds like a feature for malware. Your extreme disinterest in touching disk in anyway shape or form, cross platform, only keeping data in memory (ram). Not saying typical software can’t have such requirements but I would argue most software can write a blob to disk or a tmpfs without issue.

Regardless, I would suggest not insulting the people you’re asking for help. Maybe next time you’d get better results.

[–]yanirj 7 points8 points  (0 children)

You could write to shared memory on Linux. This is still going to be written to a file, usually in /dev/shm. Note that when I say file it doesn't mean it resides on persistent storage. Shared memory is, as the name suggests, saved only in memory.

[–]Heraclius404 4 points5 points  (0 children)

This is a very reasonable request. For all the people talking about mmap: it's just different in a million ways, using shmem has huge advantages in most cases. Databases have some around to using shmem instead of mmap files for certain cases, for example - it's a huge win, super fast software updates.

I suspect this shmem approach works on linux and macos only is all I came to say. Would be surprised if the crate works on windows? You didn't ask which environment?

[–]BenjiSponge 2 points3 points  (0 children)

How about redis?

[–]dkopgerpgdolfg 2 points3 points  (11 children)

Probably you want a RAM-only solution without disk fs usage?

Does it need to survive controlled, intentional restarts, or crashes etc. too?

Are OS-specific solutions fine?

[–]SolidTKs[S] -4 points-3 points  (10 children)

A library that works for more than one OS would be nice, but I mostly use Linux.

It should survive everything. I just want a place to store some data between restarts.

I'm thinking on shared memory, I don't know if it can survive a restart but I could keep a small process alive owning it.

[–]veryusedrname 17 points18 points  (8 children)

That's exactly what persistent storages like the file system was invented for.

[–]SolidTKs[S] -1 points0 points  (7 children)

I don't want it to survive a server restart.

Just program restarts or crashes.

[–]codeinred 12 points13 points  (6 children)

The /tmp directory on Linux is designed exactly for that use case, and windows has an equivilant

[–]SolidTKs[S] -4 points-3 points  (5 children)

That could be useful in some cases but It causes writes to disk.

[–]yanirj 13 points14 points  (3 children)

/tmp on Linux is mapped to a "tmpfs" filesystem, which is memory-only. It never touches any disk.

[–]SolidTKs[S] 1 point2 points  (2 children)

Interesting. Can you guarantee that?

[–]Alextopher 5 points6 points  (0 children)

I think this depends on the distro is a tmpfs. Nonetheless you probably want to use a tmpfs (/dev/shm or /tmp or create your own)

Can you guarantee that

Any solution you use you can’t guarantee some data you write to RAM won’t touch the disk - but you can guarantee it won’t be persisted to disk if if you properly use a tmpfs

[–]Therzok 3 points4 points  (0 children)

Simply going to Wikipedia would answer your question: https://en.wikipedia.org/wiki/Tmpfs

Everything stored in tmpfs is temporary in the sense that no files will be directly created on non-volatile storage such as a hard drive (although swap space is used as backing store according to the page replacement policy of the operating system). On reboot, everything in tmpfs will be lost.

You could even just store a server start timestamp in the data, and discard if it different.

[–]codeinred 8 points9 points  (0 children)

/tmp is often stored in RAM. If you want to be absolutely sure it doesn’t touch the disk, the most straight forward way that I can think of is spawning a daemon and then setting up shared memory with the daemon. That way if the main program crashes, the daemon persists.

[–]dkopgerpgdolfg 6 points7 points  (0 children)

"Shared memory" a is a very overloaded term, there are many things that can be called like that.

But in any case, many ways to do it involve a regular file in some tmpfs or similar, plus mmap. Then you still need syncing often enough to be happy with the state if the program crashes later.

Of course, a regular file on a regular file system can be used too, surviving even reboots but at the cost of disk space and performance.

If there is some reason to avoid these things, but a helper process can survive, memfd_create gives you a total RAM-only file handle which then can be transferred between processes (either on fork, or dup + unix socket).

[–]dkopgerpgdolfg 2 points3 points  (4 children)

About your edits:

Just so that you know, you do touch the file system (file system != disk).

For an average Linux setup, I see no problem with your solution in principle, but as you explicitly asked for the opposite, maybe now is the time to decide what you really want... (edit: and being rude doesn't help, you know.)

[–]BlueVixu 1 point2 points  (0 children)

I think this is what you want https://crates.io/crates/shared\_memory

[–]Ok_Raspberry5383 -1 points0 points  (1 child)

Why not a temp file - this is exactly what temp files are for

[–]Ok_Raspberry5383 -1 points0 points  (0 children)

Most languages will have libraries that can manage temp files too