A better CREATE ... DOES>
TG9541 opened this issue · 1 comments
Better means faster and smaller. The new solution size and speed is about the same as an ordinary CREATE, VARIABLE or CONSTANT:
: EMPTY CREATE DOES> ;
EMPTY tempty
CREATE tcreate
VARIABLE tvar
: testempty FOR P1L tempty P1H DROP NEXT ;
: testcreate FOR P1L tcreate P1H DROP NEXT ;
: testvar FOR P1L tvar P1H DROP NEXT ;
The new solution is only marginally slower than a pure CREATE
and on par with VARIABLE
:
10 testempty
2µs:
The measurements where done with PulseView on a STM8S001J3M3 with 16MHz HSI and code compiled to Flash ROM. The pin-debug adds 2 cycles (0.25µs) to the time.
Here are new measurements: the "empty" DOES>
(get address of the data stored by during a word definition) has a runtime of 1.82µs. That's actually a bit faster than VARIABLE
and just 0.5µs slower than CREATE
.
The old CREATE ... DOES>
needs 3.85µs for the same task (i.e. twice as long as the new implementation).
The simple constant value implementation : dcons CREATE , DOES> @ ;
is also just a bit slower than the literal stored by CONSTANT
(the literal uses the STM8 instruction TRAP
and requires 3 byte, like the CALL
to the word defined through dcons
).
Memory requirements:
[bytes] | old | new | diff |
---|---|---|---|
: empty CREATE DOES> ; |
22 | 18 | 4 |
empty a |
13 | 7 | 6 |
STM8S001J3 binary | 4697 | 4662 | 35 |
The old implementation needs 4 bytes more for a "defining word" and 6 bytes more for a "defined" (the new DOES>
has the same memory needs as a word defined by CREATE
, CONSTANT
or VARIABLE
). Also the STM8 eForth binary is 35 bytes smaller than before.