Homebrew Using C++ std::vectors/std::library on the Nintendo DS.

Project68K

Well-Known Member
OP
Newcomer
Joined
Nov 12, 2020
Messages
70
Trophies
0
Age
24
XP
676
Country
United States
I wanted to ask if using C++ vectors or the std:library in general for a homebrew Nintendo DS software would be wasteful for the system, considering it's low memory and storage. I read a comment a while back saying that it would be the case. I ask this as my game involves objects falling from the sky, and deleting itself in collision with the player, or the bottom of the screen. Obviously, you could see why a vector would be desired in this context.

Any thoughts?
 

averne

Well-Known Member
Member
Joined
Jan 21, 2020
Messages
193
Trophies
0
XP
1,998
Country
France
std::vector isn't particularly wasteful it itself, it's just a couple (3) pointers that represent a heap-allocated memory block.
What *is* wasteful imo is the c++ runtime stuff, especially since std:: stuff has a tendency to inline easily since it's mostly header code. So yes in this scenario I would probably try to avoid them and manage it myself with malloc + a size. Try to avoid frequent reallocations, too.
 

mrparrot2

Well-Known Member
Member
Joined
Nov 29, 2021
Messages
110
Trophies
0
Age
29
Location
SP, Brazil
XP
585
Country
Brazil
I'd expect some bump in size, but I don't know much. But you can craft a prototype in order to see how bad is the libstdc++'s std::vector<> memory overhead.

1. Create some code which uses std::vector.
2. Compile and link everything into an .elf file.
3. Use `readelf -sW <.elf file>` to show how much static memory it is using (the `Size` column).
4. Create the final ROM which shall run some code like this to see how much free memory you have once everything you need is allocated: https://github.com/giulianobelinass...fe2f99aa28e64a4f2fbe512/common/alloc.cpp#L306

Note that 3. can be skipped if you are not worried about the static memory usage, but regardless if you are running in an NDS or NDSi is is bound to a 4Mb limit. Dynamic memory usage limit on the NDSi is much larger than the NDS (16Mb vs. 4Mb).

Also note that C++ templates usually generates code for every class in the <> parameter.
 

catlover007

Developer
Developer
Joined
Oct 23, 2015
Messages
722
Trophies
1
XP
3,975
Country
Germany
It depends on how much and how you use them. If you go crazy with those heap allocating data structures e.g. you cause lots of allocations/deallocations in tight loops that can become a bottleneck (also compilers can do less optimisations in code with lots of heap allocations).

Then there's also memory usage which is also a complex question. Just in general as long as you don't allocate giant buffers (which can happen quickly if you use arrays of arrays) I wouldn't worry about this when making smaller homebrew as DS's 4 MB of RAM is not that little.

Once you come into larger applications, it becomes more complicated, though I guess in general they're ok to use on DS even then.
What *is* wasteful imo is the c++ runtime stuff, especially since std:: stuff has a tendency to inline easily since it's mostly header code.
Calling functions is not free in code size either and if code size is really important you can still tweak the compiler flags. Though I like to compile with LTO anyways where things like this become irrelevant.

std::vector isn't particularly wasteful it itself, it's just a couple (3) pointers that represent a heap-allocated memory block.

There's an additional memory overhead though, coming from the fact that to not have to reallocate the buffer after each insertion std::vector allocates a buffer bigger than necessary.

What I wouldn't use on DS from the C++ runtime library though are runtime type information (RTTI) because you can live very well without them and they can cause binary bloat. Same kind of goes for exceptions. They're a bit more useful, though you can probably also live without them. std::function also does lots of heap allocations and calling through it is also slow.
 

Pk11

A catgirl with a DSi
Member
Joined
Jun 26, 2019
Messages
1,285
Trophies
1
Age
22
Location
米国
Website
xn--rck9c.xn--tckwe
XP
3,910
Country
United States
I use std::vector and std::string and such all the time in my code and have never found them to be notably wasteful, I'm sure there's a little bit of waste but for the convenience it's not bad.

Some other C++ features like iostreams/fstreams on the otherhand I avoid at all costs, including either of those straight up adds 300 KB to the binary, definitely not worthwhile compared to normal FILE * imo.

What I do with C++ things usually is just give them a try, if it adds a ton of bloat to the binary I do something else, if it seems fine then I use it, it usually adds all the bloat up front as soon as you do just a simple test like making an object and doing nothing with it.
 
  • Like
Reactions: NightScript

Project68K

Well-Known Member
OP
Newcomer
Joined
Nov 12, 2020
Messages
70
Trophies
0
Age
24
XP
676
Country
United States
std::vector isn't particularly wasteful it itself, it's just a couple (3) pointers that represent a heap-allocated memory block.
What *is* wasteful imo is the c++ runtime stuff, especially since std:: stuff has a tendency to inline easily since it's mostly header code. So yes in this scenario I would probably try to avoid them and manage it myself with malloc + a size. Try to avoid frequent reallocations, too.

Would a doubly linked list be just as wasteful? I've used it before, and it seems like another viable alternative.
 

averne

Well-Known Member
Member
Joined
Jan 21, 2020
Messages
193
Trophies
0
XP
1,998
Country
France
Would a doubly linked list be just as wasteful? I've used it before, and it seems like another viable alternative.
Depends on the usecase, generally I would say more wasteful in both memory (each block is a separate allocation afaik), but also memory access: since the blocks aren't layed out sequentially, the cpu has to jump around a lot which causes cache misses.
Personally I would only use lists when you need to frequently insert/delete nodes at random locations.
 

NotImpLife

Active Member
Newcomer
Joined
Mar 9, 2021
Messages
42
Trophies
0
Website
github.com
XP
489
Country
Romania
I like to totally avoid use of std on the DS (no real reason though, just to make sure the memory isn't bloated with unused stuff). You can easily implement the vector class for yourself (a wrapper class around a pointer) and write in any methods you want. Example here (no iterators): https://pastebin.com/AdFAbUc1 .
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    K3Nv2 @ K3Nv2: That's something I'd expect from ancientboi +1