CppCon 2016: Jason Turner “Rich Code for Tiny Computers: A Simple Commodore 64 Game in C++17”

  Рет қаралды 250,999

CppCon

CppCon

Күн бұрын

CppCon.org
-
Presentation Slides, PDFs, Source Code and other presenter materials are available at: github.com/cpp...
-
The Commodore 64 was released in 1982 and is the best selling computer model of all time. At 34 years old, even the most simple embedded processor today outperforms it. Join me on an exploration of how C++17 techniques can be utilized to write expressive, high performance, high level code for simple computers. Together we will create a game for this aging system.
You'll leave the talk with a better understanding of what your compiler is capable of and be able to apply these ideas to create better code on modern systems.
-
Host of C++Weekly / jasonturner-lefticus , Co-host of CppCast cppcast.com, Co-creator and maintainer of the embedded scripting language for C++, ChaiScript chaiscript.com, and author and curator of the forkable coding standards document cppbestpractice.... I'm available for contracting and onsite training.
-
Videos Filmed & Edited by Bash Films: www.BashFilms.com
*-----*
Register Now For CppCon 2022: cppcon.org/reg...
*-----*

Пікірлер: 243
@MartijnvanBuul
@MartijnvanBuul 4 жыл бұрын
I know this video is 3 years old, but I keep referring people to this video because it's doing such a great job at dispelling some misconceptions. Unfortunately, there are no closed captions available for this video, which prevents me from showing this video to a deaf co-worker. I know generating *proper* captions would be a mammoth undertaking, but I would appreciate it if the automatic captions for this video would be enabled. Thank you in advance.
@michelealessandrini3421
@michelealessandrini3421 2 жыл бұрын
Yes, subtitles are essential for all the non English-native people, too, I cannot understand everything by just listening.
@anon_y_mousse
@anon_y_mousse 2 жыл бұрын
What misconceptions do people have that it is dispelling? That modern languages can't run on older hardware? Or is there something else you're referring to?
@TymexComputing
@TymexComputing 2 жыл бұрын
i have just directed 32 people to this video :)
@AnthonyDentinger
@AnthonyDentinger 2 жыл бұрын
About the captions, I've noticed CppCon generally tends to have humans writing them (which you can tell because whole chunks of sentences appear at once, rather than word-by-word). But nowadays KZbin does have automatic CC generation that is actually fairly good! So what I think happened is the CppCon channel admins enabled human-written captions on this video, which removed automatic CC, but never actually wrote the captions.
@Asdayasman
@Asdayasman 8 ай бұрын
Why don't YOU write the captions? Do you not care about your coworker?
@TheSulross
@TheSulross 4 жыл бұрын
To me this is THE best C++ presentation in the post C++11 era. Have watched it twice. Entertaining, interesting (inspired use of retro-computing as a hook), and informative/educational in respect to C++ the language
@MaherBaba
@MaherBaba 8 жыл бұрын
Is truly amazing to see the optimizer in action, maybe we can port Unreal to C64 with some constexpr
@DamianReloaded
@DamianReloaded 8 жыл бұрын
_Arghh the sarcasm's too bright!! I cannot see the point!! Where is it!!_
@PicturesqueGames
@PicturesqueGames 7 жыл бұрын
Actually, i think with this level of optimizations modern compilers bring to the table - Wolf3d "might" be possible. granted you need to set it to lowest res or even stretch lines twice.
@PflanzenChirurg
@PflanzenChirurg 6 жыл бұрын
Aw u propably mean Line Interlacing
@jomunoz
@jomunoz 5 жыл бұрын
it is possible, but it will run at 1 frame per year.
@luminousmonkey4512
@luminousmonkey4512 5 жыл бұрын
@@jomunoz kzbin.info/www/bejne/hpKclHZsfciaqKc Granted SuperCPU, but still on a C64. :)
@YuTe3712
@YuTe3712 8 жыл бұрын
"Can we do this with some overhead or no overhead?" The answers to that question continued to surprise me over and over again! It's one thing to be told to trust the compiler to optimize for you, it's another to see it in action! Thank you very much Jason for this enlightening and encouraging talk!
@marcusterrel1686
@marcusterrel1686 3 жыл бұрын
I did not understand what he meant by overhead in this context. Can you explain?
@mathieup.8277
@mathieup.8277 2 жыл бұрын
@@marcusterrel1686 When you call a method, create an object or even call a function normally without optimizations you will have a lot of overhead (meaning added instruction in the assembly code). But the compiler is able to understand that you don't really need methods or objects in some case and remove them for you. The more known example to remove overhead is the inline keyword.
@depthone82
@depthone82 4 жыл бұрын
Who else came to watch after hearing Bjarne reference this talk on Lex Fridman's AI podcast?
@bernoulli9047
@bernoulli9047 4 жыл бұрын
Depth One DJ yep. Gonna listen to Lex's interview with the creator of clang next.
@thorstensbirk339
@thorstensbirk339 4 жыл бұрын
Me too. I haven't used C/C++ for 20 years. This makes me want to start using C++. Lex's interview with Bjarne Stroustrup (kzbin.info/www/bejne/q4Xbg3lrlsxlbaM) is very interesting and inspiring.
@evugar
@evugar 4 жыл бұрын
Here is the reference: kzbin.info/www/bejne/q4Xbg3lrlsxlbaM
@BosakMaw
@BosakMaw 4 жыл бұрын
I was actually here before, but then I lost this talk and I found it again trough Lex's podcast
@minhtao9774
@minhtao9774 4 жыл бұрын
Bwuahahaha!!! ME~!
@TheSulross
@TheSulross 8 жыл бұрын
A lot of languages can seem to drift toward use in producing ever more bloated software as the language sophistication and their communities mature and evolve over time. And yet C++ is getting better abstractions for making the chore of programming easier while seemingly doing a better and better job of generating efficient software. This talk was an eye opener in that respect. (And Rob Irving's and Jason Turner's C++ podcast surely deserves a plug - kudos!)
@TheMrKeksLp
@TheMrKeksLp 3 жыл бұрын
Honestly, I'm not so sure about that. Don't forget that Jason is literally earning a living by knowing C++ inside out, so of course he knows all the cool tricks. But that's like watching Tony Hawk and deducing that skating is easy because you only need a few quick feet movements. Actually learning the language is getting harder and harder since all the cool new features (which are genuinely improvements!) are nonetheless based on the old systems. So now you need a good understanding of both and so on
@anon_y_mousse
@anon_y_mousse 2 жыл бұрын
@@TheMrKeksLp I think by easier, he meant for those who do know it inside out because they can use the language more thoroughly and not take extra time to write workarounds for things that should be built-in because they are built-in.
@AG-ld6rv
@AG-ld6rv Жыл бұрын
The optimizer isn't going to do as good a job when things get complex enough. If it's written well, it should still be faster than many languages, but don't expect to have more lines of C++ code than of assembly like happened here in several versions of the code that often.
@chillyvanilly6352
@chillyvanilly6352 4 жыл бұрын
28:00 that was SUCH a badass moment haha Just * mic-drop * :D Or actually, more like "Cool guys don't look at explosions"-moment ^^
@emilwallin1176
@emilwallin1176 2 жыл бұрын
haha indeed, I wanted to show this particular moment to a friend of mine and I found it thanks to your comment! thanks
@rahulsrma26
@rahulsrma26 8 жыл бұрын
Amazing talk! You can show this presentation to C programmers and convert them to C++. It's hard to convince them without showing 0 overhead. :)
@luispons461
@luispons461 8 жыл бұрын
C64 modern programmers are so much better than this (in terms of effciency) it hurts. Check any recent demo from Oxyron, for an example. This video of c++ on c64 is entertaining, but says nothing about optimization. HL programming languages on c64 are nothing new, there existed several alternatives in the 80s, and these were not used for commercial projects (with some exceptions). You could do little things like this Pong game, but not really compete with the asm games of the time.
@darranrowe174
@darranrowe174 8 жыл бұрын
There is a couple of things to note about this. First, the point of this wasn't to write an extremely efficient c64 game, it was basically to show that a lot of the "C++ is bloated and can't write for small devices" is really not true. Secondly, about the efficiency, all I can say is isn't that obvious? The binary output is from an x86 compiler and then translated to 6502. These are vastly different platforms so the compiler would optimise in different ways. If the compiler he used was specifically targeting the 6502, then it would be a lot more efficient, but as he stated right at the start, adding a target to a compiler toolchain is hard.
@rahulsrma26
@rahulsrma26 8 жыл бұрын
@Darran: You got it right. You can show this video to the people who says that C++ compilers are bloated or less efficient than C. Theoretically, there can't be a performance difference for the same logic in both languages. I guess we are reaching there in practice now. @Luis: I'd seen that demo. Astonishing! As Darran said generating efficient c64 code is not the point of the presentation.
@FunkyELF
@FunkyELF 8 жыл бұрын
After watching this talk to me it seems that it takes a lot of knowledge, discipline and verification of assembly get 0 overhead. Where in C it is 0 overhead by default, any overhead is explicit.
@rahulsrma26
@rahulsrma26 8 жыл бұрын
That is the problem with modern c++, too complex. But if you want top-notch abstractions, high level language features and manageable code without sacrificing performance then you don't have a choice.
@R_BNK
@R_BNK 4 жыл бұрын
Excellent presentation, It was like seeing magic how compiler can optimize code .
@SqueakyNeb
@SqueakyNeb 8 жыл бұрын
Absolute madness. I'm amazed every time I dig into the low-level aspects of C++. Some of the question askers were twits though. "Hey did you try this? Do you know about this? I know about this! I KNOW THINGS! I'M THE BEST!"
@Astfresser
@Astfresser 2 жыл бұрын
Reducing compile time is a valid optimization
@KorayYILMAZ80
@KorayYILMAZ80 4 жыл бұрын
I just started to learn C++ and the video was amazing. Thanks.
@perguto
@perguto 3 жыл бұрын
22:00 Every copy of Commodore 64 is personalized
@paulmoore7964
@paulmoore7964 4 жыл бұрын
the actual correct use of volatile !!
@Yupppi
@Yupppi 10 ай бұрын
Funnily enough, now they're selling modern very flat keyboards with Raspberry Pi inside, so in a sense you can get a super computer version of Commodore 64. Just a lot thinner and faster. I guess this talk is a great demonstration of why Rust having immutable as default is so great. Before starting to learn Rust, I never paid that much attention to making things const in C++, only some things that were like default values, something like PI. But I started passing const references a lot more after touching Rust. It's hard to name all the things this talk demonstrates about C++ (but obviously the summary of zero cost abstraction was huge), but it's pretty great in showing all kinds of parts about programming in C++. I don't want to see smart stuff if this is the stupidest thing to do, this is what I'm up for.
@devnull1013
@devnull1013 Жыл бұрын
Did something similar in a small hobby project of defining a gameboy CPU dispatch table as a set of traits, with each opcode handler generated from a combination of inlined functions which end up operating directly on the memory representing the emulated CPU state. Compilers are smart.
@steffennilsen2132
@steffennilsen2132 5 жыл бұрын
27:15 im going to show this to anyone who complains that I overuse const
@Sydra.
@Sydra. 5 жыл бұрын
Overuse const? That should be default. I use "val" (#define val auto const) almost everywhere.
@bakedbeings
@bakedbeings 4 жыл бұрын
Answer: “Oh snap, sorry, you needed to change that at runtime?” Then, with an innocent, interested expression, let them dig themselves a hole.
@Joefish90
@Joefish90 8 жыл бұрын
Awesome talk! Would be interesting to see some of the homebrewers getting inspired by this
@curtnichols
@curtnichols 8 жыл бұрын
Crazy good. I appreciate seeing the possible optimizations and some of the new language features I need to pick up on. Good talk!
@drstrangelove09
@drstrangelove09 2 жыл бұрын
Cool!!! (And I wrote quite a bit of assembler for the 6502, back in the late 70s for the Apple IIe!!!)
@wawangsf
@wawangsf 4 жыл бұрын
Is there a mistake between minute 37-41 in checking ```while(vic.background() != 240)``` when background() returns the memory reference of background color. Instead it ought to check for ```(vic.rasterline() != 240)``` where rasterline uses the memory address of SCREEN_RASTER_LINE. This problem was corrected in minute 42 in the "sanity check" which uses the correct memory address @CppCon
@derpnerpwerp
@derpnerpwerp 4 жыл бұрын
Its been a really long time since I did any c++ and I dont know anything about the commodore 64 architecture but I was really confused that he was doing "vic.background() != 240".. isnt this supposed to be "vic.raster_line() != 240" and he needs to define a raster_line function to return a pointer to that memory address? Maybe i am missing something here, but i was sitting here trying to figure out how that could possibly work because it made no sense to me so I assumed it was my poor knowledge of c++
@jeremykothe2847
@jeremykothe2847 2 жыл бұрын
That confused me too. Some research suggests the address for the current scan line is $D012, so I imagine it was just an oversight/typo.
@MindGameArcade
@MindGameArcade 5 жыл бұрын
Extremely good presentation, near perfection
@bugslayer2838
@bugslayer2838 8 жыл бұрын
It would have been a nice inside joke if instead of the memory() function you would have used a poke(uint16_t address, uint8_t value) function ;-)
@merictunc
@merictunc 4 жыл бұрын
28:00 it's truly 👀 opening
@Mtaalas
@Mtaalas 6 жыл бұрын
Compiler is amazing, but I hope people realize that there are a lot of architecture specific (C2D, Skylake etc.) and larger scale optimizations (ragarding how the cpu:s internals work and how ram is accessed etc.) that compiler can't really do. I've seen enough code where people trust too much on the compiler and are filling the CPU-pipeline inefficiently or accessing ram in a way that's suboptimal. I loved the talk though to show how good the compiler really is as long as you have some clue as to what you're doing to make it optimizable by the compiler. :)
@yutubl
@yutubl Жыл бұрын
Super interesting and impressive demo of C++ possibilities ! @27:33 I'm happy being able answering good to the question how to reduce code footprint of Color tables generated code: I would try avoid all compile-time possible calculations using const, constexpr and perhaps reorganize data structures to take this optimzation further.
@ItsPerfexion
@ItsPerfexion 8 жыл бұрын
Does anyone have any ideas about the typical memory footprint of a "hand rolled, contemporary with the hardware" version of pong?
@keris3920
@keris3920 4 жыл бұрын
A quick search is telling me between 1.2k and 1.8k bytes for the ZIPPED ROM. Assuming compression removes 25%, that means the handwritten games use between 1.6k and 2.4k bytes. So ~1.5k here seems to be one of the more compact variants of the game.
@davidledger5941
@davidledger5941 4 жыл бұрын
Had a look around, best I could find: Pong by "Tomi Malinen" was 2 blocks, 512 bytes. Its single player and black and white. Its the smallest I could find on some random website I've never seen www.gb64.com/.
@draguin
@draguin 8 жыл бұрын
Is there a point to const pass-by-value arguments to function? IIRC I've seen the Core Guidelines advise against this, but can the compiler use this to do smart optimisations?
@davidforgeas2235
@davidforgeas2235 8 жыл бұрын
I believe it doesn't change anything for optimizations, it just changes the fact that you can not change its value. If you think about it, const or not it is going to be a local variable regardless. The case of a static array is different, because a static array could be updated at some point, and the program would have to read the new values.
@lylestavast7652
@lylestavast7652 2 жыл бұрын
still interesting 6 years later :) I populated chips on the cards for the hardware atari in 1976 while in college for a semester when I couldn't get into school on a deadlines issue....
@xlar54
@xlar54 5 жыл бұрын
cc65. A modern C compiler for 8 bit machines including the 64
@VioletGiraffe
@VioletGiraffe 5 жыл бұрын
C, not C++. Huge difference. Almost none of what's been shown by Jason in this talk is possible in C.
@davidforgeas2235
@davidforgeas2235 8 жыл бұрын
I find it quite unfortunate that it is only one file and that it doesn't look like a normal C++ project. Do you think this would work with the usual structure of different source and headers files and probably LTO ?
@VenelinEfremov
@VenelinEfremov 3 жыл бұрын
Thank you Jason. Great talk, very entertaining and informative at the same time.
@NehadHirmiz
@NehadHirmiz 7 жыл бұрын
Excellent demonstration and a great presentation. Thank you very much.
@madcommodore
@madcommodore 4 жыл бұрын
6502 compatible CPUs were the ARM CPUs of the late 70s/early 80s
@RoamingAdhocrat
@RoamingAdhocrat 4 жыл бұрын
15:00 wait - a function in an anon namespace is visible in `main`?
@RoamingAdhocrat
@RoamingAdhocrat 4 жыл бұрын
24:30 wait - template functions can return `auto`?
@RoamingAdhocrat
@RoamingAdhocrat 4 жыл бұрын
39:00 wait - you can declare structs within functions?
@RoamingAdhocrat
@RoamingAdhocrat 4 жыл бұрын
59:00 wait - you can do… you can do whatever the heck that is?
@movax20h
@movax20h 4 жыл бұрын
@@RoamingAdhocrat Some of these features were pretty. You couldn't do any of these things in C++ 20 years ago. But you can for some time now.
@luis46coco
@luis46coco 4 жыл бұрын
@@RoamingAdhocrat por supuesto, and for ((int) i=0 , i
@tofikk
@tofikk 3 жыл бұрын
I will start using const everywhere;)
@markomakela2206
@markomakela2206 8 жыл бұрын
Is there any way to avoid generating machine code (one x86 MOV instruction per byte written) for the vic.make_sprite()? That is, have the compiler generate an array of the sprite data (to be placed at the correct address by the linker) instead of generating instructions to initialize the sprite data array?
@markomakela2206
@markomakela2206 8 жыл бұрын
I tried converting VIC_II::make_sprite() to a constructor VIC_II::Sprite::Sprite() of a struct with a 21-byte member, and using placement new, but also that generated machine code for initializing the 21-byte array. Any better ideas?
@markomakela2206
@markomakela2206 8 жыл бұрын
I think I found the answer. And it is of course 63 bytes (3 bytes per line, 21 lines). The objects have to be declared const in the global scope. If they are non-const, they would still be initialized at runtime. For the required 64-byte alignment, I used a GCC extension, which clang appears to support (I am seeing .p2align 6 in the assembler output). The declaration is like this: struct Sprite { uint8_t memory[63] __attribute__((aligned(64))); template Sprite(D ... d) {...} }; and the usage is like this: const VIC_II::Sprite ball(...); Note: clang would omit const objects from the output unless they are explicitly referenced. So, for testing purposes, I added fwrite() calls for dumping the sprite data to the main() function, before the infinite loop.
@markomakela2206
@markomakela2206 8 жыл бұрын
I submitted my solution at github.com/lefticus/x86-to-6502/pull/2
@Carutsu
@Carutsu 7 жыл бұрын
I don't quite follow the part about the background, once it is outside the video area the value becomes 240? why?
@genewitch
@genewitch 4 жыл бұрын
since no one else has replied, the video chips actually "drew" outside of the viewable area on a screen (overscan), which meant that a frame actually took longer to display than it looked like, that is, if you had a slow motion camera aimed at the screen, there would be a delay from when it drew "the last line at the bottom" and when it started to draw the top line again (ignoring interlacing) - the code he wrote to do the updates only changes the position of everything on the screen when the video chip is currently in the overscan region (horizontal line 240). Every so often you'll see some video that's been copied from VHS or laserdisc or even DVD and converted without cropping, and it'll have weird garbage going on at the edges of modern monitors/tvs/screens, same reason, overscan - you can capture raster that is out of bounds with modern capture hardware, and unless you crop it in handbrake or ffmpeg or whatever, the artifacts will be there. Technology Connections (or connextras) has some info about how the overscan areas were used during the era of analog broadcast around the time that color televisions hit the scenes.
@TheProCactus
@TheProCactus 8 жыл бұрын
Anyone know how that const was working like that, Or where good specific info about it is ?
@skilz8098
@skilz8098 6 жыл бұрын
If something is const in C++ it has to be known at compile time, this involves both the "constness" of an object as well as "const storage". It can not be deduced at run time, it has to be known at compile time. By making it const it is initialized when compiled and the compiler optimizes away everything on the stack that fits into the registers.
@jacobschmidt
@jacobschmidt 4 жыл бұрын
@@skilz8098 this just isn't true, const just means that the value assigned it won't be modified afterwards.
@toby9999
@toby9999 2 жыл бұрын
@@jacobschmidt It is true sometimes depending on context.
@americancitizen748
@americancitizen748 7 жыл бұрын
Cool. This really gives me some insight into how compilers work!
@slap_my_hand
@slap_my_hand 6 жыл бұрын
This is why i love C++.
@pw1169
@pw1169 6 жыл бұрын
Haha, this is why i don't like C++. I found the final C++ version almost unreadable
@xgriff91
@xgriff91 4 жыл бұрын
I love this talk
@oliprozaphod700
@oliprozaphod700 8 жыл бұрын
I'm curious about the choice of using x86 to generate the code. I would expect it to be easier to write a translator from a source architecture like ARM or Thumb
@ZelenoJabko
@ZelenoJabko 6 жыл бұрын
probably not. CISC has more complex instructions and those are simple to break down. It is harder to rearrange incompatible RISC instructions.
@LittleDavid85
@LittleDavid85 3 жыл бұрын
What about just using the C compiler of the Commodore 64
@dr.ignacioglez.9677
@dr.ignacioglez.9677 2 жыл бұрын
I LOVE C64 👍🥂🎩
@cloerenjackson3699
@cloerenjackson3699 6 жыл бұрын
As a Commodore 64 and C++ programmer, I have only these questions: 1. Show me the C++ source file. 2. Show me the 6510 executable.
@JohnnyAdroit
@JohnnyAdroit 5 жыл бұрын
Source files: github.com/lefticus/presentations/tree/CppCon2016/RichCodeForTinyMachines/Source
@dartme18
@dartme18 7 жыл бұрын
speed = 1.5 is good for this one. Nice talk! Sorry about the technical problems at the beginning.
@nagarajahuliyapuradamata3447
@nagarajahuliyapuradamata3447 3 жыл бұрын
Nice workshop!
@avwie132
@avwie132 4 жыл бұрын
28:06 was amazing :)
@mrmiyagi5
@mrmiyagi5 8 жыл бұрын
Could this be done to do stuff with a NES using something like NESHLA and a flash cart?
@shiru8bit
@shiru8bit 8 жыл бұрын
There are ANSI C compilers for 6502 for ages, and a handful of games has been developed for the NES in C. No need in x86 to 6502 translator, and C++17 isn't something you would want on the NES anyway.
@kwanarchive
@kwanarchive 7 жыл бұрын
This video shows C++17 is something you want on a NES. Why would you want to code in C when the higher level stuff costs nothing?
@AnilMylevahanan
@AnilMylevahanan 7 жыл бұрын
Awesome talk!
@kentvandervelden
@kentvandervelden 8 жыл бұрын
Anyone else, miss how quickly the C64 booted?
@BADC0FFEE
@BADC0FFEE 8 жыл бұрын
no, because that didn't matter at all as you had to wait ages for any program to load from tape
@kentvandervelden
@kentvandervelden 8 жыл бұрын
Oh, right, or type in programs from magazines :)
@thewatcher_476
@thewatcher_476 8 жыл бұрын
no because my PC starts in like 4s and loads programs in under 1s
@TheSulross
@TheSulross 8 жыл бұрын
Oh, but when I first go my C64 I had to load and store software to a cassette tape - loading software from that was not a very quick process. (Later got a floppy drive but it was not particularly very fast either, but better than tape.) Cartridges were for games and game companies. Yeah, you could turn it on and get a near instant prompt to start twiddling in BASIC, but so what?
@kentvandervelden
@kentvandervelden 8 жыл бұрын
Oh good point, I had forgotten about that. You reminded me of the pains of the olden days.
@zhaoli2984
@zhaoli2984 6 жыл бұрын
Exceptions in C++ seems a game breaker regarding the zero-overhead thing. You have to be very careful not to use any thing that might throw.
@zhaoli2984
@zhaoli2984 6 жыл бұрын
Quite impressive talk, BTW
@AG-ld6rv
@AG-ld6rv Жыл бұрын
Stroustrup has said that he believes people overemphasize the exception situation as bad. Yes, exception-safe code has some extra instructions in the binary (quite a lot actually), but unless your program is constantly throwing exceptions, expect those code paths pretty much never to be used. You only have to obsess over removing exceptions if the size of your binary matters. The result should run plenty fast, just a tad slower.
@linonator
@linonator 2 жыл бұрын
Can anyone explain to some degree what happened when he used the const at the beginning of that function? I can see the assembly code decreased dramatically but not sure what const did to do that.
@MrShebaw
@MrShebaw 2 жыл бұрын
The std::array that we used initially was mutable so the compiler was generating code assuming that the std::array is mutable. It built the whole array at runtime by generating the Color structure and storing it into an array. Furthermore, when we indexed it to call the `nearest_value` function on the array member, the in-lined code was generated for it by indexing into the runtime built array. So code was being generated to construct, store and index into the array. But we are using it as a read-only structure so we can let the compiler optimize it further by marking the array as read only. When we changed it to a `const` array, the compiler was given a hint that the array is a read-only array instead. The compiler can use this info to build the array but it wouldn't have been different from the code generated for the mutable structure at that stage. But the compiler didn't stop there. It saw that we only indexed a single value from the array from our now read only array. So it went a step further and didn't even bother to build the array. It instead took the value directly and calculated the result of `nearest_value` at compile time and saved that instead. This step removed the code for generating our array, and also indexing into it to calculate the `nearest_value` value. It can do this for a read-only array since it can know that the values of the array won't change. It can just index it at compile time and use the value directly instead. But if we just used the mutable array, it can never be sure that a subsequent call won''t change the values of the array so it's instead forced to generate code to build the initial array, index the array and get the values at runtime which ends up generating a lot more code. tldr; it didn't even bother to build the read-only array and compiled the resulting value of the function directly instead. That's why the code ended up being small.
@christophe3d
@christophe3d 2 жыл бұрын
Quite amazing.
@victotronics
@victotronics 2 жыл бұрын
32:00 "while true if-initializer" That should of course be "while initializer". Why doesn't that exist?
@Evan490BC
@Evan490BC 2 жыл бұрын
You can use an equivalent "for"-with-initialiser expression.
@davidjulitz7446
@davidjulitz7446 5 жыл бұрын
Well done, this was eye Opening.
@JunaidHasan23
@JunaidHasan23 3 жыл бұрын
LMAO 27:58 magic!
@raymondhill7837
@raymondhill7837 3 жыл бұрын
Rust got it right, everything is immutable by default.
@verbnoun1531
@verbnoun1531 8 жыл бұрын
Source code: github.com/lefticus/presentations/tree/CppCon2016/RichCodeForTinyMachines
@WavemStudios
@WavemStudios 5 жыл бұрын
Great work Jason
@Silmarieni1
@Silmarieni1 8 жыл бұрын
Awesome presentation !
@Silmarieni1
@Silmarieni1 8 жыл бұрын
Pong is ultra simplistic though, I wonder how well we can get 0 overhead with a more contemporary app
@alexmalenowski1388
@alexmalenowski1388 8 ай бұрын
Jason 💪💪💪
@mlfconv
@mlfconv 5 жыл бұрын
How did you manage to translate the x86 asm code to 6502
@luis46coco
@luis46coco 4 жыл бұрын
cross compiler
@jcamargo2005
@jcamargo2005 2 жыл бұрын
A cross-compiler translates all x86 instructions to (usually) one or more 6502 instructions
@vbarr67
@vbarr67 4 жыл бұрын
This is brilliant !
@chessprogramming591
@chessprogramming591 2 жыл бұрын
Watching guys like Jason talking and coding makes me feel like a total noob)))
@georggast6398
@georggast6398 8 жыл бұрын
Very inspiring Talk!
@jschroedl
@jschroedl 8 жыл бұрын
Very cool Jason!
@megoo401
@megoo401 4 жыл бұрын
this is amazing, is there a similar compiler explorer for java?
@AG-ld6rv
@AG-ld6rv Жыл бұрын
For Java, you have the output byte code right there for viewing. You can also run a reverse compiler on it to see what the code looks like after compilation. There might be simplifications like fewer intermediary variables or entire chunks of code reordered. But Java isn't really known for the highest performance, which makes looking into making highly tuned Java code make less sense. When you write something in Java, you are already admitting it's all right if it's about 2-4 times slower than the fastest option, and you make that choice to have an easier language to use that you can make meaningful software with faster and hopefully with fewer bugs. Simply put, all the bells and whistles in C++ make it a harder language to use than most popular languages.
@anthonyrocha8075
@anthonyrocha8075 3 жыл бұрын
where can we find the code for the translator?
@petrusscott3038
@petrusscott3038 2 жыл бұрын
Very informative 👍
@CppCon
@CppCon 2 жыл бұрын
Glad it was helpful!
@AlexisPaques
@AlexisPaques 3 жыл бұрын
It seems my computer just rebooted on me. Just a moment ... [I was expecting] I will quickly recompile the kernel.
@Pracedru
@Pracedru 8 жыл бұрын
pretty amazing.
@JustWasted3HoursHere
@JustWasted3HoursHere 8 жыл бұрын
Modern assemblers, even on the C64, are actually not _that_ hard to program. And it is even possible to use PC-based assemblers to create programs _for_ the C64. There is even at least one modern development system for the Atari 2600! JW3HH
@SerBallister
@SerBallister 7 жыл бұрын
What would be the difficulty in writing an assembler? They are quite straight forward imho. A compiler on the other hand..
@hobbified
@hobbified 8 жыл бұрын
46:40 name them d7 through d0 in that order, then! :)
@ian3084
@ian3084 4 жыл бұрын
Awesome
@64jcl
@64jcl 5 жыл бұрын
A shame you dont show the C64 emulator running the code as well while you do the coding/testing.
@masheroz
@masheroz 2 жыл бұрын
It wasn't an emulator. He had an actual C64.
@drstrangelove09
@drstrangelove09 2 жыл бұрын
what is "overhead"?
@TamasKalman
@TamasKalman 6 жыл бұрын
amazing...!
@selvakumarjawahar
@selvakumarjawahar 8 жыл бұрын
awesome........
@victornoagbodji
@victornoagbodji 8 жыл бұрын
best talk so far for me : ) great job. i looked up the online tool. pretty cool. i was expecting MS Visual C++ to show up in the list of compiler... that would be asking too much i guess.
@victornoagbodji
@victornoagbodji 8 жыл бұрын
awesome!
@MultiClittle
@MultiClittle 7 жыл бұрын
FYI it's there now.
@lebanbo55
@lebanbo55 8 жыл бұрын
1:05:00 The game
@KX36
@KX36 2 жыл бұрын
I would have loved it if when he needed to reboot his computer, the projector displayed multicolour flashing screens for 20 minutes.
@jcamargo2005
@jcamargo2005 2 жыл бұрын
It is certanly interesting to compile existing C/C++ code to old architectures, but the truth is that even C does not suit them very well. I am not convinced using 'modern C++' actually makes you more productive on those architectures. BTW it took me half day to get LLVM working in the Z80. What it can do is pretty amazing, but those are really heavy toolchains.
@SirMo
@SirMo 6 ай бұрын
Yeah. For C64 you have KickAssembler which I think is the preferred way to program it these days, if you want the lowest overhead possible.
@KhalaWarrior55
@KhalaWarrior55 8 жыл бұрын
why's is he adding the auto keyword if all of the functions and classes default to auto?
@АлександрМорженко-поэткаменног
@АлександрМорженко-поэткаменног 4 жыл бұрын
6502 is a brain of f14 tomcat jet fighter
@thesanctuary225
@thesanctuary225 5 жыл бұрын
I want to be a master of C++. How long does it take?
@strictnonconformist7369
@strictnonconformist7369 5 жыл бұрын
Until you’re done. That will be awhile after C++ stops evolving, well after everyone has mastered all its features, or so they think. C++, for better and worse, is perhaps a modern replacement for COBOL but less portable between implementations by its very nature, by design. C++ templates are themselves Turing-complete, and people have used them to have the C++ compiler generate an answer by “running” the template program. It is these sharp edges that will cut you the most, as C++ has a focus on performance, close-to-the-metal, and that means a huge number of things are specified as “implementation left to the vendor” and non-portable.
@jcamargo2005
@jcamargo2005 2 жыл бұрын
All your life, most bloated language ever
@Gameboygenius
@Gameboygenius 8 жыл бұрын
Cpp = crashing powerpoint. :)
@ruadeil_zabelin
@ruadeil_zabelin 7 жыл бұрын
Even the most recent versions? They even changed the Visual Studio IDE to .NET for some reason.
@platin2148
@platin2148 7 жыл бұрын
Kayak Fan Only the GUI wich uses C#. Same for PowerPoint.
@VoyivodaFTW1
@VoyivodaFTW1 2 жыл бұрын
makes game in front of us. Asks if we want to see him play it. OFC you goober!! Show us the goods.
@mryup6100
@mryup6100 4 жыл бұрын
bookmark: 25:28
@mrlithium69
@mrlithium69 7 жыл бұрын
Some overhead? Bueller ? Bueller? No overhead? .....
@WizardNumberNext
@WizardNumberNext 5 жыл бұрын
Seriously? x86_64 have 16 registers and it would be impossible to use all of them on PC (it doesn't matter if it is Windows or Linux or something else, as it is physical architecture which eats up resources, not logical one i386 had 8, of which 6 were (sort of) general purpose this is NEITHER PowerPC (32) NOR SPARC (32 with banks or renaming (I am confused now, as I do not remember) plus each thread have full set of registers)
@DerHeiligste
@DerHeiligste 4 жыл бұрын
I think he's counting the sub-registers separately... so maybe counting AH, AL, and AX as three registers
@SirMo
@SirMo 6 ай бұрын
x86_64 has like 40 registers, if you count SSE registers and the like. Lookup x86_64 register map.
@TheAngelOfDeath01
@TheAngelOfDeath01 4 жыл бұрын
Stupid? No, why would it be stupid. Doing something you enjoy, fancy, and take fun and pleasure in isn't stupid. Creation is never stupid. As for PowerPoint crashing: I must be the only person in the whole world who RARELY experience issues with Windows and Office...What are you all doing to your computer, ladies and gents!?
@johnmccarthy2594
@johnmccarthy2594 3 жыл бұрын
This is the dumbest thing I've ever seen and I love it and want more 😃
@CodexPermutatio
@CodexPermutatio 8 жыл бұрын
Please, edit the title. It says "Commodor" instead of "Commodore".
@defense200x
@defense200x 6 жыл бұрын
they did it :)
@maybephilipp
@maybephilipp Жыл бұрын
For anyone who came here for const moment: 27:59
@sznio
@sznio 8 жыл бұрын
at least at the beginning of the talk, the cameraman must've been so annoyed at you.
@gwentarinokripperinolkjdsf683
@gwentarinokripperinolkjdsf683 2 жыл бұрын
This guy is not a very good presenter, but it's a pretty solid presentation non the less.
@SirMo
@SirMo 6 ай бұрын
He's a very good presenter imo. Very few people present while doing live coding and demoing on a 40 year old computer. Lots of things are going though his mind most likely, also he got struck with the curse of the demos, where his computer crashed right as the presentation started making his keyboard shortcuts malfunction. He handled it pretty well imo.
@TomAtkinson
@TomAtkinson 5 жыл бұрын
Did they say the x86 has hardware sprites?
@luis46coco
@luis46coco 4 жыл бұрын
Nooo, the C64
@SirMo
@SirMo 6 ай бұрын
@@luis46coco Or the VIC-II too chip to be more precise.
@robot_madness3164
@robot_madness3164 8 жыл бұрын
1:01:26 lol
CppCon 2016: Jason Turner “Practical Performance Practices"
1:00:29
The Making of Linux: The World's First Open-Source Operating System
11:33
ForrestKnight
Рет қаралды 1,2 МЛН
Do you choose Inside Out 2 or The Amazing World of Gumball? 🤔
00:19
How To Get Married:   #short
00:22
Jin and Hattie
Рет қаралды 11 МЛН
💩Поу и Поулина ☠️МОЧАТ 😖Хмурых Тварей?!
00:34
Ной Анимация
Рет қаралды 1,7 МЛН
So Cute 🥰
00:17
dednahype
Рет қаралды 66 МЛН
27c3: Reverse Engineering the MOS 6502 CPU (en)
51:57
Christiaan008
Рет қаралды 437 М.
Learn to program C on your Commodore 64 (C64) with Abacus Super-C.
49:38
My Developer Thoughts
Рет қаралды 15 М.
Emulating a CPU in C++ (6502)
52:28
Dave Poo
Рет қаралды 982 М.
I made the same game in Assembly, C and C++
4:20
Nathan Baggs
Рет қаралды 733 М.
Real Programmers Write Machine Code
26:25
ThePrimeTime
Рет қаралды 112 М.
Do you choose Inside Out 2 or The Amazing World of Gumball? 🤔
00:19