Ticket UUID: | 827119 | |||
Title: | TIP 157 implementation - {expand} | |||
Type: | Patch | Version: | TIP Implementation | |
Submitter: | dgp | Created on: | 2003-10-20 20:31:54 | |
Subsystem: | 45. Parsing and Eval | Assigned To: | dgp | |
Priority: | 5 Medium | Severity: | ||
Status: | Closed | Last Modified: | 2003-11-15 05:26:37 | |
Resolution: | Accepted | Closed By: | dgp | |
Closed on: | 2003-11-14 20:44:12 | |||
Description: |
Here's a first draft implementation of TIP 157 as a patch to the HEAD. Note changes to the public interface. Besides the script level {expand} expansion, a new Tcl_Token type #define TCL_TOKEN_EXPAND_WORD 256 is in tcl.h and a new bytecode opcode #define INST_LCONCAT1 100 is in tclCompile.h Patch does not yet include documentation or test updates. Contributions for both are welcome, as well as mucho banging on it in search of bugs. There are alternative implementation ideas out there too, I'm sure, particularly on better ways to handle bytecode compiling of the {expand} operation. Feel free to treat this as the straw man. | |||
User Comments: |
msofer added on 2003-11-15 05:26:37:
Logged In: YES user_id=148712 Moved the discussion of the list-based alternative (tip157.patch.2) to [Patch 842446] dgp added on 2003-11-15 03:44:13: File Added - 67522: 827119.patch dgp added on 2003-11-15 03:44:12: Logged In: YES user_id=80530 File 827119.patch is the combined patch that is now being applied to the HEAD. Further optimization, additional tests, etc. can take place as ongoing development. dkf added on 2003-11-14 22:37:03: Logged In: YES user_id=79902 TIPs updated msofer added on 2003-11-14 19:57:23: File Deleted - 67067: File Added - 67480: tip157.patch.2 vincentdarley added on 2003-11-14 19:39:04: Logged In: YES user_id=32170 NB. In the TIP archive, tip144 needs to be withdrawn, and tip157 needs to specify Tcl version 8.5 (not 9.0). msofer added on 2003-11-14 07:22:19: Logged In: YES user_id=148712 I agree with donal: patch approved, apply to HEAD. I'll let you do it, as you mentioned a possible last round of changes. We can still think of alternative implementations afterwards. dkf added on 2003-11-14 06:22:09: Logged In: YES user_id=79902 If the patch works, apply it. If there are optimizations to be done, they can be done later on. dgp added on 2003-11-14 05:13:54: File Added - 67441: tip157-imm-3.patch Logged In: YES user_id=80530 here's another alternative patch, tip157-imm-3.patch It stores deltas between words need expansion as immediate bytes in the code stream. Also, it builds up the delta list in a Tcl_DString instead of a Tcl_ListObj for less memory allocation overhead. Also, unnecessary INST_LIST_VERIFY opcodes are left out. Could probably use more whitebox tests tuned to its particular branch points. msofer added on 2003-11-11 05:59:59: File Deleted - 67065: File Added - 67067: tip157.patch.2 msofer added on 2003-11-11 05:59:58: Logged In: YES user_id=148712 oops - removing latest patch.3 (wrong), back to revised patch.2 msofer added on 2003-11-11 05:01:17: File Deleted - 66997: File Added - 67065: tip157.patch.3 msofer added on 2003-11-11 05:01:16: Logged In: YES user_id=148712 Updated patch.2 - slight change, added comments. msofer added on 2003-11-10 17:25:34: File Deleted - 66970: File Added - 66997: tip157.patch.2 msofer added on 2003-11-10 17:25:33: Logged In: YES user_id=148712 Updated patch.2 (fix copy-paste error) msofer added on 2003-11-10 09:00:47: File Added - 66970: tip157.patch.2 Logged In: YES user_id=148712 Here's the 2nd revision of the "invoke list at stacktop" approach. Improved efficiency in building the list, reducing the memory churn. dgp added on 2003-11-08 05:46:57: File Added - 66826: tip157-imm-2.patch Logged In: YES user_id=80530 Here's the next revision on the index list as immediates in the codestream approach. The execution of the INST_INVOKE_EXP* instructions are now simplified to not attempt complex "in-place" expansion. Instead expansion is done into the stack expansion space above the tosPtr. Refcount management is also left to the bytecode execution engine macros. This code should be considerably simpler to understand. msofer added on 2003-11-07 20:34:51: File Added - 66775: tip157.patch.1 Logged In: YES user_id=148712 Enclosing a new patch tip157.patch.1 This is a modification of dgp's tip157.patch, keeping the original logic of building the command to invoke as a list object at stacktop. The differences to the original are: - new INST_INVOKE_LIST that replaces the call to INST_EVAL_STK and fixes the object-safety bug - modification of INST_LCONCAT1 to improve efficiency. The list handling in the bcc code can (and will) be improved further; the hope is that the runtime penalty vs the approach in tip157-imm.patch will be small enough to justify using this simpler approach. dgp added on 2003-11-07 04:44:05: File Added - 66711: tip157-imm.patch dgp added on 2003-11-07 04:44:04: Logged In: YES user_id=80530 Here's an alternative patch that emits lists of word indices to be expanded directly into the codestream (as new operand types OPERAND_ULIST1 or OPERANCE_ULIST4). New instructions INST_INVOKE_EXP1 and INST_INVOKE_EXP4 read the immediate index lists and perform expansion before going to a modified doInvocation to take care of the command evaluation. This patch corrects test basic-48.17.0, indicating proper object safety of expansion. I'm keeping the former patch registered this time, for sake of comparison by interested reviewers. dgp added on 2003-10-29 04:45:36: File Deleted - 65603: File Added - 65724: tip157.patch Logged In: YES user_id=80530 Latest patch defines and uses a new opcode INST_LIST_VERIFY to verify list validity before expansion with less cost than before. dgp added on 2003-10-28 04:59:57: File Deleted - 65324: File Added - 65603: tip157.patch Logged In: YES user_id=80530 New patch removes some objv shifting in Tcl_EvalEx. dgp added on 2003-10-27 20:46:29: Logged In: YES user_id=80530 dkf's outline has many similarities to what is done in the patch to support argument expansion during direct string evaluation. msofer added on 2003-10-26 19:32:47: Logged In: YES user_id=148712 Doc proposal exposed in the wiki: http://mini.net/tcl/Dodekalogue msofer added on 2003-10-26 06:21:00: File Added - 65434: Tcl.n.patch Logged In: YES user_id=148712 Attached my first attempt at Tcl.n revision for this Tip; RFC msofer added on 2003-10-26 04:18:46: Logged In: YES user_id=148712 Having trouble with the endekalogue: all my attempts to document {expand} seem to require to incoporate the concept of 'list' to Tcl.n, a concept which was not necessary up to now. Does anybody have a simple proposal? I guess I deserve this ... dkf added on 2003-10-25 05:27:13: Logged In: YES user_id=79902 I suspect that we could use a better compilation strategy than purely list concatenation, as that does a lot of object-churn. My idea goes something like this: Two new opcodes (though one might be simulatable using existing opcodes - I don't know for sure): INST_VERIFY_LIST and INST_EXPAND_STK (might want it in 1/4 forms). INST_VERIFY_LIST checks that the object on the top of the stack is a list; it *doesn't* pop anything off the stack, but it throws an error if it isn't a list at the stack top. INST_EXPAND_STK has one immediate value which is the number of words to evaluate (much as in INST_INVOKE_STK) but it also reads an additional object off the top of the stack (not counted?) which is a *sorted* list of integer indices to expand. Compilation proceeds as follows: Generate words for command execution as normal. If any of the words are expanded, add a trailing INST_VERIFY_LIST (required for correct functioning of basic-48.19.*) and append the index of the word within the whole command invokation to a list you are building. If at the end of the command you've got a non-empty list, push the list (which is guaranteed to be sorted ints) and use INST_EXPAND_STK instead of INST_INVOKE_STK. This simplifies the compiler *and* allows for a more efficient strategy for use of temporary space when building the actual list of argument objects. I proposed such a strategy for expansion compilation back in the discussions for TIP#144 on tcl-core dgp added on 2003-10-24 21:57:20: File Deleted - 65227: File Added - 65324: tip157.patch Logged In: YES user_id=80530 New patch updates the [eval]'s found in the script library and test suite that were only for word expansion to use the {expand} syntax, and those [eval]'s that were used only to force direct string evaluation to [testevalex]. No updates done to the bundled pacakges, which may want to consider the timing of when they [package require Tcl 8.5]. Upon further reflection, it seems that the failing test basic-48.17.0 really is a bug, as performance/object safety was one of the motivators for TIP 157. Fixing it will require another bytecode, and perhaps opens an opportunity for the more ambitious bytecoders to look for the best solutions. dgp added on 2003-10-24 00:52:44: File Deleted - 65203: File Added - 65227: tip157.patch Logged In: YES user_id=80530 latest patch adds more tests in compile.test, simplifies the code in tclCompile.c a bit. Note the knownBug tests that highlight a potential ability to overflow objc . dgp added on 2003-10-23 21:52:10: File Deleted - 65008: File Added - 65203: tip157.patch Logged In: YES user_id=80530 Updated patch adds more tests, moves those from cmdAH.test into basic.test, fixes a few bugs, and add docs for the parser changes. Three tests fail, revealing not exactly bugs, but inconsistencies between compiled operation and direct string evaluation. dgp added on 2003-10-22 03:58:28: File Deleted - 64974: File Added - 65008: tip157.patch Logged In: YES user_id=80530 Updated patch adds several tests to parse.test (and updates it to tcltest 2) dgp added on 2003-10-21 21:56:42: File Deleted - 64922: File Added - 64974: tip157.patch Logged In: YES user_id=80530 Updated patch corrects all known flaws. Includes several tests from Peter Spjuth. dgp added on 2003-10-21 13:28:33: Logged In: YES user_id=80530 Looks like an "off-by-one" in the in-place objv array expansion shift. Will look again tomorrow. dgp added on 2003-10-21 12:25:17: Logged In: YES user_id=80530 Crash: % set l1 [list a {b c} d] a {b c} d % list {expand}$l1 a {b c} d % !! list {expand}$l1 a {b c} d % !! list {expand}$l1 Segmentation fault Note that the use of "!!" apparently matters. msofer added on 2003-10-21 05:05:24: File Deleted - 64913: File Added - 64922: tip157.patch Logged In: YES user_id=148712 The new INST was missing in the tclInstructionTable (tclCompile.c); patch updated. dgp added on 2003-10-21 03:45:52: Logged In: YES user_id=80530 Probably worthwhile to note that the controversial syntax choice between TIPs 157 and 144 are completely determined by line 337 of the patched tclParse.c. Change static char expPfx[] = "expand"; to static char expPfx[] = ""; and this patch becomes an implementation of TIP 144. dgp added on 2003-10-21 03:31:54: File Added - 64913: tip157.patch |
Attachments:
- 827119.patch [download] added by dgp on 2003-11-15 03:44:12. [details]
- tip157.patch.2 [download] added by msofer on 2003-11-14 19:57:23. [details]
- tip157-imm-3.patch [download] added by dgp on 2003-11-14 05:13:54. [details]
- tip157-imm-2.patch [download] added by dgp on 2003-11-08 05:46:57. [details]
- tip157.patch.1 [download] added by msofer on 2003-11-07 20:34:51. [details]
- tip157-imm.patch [download] added by dgp on 2003-11-07 04:44:04. [details]
- tip157.patch [download] added by dgp on 2003-10-29 04:45:36. [details]
- Tcl.n.patch [download] added by msofer on 2003-10-26 06:21:00. [details]