Skip to content

perf!: cache UserData fields#2572

Merged
sxyazi merged 1 commit intomainfrom
pr-b26055f4
Apr 4, 2025
Merged

perf!: cache UserData fields#2572
sxyazi merged 1 commit intomainfrom
pr-b26055f4

Conversation

@sxyazi
Copy link
Owner

@sxyazi sxyazi commented Apr 4, 2025

This PR fixes a longstanding issue in the plugin system where every field access triggers redundant memory allocation. For example, when getting the currently selected file Url:

cx.active.current.hovered.url
cx.active.current.cwd
-- ...

Every time the .url field is accessed, a new Url is allocated. During UI rendering, these Urls are used repeatedly in multiple render functions, leading to unnecessary memory allocation.

Also, each access of .active, .current, and .hovered creates a new UserData. Although UserData is lighter compared to Urls, they still all need to be destroyed after rendering ends.

With this PR, these field accesses will be globally cached and created only once. This means that every function will share the same object, avoiding repeated memory allocation and destruction.

⚠️ Breaking changes

  • The methods frag, name, stem, ext, and parent on Url are now properties.
  • The method name on tab::Tab is now a property.
  • The method is_hovered on fs::File is now a property.

For instance:

- local name = url:name()
+ local name = url.name

Previously, these were methods to hint that each call would create a new object. With field caching, they are now treated as cacheable properties and are guaranteed to allocate memory at most once on access, simplifying the code at the same time.

@sxyazi sxyazi merged commit c7dbf3a into main Apr 4, 2025
6 checks passed
@sxyazi sxyazi deleted the pr-b26055f4 branch April 4, 2025 11:45
sxyazi added a commit that referenced this pull request Apr 4, 2025
@boydaihungst
Copy link
Contributor

boydaihungst commented Apr 4, 2025

ya.manager_emit/ya.mgr_emit throw this error, also new:

  2025-04-04T20:27:57.202500Z ERROR yazi::app::commands::plugin: Sync plugin `json` failed: unsupported userdata included
stack traceback:
	[C]: in field 'mgr_emit'
	[string "code"]:21: in function 'code.seek'
	>[C]: in method 'seek'
	?: in function 'json.seek'

    at yazi-fm/src/app/commands/plugin.rs:79

Also extract.lua plugin still using old :parent() instead of .parent: https://github.com/sxyazi/yazi/blob/75fad253c0948a06bcfed57b99ca60ecca39a0c3/yazi-plugin/preset/plugins/extract.lua#L42C13-L42C26

sxyazi added a commit that referenced this pull request Apr 4, 2025
@sxyazi
Copy link
Owner Author

sxyazi commented Apr 4, 2025

Thanks for reporting, fixed in 9565c1b

GrzegorzKozub added a commit to GrzegorzKozub/yazi that referenced this pull request Apr 5, 2025
@boydaihungst
Copy link
Contributor

@sxyazi This also broke plugin vcs-files:
https://github.com/yazi-rs/plugins/blob/9a095057d698aaaedc4dd23d638285bd3fd647e9/vcs-files.yazi/main.lua#L26

Here is the error message.

Failed to run the plugin:
userdata is not expected type
stack traceback:
	[C]: in field 'op'
	[string "vcs-files"]:29: in function <[string "vcs-files"]:7>

@sxyazi
Copy link
Owner Author

sxyazi commented Apr 5, 2025

Fixed in fd007ab

Lemi0002 pushed a commit to Lemi0002/yazi that referenced this pull request Apr 10, 2025
imsi32 added a commit to imsi32/yatline.yazi that referenced this pull request Apr 11, 2025
details: With the new release of Yazi, some methods are changed to
properties.

see:
- sxyazi/yazi#2572
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 6, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants