Verse Clamp(float), Min, and Max are non-native

Summary

In /Verse.org/Verse (Verse.digest.verse:2128–2167), Clamp(int) and float math intrinsics (Sqrt, Sin, Cos, Tan) are @rtfm_always_open, while Clamp(float), Min(int/float), and Max(int/float) are non-native (= external {}). In server microbenchmarks, built-in Clamp(float) is ~8.8x slower than a one-line inline if/else clamp (~11.4 µs vs. ~1.3 µs per call).

In active multiplayer islands, float clamping runs heavily inside per-tick UI animations, progress bars, movement curves, and smoothers, needlessly eating into server tick headroom.

Please select what you are reporting on:

Verse

What Type of Bug are you experiencing?

Verse

Steps to Reproduce

  1. Add the minimal benchmark below to a Verse device in UEFN (43.x) and call BenchmarkFloatClamp() during an active session:

BenchmarkFloatClamp():void=
Iterations := 50000
var AccA: float = 0.0
var AccB: float = 0.0

T0 := GetSecondsSinceEpoch()
for (I := 1..Iterations):
    X := (I * 0.00013) - 2.0
    set AccA += Clamp(X, 0.0, 1.0)
T1 := GetSecondsSinceEpoch()

for (I := 1..Iterations):
    X := (I * 0.00013) - 2.0
    set AccB += if (X < 0.0) then 0.0 else if (X > 1.0) then 1.0 else X
T2 := GetSecondsSinceEpoch()

Print("Built-in Clamp(float): {(T1 - T0) * 1000.0} ms | Inline if/else: {(T2 - T1) * 1000.0} ms | Check: {AccA}, {AccB}")
  1. Launch a session and compare the printed timings for built-in Clamp(float) vs. inline if/else.

Expected Result

Built-in Clamp(float), Min, and Max execute as intrinsics (matching Clamp(int) and Sqrt/Sin/Cos/Tan) at speeds equal to or faster than a hand-written inline if/else.

Observed Result

  • Built-in Clamp(Val:float, A:float, B:float) averages ~11.4 µs/call vs. ~1.3 µs/call for an inline if/else helper (~8.8x slower, 0.05% in-run repeatability).
  • Min(Max(V, Lo), Hi) still carries +4.5 µs/call overhead because Min and Max are also non-native = external {}.
  • In Verse.digest.verse:2128–2155:
    – Clamp(int) and Sqrt are declared @rtfm_always_open.
    – Clamp(float), Min(int/float), and Max(int/float) are declared = external {}

Platform(s)

UEFN Server (all platforms)

FORT-1208458 has been created and its status is ‘Unconfirmed’. This is now in a queue to be reproduced and confirmed.

Wait, this does not make sense… you are not even supposed to be able to use GetSecondsSinceEpoch() like that, since calling it during the same tick/transaction should always give the same value frozen in time… (Only updates on suspends contexts across multiple ticks elapsed, same as GetSimulationElapsedTime())

If it is giving different values, then that is a bug and not intended behavior, not matching the specification and documentations.

Also just to annotate, this is the implementation of the built in Clamp/Min/Max functions (from Unreal Engine 6 source code):

Can you do the tests again, but now properly using profile{}? that is the expected macro to do benchmarks and profiling timings…

yeah great call out, the reproduction code is a little silly in retrospect and I appreciate the correction. I should not have rushed this report.

Re-tested using profile(...), the performance gap still reproduces:

Block Mean (ms) Net / Call (minus empty loop) Ratio
EmptyFloatLoop 17.46 — —
Inline if/else (float) 30.69 0.66 µs 1.0x
User ClampFloat (float) 45.16 1.39 µs 2.1x
Min(Max(float)) (= external {}) 87.10 3.48 µs 5.3x
Clamp(float) (= external {}) 173.59 7.81 µs 11.8x
EmptyIntLoop 13.46 — —
Clamp(int) (<native>) 21.67 0.41 µs 1.0x
Inline if/else (int) 32.26 0.94 µs 2.3x
Min(Max(int)) (= external {}) 68.58 2.76 µs 6.7x

<native> Clamp(int) (0.41 µs) outperforms inline if/else, while non-<native> Clamp(float) (7.81 µs) is ~11.8x slower than inline and ~5.6x slower than a user-defined Verse helper.

Updated profile(...) repro:

using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }

ClampFloat<public>(V: float, Lo: float, Hi: float)<computes>:float =
    if (V < Lo) then Lo else if (V > Hi) then Hi else V

clamp_benchmark_device := class(creative_device):
    OnBegin<override>()<suspends>:void =
        Sleep(2.0)
        Iterations := 20000
        var Acc: float = 0.0
        var IntAcc: int = 0

        Sleep(0.1)
        profile("01_EmptyFloatLoop"):
            for (I := 1..Iterations):
                X := (I * 0.00013) - 2.0
                set Acc += X
        Sleep(0.1)
        profile("02_Builtin_Clamp_Float"):
            for (I := 1..Iterations):
                X := (I * 0.00013) - 2.0
                set Acc += Clamp(X, 0.0, 1.0)
        Sleep(0.1)
        profile("03_Builtin_MinMax_Float"):
            for (I := 1..Iterations):
                X := (I * 0.00013) - 2.0
                set Acc += Min(Max(X, 0.0), 1.0)
        Sleep(0.1)
        profile("04_UserFn_ClampFloat"):
            for (I := 1..Iterations):
                X := (I * 0.00013) - 2.0
                set Acc += ClampFloat(X, 0.0, 1.0)
        Sleep(0.1)
        profile("05_Inline_IfElse_Float"):
            for (I := 1..Iterations):
                X := (I * 0.00013) - 2.0
                set Acc += if (X < 0.0) then 0.0 else if (X > 1.0) then 1.0 else X
        Sleep(0.1)
        profile("06_EmptyIntLoop"):
            for (I := 1..Iterations):
                set IntAcc += I
        Sleep(0.1)
        profile("07_Builtin_Clamp_Int_Native"):
            for (I := 1..Iterations):
                set IntAcc += Clamp(I, 500, 15000)
        Sleep(0.1)
        profile("08_Builtin_MinMax_Int"):
            for (I := 1..Iterations):
                set IntAcc += Min(Max(I, 500), 15000)
        Print("Check: {Acc}, {IntAcc}")