[GH-ISSUE #3183] Render thread reads the clock once per dropped item per frame: ~18% of frame time on machines using the HPET clocksource #866

Closed
opened 2026-11-02 15:42:28 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @Utsavkattel000 on GitHub (Oct 28, 2026).
Original GitHub issue: https://github.com/horizon-rust/horizon/issues/3183

What happens

When many item entities are on screen, the render thread spends a large share of each frame in clock_gettime. Game::entity_geometry reaches Instant::elapsed()/Timespec::now once per rendered item, through Game::glint() and Game::glint_ticks() (crates/horizon-client/src/game/item_draw.rs, around lines 318–338). Each call also reads glintSpeed/glintStrength through OptionsFile::double, which shows up as hash_one<&str>/SipHash in the profile.

On a normal TSC clocksource each read is about 20–30 ns. On machines where the kernel falls back to HPET, each read is a syscall plus an HPET access, about 1.6 µs measured here, and the per-item reads become a major part of the frame.

Environment

  • Horizon v0.2.93 (launcher 0.2.19), Linux 7.0.0-38-generic, KDE Plasma (Wayland)
  • Lenovo Legion 5 15ACH6, Ryzen 5 5600H, RTX 3050 Ti Laptop GPU, BIOS HHCN37WW
  • Clocksource hpet: the firmware leaves CPU0's TSC about 2.14 s off the other cores, so the kernel logs Marking TSC unstable due to check_tsc_sync_source failed and switches to hpet. This happens on every boot. Plenty of AMD laptops (and some VMs) run on hpet or acpi_pm for the same kind of reason.

Measurements (perf, 15 s at 499 Hz, with several thousand dropped items in view)

  • Render thread at 100% of one core, GPU at 34%, so the game was CPU-bound.
  • Render thread time: Timespec::now was ~18% inclusive (13.3% of all process samples, while the render thread made up 72.8%).
    • Almost all of it came through entity_geometry (1272 samples, against 23 from glint outside it and 12 from draw_item_model).
    • Kernel side: _copy_to_user 11.3%, read_hpet 5.0%, __x64_sys_clock_gettime 5.3%.
  • The same session with few items in view: Timespec::now was ~3% of the render thread.
  • Cost of one read on this machine: ~1,600 ns per clock_gettime(CLOCK_MONOTONIC) (a TSC machine takes about 20–30 ns).

Comparison with vanilla 26.3

Checked with javap on the 26.3 client jar:

  • The glint animation reads the clock in vanilla too: TextureTransform.setupGlintTexturing(float) calls Util.getMillis() and reads OptionsRenderState.glintSpeed.
  • Vanilla reaches it only through RenderType.prepare() → writeDynamicTransforms → TextureTransform.createMatrix(), called from RenderTypeFeatureRenderer$Group.getOrAddDraw(RenderType). That is once per glint render-type batch per frame, and only when a glint render type is drawn.
  • ItemEntityRenderer has no clock reads (getMillis, nanoTime, currentTimeMillis).
  • Horizon's submit_item_world and submit_item_unlit evaluate glint() (clock read plus two option lookups by name) once per submitted item, including items with no foil. In the repro below (9610 cobblestone drops) vanilla reads the clock 0 times for glint per frame; Horizon reads it once per visible item.

Steps to reproduce

  1. Use a machine on the HPET clocksource. On a machine that normally uses TSC you can switch it for the session (it reverts on reboot):
    cat /sys/devices/system/clocksource/clocksource0/available_clocksource
    echo hpet | sudo tee /sys/devices/system/clocksource/clocksource0/current_clocksource
    
  2. Create a creative world with cheats on.
  3. Make thousands of item entities at your feet:
    /fill ~-15 ~-1 ~-15 ~15 ~-10 ~15 minecraft:stone
    /fill ~-15 ~-1 ~-15 ~15 ~-10 ~15 minecraft:air destroy
    
    (31×10×31 = 9610 blocks, all dropping as items.)
  4. Look at the drops and note the FPS (F3).
  5. Optional comparison: switch back with echo tsc | sudo tee …/current_clocksource and note the FPS for the same view.

How to measure

# Sample the running game for 15 s
perf record -F 499 -g -p "$(pgrep horizon-client)" -o horizon.data -- sleep 15

# Share of each thread
perf report -i horizon.data --no-children -g none --sort comm --stdio

# Inclusive cost of clock reads on the render thread
perf report -i horizon.data --children --comm horizon-client --sort sym -g none --stdio | grep -E 'Timespec>::now|clock_gettime|read_hpet|entity_geometry'

With the control socket you can also get an FPS figure: horizon-ctl launch --world <name>, then horizon-ctl bench with the items in view and again without them.

Originally created by @Utsavkattel000 on GitHub (Oct 28, 2026). Original GitHub issue: https://github.com/horizon-rust/horizon/issues/3183 ### What happens When many item entities are on screen, the render thread spends a large share of each frame in `clock_gettime`. `Game::entity_geometry` reaches `Instant::elapsed()`/`Timespec::now` once per rendered item, through `Game::glint()` and `Game::glint_ticks()` (`crates/horizon-client/src/game/item_draw.rs`, around lines 318–338). Each call also reads `glintSpeed`/`glintStrength` through `OptionsFile::double`, which shows up as `hash_one<&str>`/SipHash in the profile. On a normal TSC clocksource each read is about 20–30 ns. On machines where the kernel falls back to HPET, each read is a syscall plus an HPET access, about 1.6 µs measured here, and the per-item reads become a major part of the frame. ### Environment - Horizon v0.2.93 (launcher 0.2.19), Linux 7.0.0-38-generic, KDE Plasma (Wayland) - Lenovo Legion 5 15ACH6, Ryzen 5 5600H, RTX 3050 Ti Laptop GPU, BIOS HHCN37WW - Clocksource `hpet`: the firmware leaves CPU0's TSC about 2.14 s off the other cores, so the kernel logs `Marking TSC unstable due to check_tsc_sync_source failed` and switches to hpet. This happens on every boot. Plenty of AMD laptops (and some VMs) run on hpet or acpi_pm for the same kind of reason. ### Measurements (perf, 15 s at 499 Hz, with several thousand dropped items in view) - Render thread at 100% of one core, GPU at 34%, so the game was CPU-bound. - Render thread time: `Timespec::now` was **~18%** inclusive (13.3% of all process samples, while the render thread made up 72.8%). - Almost all of it came through `entity_geometry` (1272 samples, against 23 from `glint` outside it and 12 from `draw_item_model`). - Kernel side: `_copy_to_user` 11.3%, `read_hpet` 5.0%, `__x64_sys_clock_gettime` 5.3%. - The same session with few items in view: `Timespec::now` was ~3% of the render thread. - Cost of one read on this machine: ~1,600 ns per `clock_gettime(CLOCK_MONOTONIC)` (a TSC machine takes about 20–30 ns). ### Comparison with vanilla 26.3 Checked with `javap` on the 26.3 client jar: - The glint animation reads the clock in vanilla too: `TextureTransform.setupGlintTexturing(float)` calls `Util.getMillis()` and reads `OptionsRenderState.glintSpeed`. - Vanilla reaches it only through `RenderType.prepare()` → `writeDynamicTransforms` → `TextureTransform.createMatrix()`, called from `RenderTypeFeatureRenderer$Group.getOrAddDraw(RenderType)`. That is once per glint render-type batch per frame, and only when a glint render type is drawn. - `ItemEntityRenderer` has no clock reads (`getMillis`, `nanoTime`, `currentTimeMillis`). - Horizon's `submit_item_world` and `submit_item_unlit` evaluate `glint()` (clock read plus two option lookups by name) once per submitted item, including items with no foil. In the repro below (9610 cobblestone drops) vanilla reads the clock 0 times for glint per frame; Horizon reads it once per visible item. ### Steps to reproduce 1. Use a machine on the HPET clocksource. On a machine that normally uses TSC you can switch it for the session (it reverts on reboot): ```sh cat /sys/devices/system/clocksource/clocksource0/available_clocksource echo hpet | sudo tee /sys/devices/system/clocksource/clocksource0/current_clocksource ``` 2. Create a creative world with cheats on. 3. Make thousands of item entities at your feet: ``` /fill ~-15 ~-1 ~-15 ~15 ~-10 ~15 minecraft:stone /fill ~-15 ~-1 ~-15 ~15 ~-10 ~15 minecraft:air destroy ``` (31×10×31 = 9610 blocks, all dropping as items.) 4. Look at the drops and note the FPS (F3). 5. Optional comparison: switch back with `echo tsc | sudo tee …/current_clocksource` and note the FPS for the same view. ### How to measure ```sh # Sample the running game for 15 s perf record -F 499 -g -p "$(pgrep horizon-client)" -o horizon.data -- sleep 15 # Share of each thread perf report -i horizon.data --no-children -g none --sort comm --stdio # Inclusive cost of clock reads on the render thread perf report -i horizon.data --children --comm horizon-client --sort sym -g none --stdio | grep -E 'Timespec>::now|clock_gettime|read_hpet|entity_geometry' ``` With the control socket you can also get an FPS figure: `horizon-ctl launch --world <name>`, then `horizon-ctl bench` with the items in view and again without them.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#866
No description provided.