# Semantics and Optimization of Copying Function Bodies

**URL:** <https://revolt.metaeducation.com/forum/t/semantics-and-optimization-of-copying-function-bodies/2119>\
**Category:** Optimization\
**Created:** [January 16, 2024, 9:00am UTC](https://revolt.metaeducation.com/forum/t/semantics-and-optimization-of-copying-function-bodies/2119 "2024-01-16T09:00:09Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![hostilefork](https://revolt.metaeducation.com/forum/user_avatar/revolt.metaeducation.com/hostilefork/32/421_2.png) [@hostilefork](https://revolt.metaeducation.com/forum/u/hostilefork)\
**Post date:** [June 19, 2025, 7:42am UTC](https://revolt.metaeducation.com/forum/t/semantics-and-optimization-of-copying-function-bodies/2119/4 "2025-06-19T07:42:59Z")

</div>

This optimization has to be rethought in light of [$word binding to environments](https://revolt.metaeducation.com/forum/t/should-word-capture-the-whole-binding-environment/2494).

Here's a code fragment for assigning the properties:

```
REBINT n = maybe Try_Get_Binder_Index(
    unwrap binder, Cell_Word_Symbol(v)
);
CELL_WORD_INDEX_I32(v) = -(n); // negative or zero signals unbound [2]
Tweak_Cell_Relative_Binding(v, unwrap relative);

```

For deciphering it:

```
// there's an attempted acceleration of the search by caching a
// function's identity in WORD! cells and a negative index that tells
// you where to find the variable if that word is searched in a frame
// for that function.

if (
    CTX_TYPE(vlist) == TYPE_FRAME
    and binding // word has a cache for if it's in an action frame
    and Is_Stub_Details(binding)
    and Action_Is_Base_Of(
        cast(Phase*, binding),
        cast(ParamList*, vlist)
    )
){
    assert(CELL_WORD_INDEX_I32(any_word) <= 0);
    if (CELL_WORD_INDEX_I32(any_word) == 0)
        goto next_context;
    *index_out = -(CELL_WORD_INDEX_I32(any_word));
    return vlist;
}

```

Something from caching that slowed things down and was disabled:

```
// Note: if frame, caching here seems to slow things down?
if (CTX_TYPE(vlist) == TYPE_FRAME) {
    if (CELL_WORD_INDEX_I32(any_word) <= 0) { // cache in unbounds
        CELL_WORD_INDEX_I32(
            m_cast(Cell*, any_word)
        ) = -(maybe index);
        Tweak_Cell_Binding(
            m_cast(Cell*, any_word),
            Phase_Details(vlist)
        );
    }
}

```

---

_[View the full topic](https://revolt.metaeducation.com/forum/t/semantics-and-optimization-of-copying-function-bodies/2119)._
