API Design - Two Tips: pimpl idiom and ‘struct Options’ | Modern Cpp Series Ep. 90

  Рет қаралды 2,875

Mike Shah

Mike Shah

Күн бұрын

Пікірлер: 21
@marcomorais9472
@marcomorais9472 6 ай бұрын
Thanks for the great videos! Totally agree on the design benefits of Options structs. I see this a lot in Golang. One additional consideration that you did not mention is whether to pass the struct by value or by reference. If the struct is large (say > 16 bytes) then you most likely want to pass by reference to avoid the memory copy penalty.
@MikeShah
@MikeShah 6 ай бұрын
Cheers, thanks for mentioning that! Indeed anything larger than the pointer size (8 bytes, so usually 16-bytes aligned) is worth by reference.
@polyontomorphic
@polyontomorphic Жыл бұрын
Very useful to display that int main() { return 0; } Really cleared things up.
@MikeShah
@MikeShah Жыл бұрын
😅
@NickEnchev
@NickEnchev 7 ай бұрын
One thing of note about pImpl, especially in the given context of a game (or engine), is the fact that you guarantee indirection when accessing the implementation. In the case of a game, you'd be accessing many of these items per frame, potentially leading to very costly cache misses. I only bring this up since you example included a game character.
@MikeShah
@MikeShah 7 ай бұрын
Agreed -- that's a very good point! It'd be worth measuring to see how bad a penalty -- especially if that pointer is changing frequently, or the underlying data, then cache hits could really start to add up. So probably a reasonable pattern to use during development when ABI is unstable, but likely worth removing in a 1.0 release as things stabalize for performance.
@NickEnchev
@NickEnchev 7 ай бұрын
@@MikeShah It actually makes a pretty big difference. If you look at the way Unity shifted to DOTS for example, away from a traditional OO approach, they moved to a cache friendly data oriented approach. The performance difference is actually quite massive as you process hundreds or thousands of game entities in your game loop, the data stays hot in your cpu cache, ready to be processed.
@MikeShah
@MikeShah 7 ай бұрын
@@NickEnchev The difference between where you're getting cache hits (i.e. which level, L1, L2, L3, or DRAM) indeed is an order of magnitude at each level. Indeed optimal design is likely something data-oriented where you have indices into flat, fixed-size arrays for your entities.
@ricardonunosr
@ricardonunosr 2 жыл бұрын
I know you said it would be a nice exercise to try and find this API design in real code. But if someone cant find it it would be nice to show 1 or 2 quick examples of real code on the video. Loving the short and useful videos keep it up
@MikeShah
@MikeShah 2 жыл бұрын
Cheers, you are right it would have been useful to show in some open source projects. Will keep that in mind for future :) Video later on on the importance of reading code is coming!
@damondouglas
@damondouglas Жыл бұрын
In the context of templated classes, do you still use pimpl? Thank you for sharing these tips
@MikeShah
@MikeShah Жыл бұрын
That's getting into interesting territory. 🙂 You could probably specialize a template or provide a template parameter to the implementation, but in all honesty I have almost always had concrete implementations. Do you have a use case you're thinking about using this?
@damondouglas
@damondouglas Жыл бұрын
Thank you for responding. 🙂 I haven't a concrete use case. Yet I find a lot of what I code gravitates towards generics.
@MikeShah
@MikeShah Жыл бұрын
@@damondouglas very cool 🙂
@joebosah2727
@joebosah2727 2 жыл бұрын
Very useful. Thank you very much. This video helped me fully comprehend Chapter 3.1 of Martin Reddy API for C++ Your course is very comprehensive for smallies like moi converting from Old C++ to Modern C++. Keep it up.
@MikeShah
@MikeShah 2 жыл бұрын
Cheers Joe!
@joebosah2727
@joebosah2727 2 жыл бұрын
@@MikeShah I love C++. It is no magic
@ghulam2545
@ghulam2545 2 жыл бұрын
One thing I would like to guess, having hands on experience with OOP could be considered as one of the prerequisite.
@MikeShah
@MikeShah 2 жыл бұрын
Agreed, this lesson is a bit forward looking. Probably not the way for everyone to write code, but for maintainers of libraries or code that a lot of folks will be dependent on for a long time. These two tips should be useful in that context. :)
@jaykraft9523
@jaykraft9523 Жыл бұрын
hint: do these typing in an editor rather than writing, you lost me at the start
@MikeShah
@MikeShah Жыл бұрын
👍 You're probably right -- I may do a follow-up in the future, as these are two API tips I like to use 🙂
黑天使被操控了#short #angel #clown
00:40
Super Beauty team
Рет қаралды 61 МЛН
Что-что Мурсдей говорит? 💭 #симбочка #симба #мурсдей
00:19
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 31 МЛН
Арыстанның айқасы, Тәуіржанның шайқасы!
25:51
QosLike / ҚосЛайк / Косылайық
Рет қаралды 700 М.
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 913 М.
Return Value Optimization and Copy Elision in C++
9:54
mCoding
Рет қаралды 40 М.
This is the Only Right Way to Write React clean-code - SOLID
18:23
Arenas, strings and Scuffed Templates in C
12:28
VoxelRifts
Рет қаралды 103 М.
Let's Create a Compiler (Pt.1)
1:11:03
Pixeled
Рет қаралды 603 М.
C++ Weekly - Ep 442 - Stop Using .h For C++ Header Files!
6:06
C++ Weekly With Jason Turner
Рет қаралды 31 М.
C++ Lambda Idioms - Timur Doumler - CppCon 2022
1:04:45
CppCon
Рет қаралды 53 М.
黑天使被操控了#short #angel #clown
00:40
Super Beauty team
Рет қаралды 61 МЛН