2024-07-17 04:34:29 Hello? I've been slowly making my way through Starting Forth, and in chapter 8 now. I'm having trouble figuring out, what is the difference between `32 CONSTANT SPACE` and `: SPACE 32 ;`? 2024-07-17 04:40:31 UnderSampled: to the user, nothing. and you're probably not there yet, but this is why i really question the value of create/does> as it's usually implemented 2024-07-17 04:40:58 Is it answered later in the chapter? 2024-07-17 04:41:29 but i guess a more satisfying answer is that the constant is almost certainly smaller in memory and probably neglibly faster 2024-07-17 04:42:09 I'm happy to hear the expected implimentation difference 2024-07-17 04:43:10 Please tell me? :smile: 2024-07-17 04:43:27 tell you what? 2024-07-17 04:43:58 fwiw, i've seen chuck moore use the : space 32 ; form quite a bit 2024-07-17 04:43:59 The difference of what gets stored in the dictionary, which would make CONSTANT slightly smaller and faster than : 2024-07-17 04:46:27 oh, with the traditional threaded implementation, constant compiles only a code field that points to a small doconst routine (which just pushes the parameter and then jumps to the next word), followed by the value. 2024-07-17 04:48:25 the colon version will compile to a code field pointing to a docol routine, which has to push the IP to tbe return stack and then setup the new IP to begin in SPACE, then execute the LIT word which pushes the value from the execution stream, followed by the value, and then an exit 2024-07-17 04:49:25 in other words, in the dictionary you have this: [(doconst)][32] versus this: [(docol)][lit][32][exit] 2024-07-17 04:50:21 neglecting headers, of course. those are the same either way. 2024-07-17 04:52:11 gotcha. One has to mess with the return stack, while CONST doesn't? 2024-07-17 04:52:25 right 2024-07-17 04:52:34 what is `col` short for? 2024-07-17 04:52:40 colon 2024-07-17 04:52:42 ah 2024-07-17 04:52:47 Thanks! 2024-07-17 04:52:59 np 2024-07-17 04:53:17 When you say "traditional threaded implimentation", is that not the standard normal? 2024-07-17 04:53:46 or I guess there's some sort of optomizations applied in some modern forths? 2024-07-17 04:54:10 wouldn't it still be threaded? 2024-07-17 04:54:23 there are forths today that compile to native machine code 2024-07-17 04:55:02 as opposed to? 2024-07-17 04:55:24 as opposed to threaded code 2024-07-17 04:56:11 when i say threaded code, i don't mean multithreading. it's a vm technique. 2024-07-17 04:56:37 ah. I just found the wiki page for it 2024-07-17 04:56:58 definitely was thinking multithreaded. good catch 2024-07-17 04:57:14 (or single-threaded) 2024-07-17 04:57:22 one of those unfortunate context sensitive terms 2024-07-17 04:59:00 anyway, historically forth is often implemented that way because it makes it dead simple to implement, but it doesn't have to be 2024-07-17 04:59:14 ye 2024-07-17 05:05:26 Undersampled: Re: 32 CONSTANT SPACE and : SPACE 32 ; there is no difference in the effect. 2024-07-17 05:05:35 Both of them will result in 32 being placed on the stack. 2024-07-17 05:05:57 But the first implementation is a Forth "constant," whereas the second is actualy Forth executable code. 2024-07-17 05:06:28 The second one would let you put a + after the 32, and you'd have a word that ADDED 32 to whatever was already on the stack. 2024-07-17 05:06:47 Whereas CONSTANT only does exactly that - just places a supplied value on the stack. 2024-07-17 05:07:29 The CONSTANT will likely be faster, because it doesn't have to save your location in your current definition and aim the instruction pointer at a new definition, and then restore the old value when done. 2024-07-17 05:07:47 CONSTANT is implemented by a small bit of machine code that doesn't need to change the instruction pointer. 2024-07-17 05:09:04 anybody else hear an echo? 2024-07-17 05:27:17 Thank you for the help. Farewell! 2024-07-17 16:15:42 I imagine an optimizing forth would just turn : thing 3 ; into a constant 2024-07-17 16:15:55 I kinda feel all forths should maybe do that lol 2024-07-17 16:16:12 > [(doconst)][32] versus this: [(docol)][lit][32][exit] 2024-07-17 16:16:20 Seems like such an easy thing to optimize 2024-07-17 16:17:24 Couldn't you make it an immediate which inlined the value into the current definition? 2024-07-17 16:19:05 I have no idea but that sounds like one! :) 2024-07-17 16:19:39 I feel like even forths for embedded systems could get away with this small optimization 2024-07-17 16:20:02 Maybe there's something I'm missing. Meta programming maybe becomes a problem? 2024-07-17 16:31:37 lf94: : const create , immediate does> @ state @ if postpone literal then ; 2024-07-17 16:33:46 lf94: https://ideone.com/DOFYMJ 2024-07-17 16:35:32 i think he was talking about recognizing the ": thing 3 ;" pattern and turning those into that 2024-07-17 16:36:33 Pfft! That just adds complexity to the compiler :P 2024-07-17 16:37:46 Well, the constant in gforth at least already inlines the value 2024-07-17 16:43:36 [[zel yea 2024-07-17 16:43:41 zelgomer: yea* 2024-07-17 16:43:49 So I guess constant does simplify things 2024-07-17 17:39:05 Yeah I mean if you want constant folding use CONSTANT, compilers that do simple inlining would probably be able to fold that example as well 2024-07-17 17:39:27 And STC compilers that optimise CONSTANT's might get that example for free because it would be the same code 2024-07-17 17:39:31 as with CONSTANT 2024-07-17 19:20:10 if doing `:thing #3 ;` is too inefficient, I'd just bind the value to a name and use my & sigil, making this `#3 \thing`, then `&thing` to inline the value 2024-07-17 19:23:47 http://forth.works/share/CvkymL9ywj.txt is a block implementing a `const` word for doing `#3 'thing const`, still using the & sigil to access 2024-07-17 21:09:06 I do fall into the "if it needs optimising it should be written in assembly" camp 2024-07-17 21:09:26 Which certainly this inlined vs non-inlined constants / constant folding falls into 2024-07-17 21:53:49 I just found out the hard way that pforth's implementation of local variables uses HERE for its storage space. So you can't use it in defining words that allocate extra space in the dictionary with , or ALLOT. :/ 2024-07-17 21:53:55 This is more than slightly annoying.