Self-modifying Code - Usage - Interaction of Cache and Self-modifying Code

Interaction of Cache and Self-modifying Code

On architectures without coupled data and instruction cache (some ARM and MIPS cores) the cache synchronization must be explicitly performed by the modifying code (flush data cache and invalidate instruction cache for the modified memory area).

In some cases short sections of self-modifying code execute more slowly on modern processors. This is because a modern processor will usually try to keep blocks of code in its cache memory. Each time the program rewrites a part of itself, the rewritten part must be loaded into the cache again, which results in a slight delay, if the modified codelet shares the same cache line with the modifying code, as is the case when the modified memory address is located within a few bytes to the one of the modifying code.

The cache invalidation issue on modern processors usually means that self-modifying code would still be faster only when the modification will occur rarely, such as in the case of a state switching inside an inner loop.

Most modern processors load the machine code before they execute it, which means that if an instruction that is too near the instruction pointer is modified, the processor will not notice, but instead execute the code as it was before it was modified. See prefetch input queue (PIQ). PC processors must handle self-modifying code correctly for backwards compatibility reasons but they are far from efficient at doing so.

Read more about this topic:  Self-modifying Code, Usage

Famous quotes containing the words interaction of, interaction and/or code:

    Here is this vast, savage, howling mother of ours, Nature, lying all around, with such beauty, and such affection for her children, as the leopard; and yet we are so early weaned from her breast to society, to that culture which is exclusively an interaction of man on man,—a sort of breeding in and in, which produces at most a merely English nobility, a civilization destined to have a speedy limit.
    Henry David Thoreau (1817–1862)

    Our rural village life was a purifying, uplifting influence that fortified us against the later impacts of urbanization; Church and State, because they were separated and friendly, had spiritual and ethical standards that were mutually enriching; freedom and discipline, individualism and collectivity, nature and nurture in their interaction promised an ever stronger democracy. I have no illusions that those simpler, happier days can be resurrected.
    Agnes E. Meyer (1887–1970)

    Wise Draco comes, deep in the midnight roll
    Of black artillery; he comes, though late;
    In code corroborating Calvin’s creed
    And cynic tyrannies of honest kings;
    He comes, nor parlies; and the Town, redeemed,
    Gives thanks devout; nor, being thankful, heeds
    The grimy slur on the Republic’s faith implied,
    Which holds that Man is naturally good,
    And—more—is Nature’s Roman, never to be
    scourged.
    Herman Melville (1819–1891)