# Insane Optimization Tricks & LPH Macro Usage

{% hint style="info" %}
Luarmor uses [Luraph™️](https://lura.ph) as the obfuscation provider. So if you're experiencing lags, fps drops, or even crashes, you must use certain "macro"s in your script. This documentation will show you everything about the LPH\_NO\_VIRTUALIZE macro.
{% endhint %}

{% hint style="success" %}
**We already know** that your code "runs fine with source" and "crashes when obfuscated"
{% endhint %}

## Why does obfuscation affect performance?

Because [Luraph™️](https://lura.ph) is a VM based obfuscator that generates its own instructions that are interpreted by Luraph, which, in turn, is interpreted by the Lua interpreter. So the number of instruction cycles increases exponentially.

Normally, your script gets compiled into a set of Lua instructions. See the example below:

<figure><img src="https://3136584356-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwOpQH2LpMjsOwgeFM5gE%2Fuploads%2FJw3L8RksDkPCbCvYBXIN%2Fimage.png?alt=media&#x26;token=9c4f6a54-de07-4af3-9a53-402339c92c55" alt=""><figcaption></figcaption></figure>

As you can see, your simple "print" statement gets compiled into 4 instructions. Each instruction is a cycle, so it will take **4 cycles to execute your code**.&#x20;

When you obfuscate your script, the main goal is to make the original code unreadable. Luraph does this by generating its custom instruction set that can be understood by Luraph VM only.&#x20;

See the example below:

<figure><img src="https://3136584356-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwOpQH2LpMjsOwgeFM5gE%2Fuploads%2FNzz12w2dXBzhvy3OQxAw%2Fimage.png?alt=media&#x26;token=8f7ed35c-9c9e-4942-bdf7-fea625db35f1" alt=""><figcaption></figcaption></figure>

Notice how your simple print statement turned into 40 instructions, which will take **40 cycles to execute.** Also keep in mind that Luraph does a lot of things to obfuscate the code flow, which will ultimately lead to **even more instructions**.

Normally, Lua is extremely fast, therefore you **will not notice** any performance impact. But when your code runs **hundreds of times per second**, there will be obvious lags, fps drops, or even freezes during the execution of the instructions above.

A good example would be **RenderStepped** connections. At this point, everyone knows that you love wrapping your wallhack render function inside this RenderStepped event.

<figure><img src="https://3136584356-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwOpQH2LpMjsOwgeFM5gE%2Fuploads%2FM7q6KzkNHjwx2jwbKShI%2Fimage.png?alt=media&#x26;token=0e16d564-7445-41f8-a017-794f13521fff" alt=""><figcaption></figcaption></figure>

This code will be slow when obfuscated. Because the number of instructions generated by Luraph will be more than **2000**. This means Lua will have to run **2000+ instruction cycles** EVERY frame. Which will be around **60 \* 2000 = 120k** instructions to run every second.

And there are local functions (e.g "*CalculateParameters*") used in this loop, which automatically means that function will be executed as well, which will lead to **even more and more instructions** to run.

Here is a second example. A metamethod hook on \_\_index

<figure><img src="https://3136584356-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwOpQH2LpMjsOwgeFM5gE%2Fuploads%2FGpasMJRsBPUoDu8bAsVe%2Fimage.png?alt=media&#x26;token=713a1a08-4c90-415e-8e38-87f7b04a5fcb" alt=""><figcaption></figcaption></figure>

\_\_index runs quite often. Example: game.Workspace, game.Players... \
These will invoke the \_\_index function and run the code above. This will be extremely resource intensive and heavy, considering that it is probably called **thousands of times per second**.

And on top of that, **if you obfuscate this part**, you will end up with **more than a million** instructions to run every second, which will crash your game.

## How to deal with this problem?

You have to exclude these chunks from obfuscation. You can either use loadstring(), or LPH\_NO\_VIRTUALIZE.

It is recommended to use **LPH\_NO\_VIRTUALIZE** instead of loadstring while excluding chunks from obfuscation, because it is generally harder to view the code when you're using LPH\_NO\_VIRTUALIZE.&#x20;

{% hint style="warning" %}
**Do not** wrap your entire script in LPH\_NO\_VIRTUALIZE as it defeats the purpose of virtualization (obfuscation). Only use this macro if a function runs more than 30 times per second.
{% endhint %}

## How to use LPH\_NO\_VIRTUALIZE?

LPH\_NO\_VIRTUALIZE takes one constant argument which must be a function, and returns a function that can be called.

{% hint style="warning" %}
**It is important** to add this on top of your script, so you won't have issues while running the original code.

```lua
loadstring([[
    function LPH_NO_VIRTUALIZE(f) return f end;
]])();
```

{% endhint %}

{% hint style="success" %}
{% code title="Good example 1:" lineNumbers="true" %}

```lua
RunService.RenderStepped( LPH_NO_VIRTUALIZE( function(s)
   -- regular function body
end ))
```

{% endcode %}
{% endhint %}

{% hint style="success" %}
{% code title="Good example 2:" lineNumbers="true" %}

```lua
old = hookmetamethod(game, "__index", LPH_NO_VIRTUALIZE( function(t, k)
    -- regular function body
end ))
```

{% endcode %}
{% endhint %}

{% hint style="success" %}
{% code title="Good example 3:" lineNumbers="true" %}

```lua
local generateTracers = LPH_NO_VIRTUALIZE( function(pos, pos2)
    -- function body
end )
heartbeat:Connect(generateTracers)
```

{% endcode %}
{% endhint %}

{% hint style="success" %}
{% code title="Good example 4:" lineNumbers="true" %}

```lua
LPH_NO_VIRTUALIZE(function()
   for i,v in pairs(getgc()) do 
       if type(v) == 'table' then
           f = v
       end
   end
end)()

```

{% endcode %}
{% endhint %}

{% hint style="danger" %}
{% code title="Bad example 1:" lineNumbers="true" %}

```lua
local function doSomething()
   -- function body
end
LPH_NO_VIRTUALIZE(doSomething) -- you can't pass reference arguments
hookfunction(print, doSomething)
```

{% endcode %}
{% endhint %}

{% hint style="danger" %}
{% code title="Bad example 2:" lineNumbers="true" %}

```lua
LPH_NO_VIRTUALIZE( -- this is a syntax error
local old old = hookmetamethod(game, "__namecall", function(...) end)
)
```

{% endcode %}
{% endhint %}

{% hint style="danger" %}
{% code title="Bad example 3:" lineNumbers="true" %}

```lua
LPH_NO_VIRTUALIZE(function()
-- something
end) -- This part will not run because you are not calling it at the end.
-- You should add an extra () at the end in order to call it

print('done')
```

{% endcode %}
{% endhint %}

## Where to use LPH\_NO\_VIRTUALIZE?

* RenderStepped connections
* Heartbeat connections
* \_\_index hooks
* \_\_namecall hooks (optional)
* while true loops with no delay
* GC scan loops
* functions, if they are called by one of those above.
