Nim icon indicating copy to clipboard operation
Nim copied to clipboard

Document Nim data structure layout

Open dom96 opened this issue 9 years ago • 6 comments

It would be really nice if somebody could write some documentation explaining the layout of Seq[T] and other data structures in Nim.

dom96 avatar May 16 '15 17:05 dom96

Memory layout?

reactormonk avatar May 16 '15 17:05 reactormonk

Yep.

dom96 avatar May 16 '15 17:05 dom96

@dom96 can you be a bit more specific what exactly would be a task here? (a short example would be great)

narimiran avatar Oct 04 '18 16:10 narimiran

The task is to document what seq and other common types actually look like in memory:

A sequence type is really just an object containing two fields: the length and a pointer to the first element.

type
 Seq[T] = ref object
   len: int
   data: pointer

I've made that up, I can't remember exactly what the structure of a seq looks like and I'm too lazy to look it up. But hopefully you get the idea :)

dom96 avatar Oct 05 '18 03:10 dom96

http://zevv.nl/nim-memory/#_how_nim_stores_data_in_memory was my first intro, and is great, but needs updating for seq and string v2

maybe also:

  • UncheckedArray
  • openArray
  • some discussion of the RTTI pointer

shirleyquirk avatar Oct 01 '20 09:10 shirleyquirk

I am really looking forward to it. The documentation @shirleyquirk has written is a great beginning, but I would like to understand more the internals.

Maybe we can add urls to specific parts of the std lib mentioned by the doc. I am thinking of the seq implementation :

This data is normally hidden from the user, but you can simply find the implementation of this header in the Nim system library, and it looks like this:

type TGenericSeq = object
  len: int  
  reserved: int 

I have been surprised to learn with similar examples as those given in this documentation, that adding to an object the mention of RootObj requires an extra slot in memory (which is important to know for embedded development I guess ?).

dlesnoff avatar Sep 11 '22 20:09 dlesnoff

There is http://zevv.nl/nim-memory/#_how_nim_stores_data_in_memory and feel free to write an article about it.

Araq avatar Aug 27 '23 19:08 Araq