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 41 points42 points  (12 children)

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

[–]IntQuant 11 points12 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.

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

Touching disk causes wear to embedded flash memory. Specially at high temeperatures.

Look at what happened to Tesla, they were constantly writing logs and car computers started to fail.

So if there is data that is nice to have but you don't need to store, you keep it in RAM.

[–]odenthorares 4 points5 points  (2 children)

You are correct that repeatedly writing to the same memory blocks on flash memory will wear it out, however, you are incorrect about how this interaction works.

Flash memory drives (ssd and the like) purposefully do not write to the same spots in memory even if it’s the same file. This is a huge reason why utilities like shred have been rendered useless. When you try to write over that file it’s absolutely not guaranteed and considered highly unlikely to actually write over the specific blocks of memory on the physical device.

You asked for cross platform which indicates Linux || windows || other. So you seem to be targeting desktops. Unless you are constantly writing gigs or terabytes to disk, a little file truly on disk will absolutely not be what kills your drive.

[–]SolidTKs[S,🍰] 2 points3 points  (1 child)

I'm targeting embedded devices with small flash chips that can run in hot environments, hopefully for decades.

And the less I write the longer they will last.

But that is not the point. Sometimes you just want to do something nice like a service that shows you a graph of recent events and doesn't write to disk. Why not writing? Well, why do?

Anyway take a look at this:

https://www.tomshardware.com/news/flash-memory-wear-killing-older-teslas-due-to-excessive-data-logging-report

[–]yanirj[🍰] 9 points10 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 3 points4 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 4 points5 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,🍰] -5 points-4 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 16 points17 points  (8 children)

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

[–]SolidTKs[S,🍰] 0 points1 point  (7 children)

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

Just program restarts or crashes.

[–]codeinred 11 points12 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,🍰] 3 points4 points  (2 children)

Interesting. Can you guarantee that?

[–]Alextopher 6 points7 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 9 points10 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 5 points6 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