Skip to content

Conversation

@lewis6991
Copy link
Member

This change adds the necessary plumbing to annotate functions in funcs.c
as being allowed in run in luv fast events.

@github-actions github-actions bot added the lua stdlib label Apr 29, 2022
@lewis6991 lewis6991 force-pushed the fnfast branch 2 times, most recently from ac888db to 2c3b9f2 Compare April 29, 2022 16:37
enumerates functions that were called at least once.

Note: The majority of functions cannot run in fast callbacks with some
undocumented exceptions which are allowed.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

once we get around to generating eval.txt, this will follow. #18311

@lewis6991
Copy link
Member Author

@bfredl are you able to describe the conditions that determine whether a function can be allowed/disallowed from running in fast events? Is it just textlock?

@lewis6991 lewis6991 marked this pull request as ready for review May 1, 2022 15:55
@tjdevries
Copy link
Contributor

(doesn't have to be in this PR) but I was wondering if it made sense to create a new table on vim (something like vim.fast_fn) for any of the vim functions that are available in fast?

I'm not sure if it's a good idea or if it exposes too much confusion, but the reason I suggest that is because then vim.fast_fn will always error when someone tries to call something that isn't fast, as opposed to vim.fn sometimes errors depending on how the function is called.

Haven't thought this through a ton yet though, so I could be completely wrong about the idea.

@justinmk
Copy link
Member

justinmk commented May 3, 2022

vim.fast_fn will always error when someone tries to call something that isn't fast, as opposed to vim.fn sometimes errors depending on how the function is called.

We need to be a lot less willing to add random modules. Let's try to make vim.fn do the right thing (even if that currently just means an error).

@tjdevries
Copy link
Contributor

I'm saying, the error can be confusing for users because it sometimes errors.

I'm totally fine w/ us not adding another module, I just wanted to at least surface the issue and at least make sure we had considered it (since I didn't see it mentioned)

@lewis6991
Copy link
Member Author

To begin with vim.fn in fast will be the exception rather than the norm, but as the list grows we need to make sure this is properly documented. It's similar to how some API functions will error if textlock is active.

One thing I like about vim.fast_fn is it allows doing something like :lua =vim.fast_fn to quickly see all functions which can run in fast. However, I don't think we should go that route since it is just creating duplication since the original versions in vim.fn will need to remain. Maybe adding this into something like nvim_get_api_info or similar (probably the latter) might be better.

@bfredl
Copy link
Member

bfredl commented May 3, 2022

I suspect calling these are being pretty niche, and not something most lua plugins are going to need consider (you can write many a nice lua plugin without going into the fast event loop at all, or schedule_wrap all callbacks). So a separate module would be overkill.

@tjdevries
Copy link
Contributor

Sounds good to me :)

@justinmk
Copy link
Member

justinmk commented May 3, 2022

To begin with vim.fn in fast will be the exception rather than the norm, but as the list grows we need to make sure this is properly documented.

I think we can get that, and treesitter-based checks, and auto-completion, by annotation/flags. Even if that just means our docstring API: #12040

@lewis6991
Copy link
Member Author

Anything else required here?

return 1;
}

static bool viml_func_is_deferred_safe(const char_u *name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name is confusing because it returns fast but that's the opposite of "deferred".

Let's avoid needless entropy by using the same jargon everywhere by default (i.e. unless there's a clear reason to diverge). Consistent with nlua_in_fast_event, I would suggest a name like viml_func_is_fast.

size_t name_len;
const char_u *name = (const char_u *)luaL_checklstring(lstate, 1, &name_len);
if (!nlua_is_deferred_safe()) {
if (!nlua_is_deferred_safe() && !viml_func_is_deferred_safe(name)) {
Copy link
Member

@justinmk justinmk May 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nlua_is_deferred_safe returns false if not in a fast context.

I think maybe nlua_is_deferred_safe should be modified to check find_internal_func().fast, we don't need a new function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally did this, but @bfredl suggested I separated it. Since nlua_is_deferred_safe is used in a ton of places in the API functions I think I agree it should be split. This new condition is only relevant to nvim_call.

This change adds the necessary plumbing to annotate functions in funcs.c
as being allowed in run in luv fast events.
Copy link
Member

@justinmk justinmk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgmt if (1) confirm that the test correctly fails, (2) @bfredl signoff

thanks for getting this started!

@lewis6991
Copy link
Member Author

(1) confirm that the test correctly fails

Can confirm test fails locally if fast=false in eval.lua

@lewis6991
Copy link
Member Author

@bfredl @justinmk : ok to merge?

@dundargoc dundargoc requested review from bfredl and justinmk May 26, 2022 10:49
@justinmk
Copy link
Member

lgtm as before :) but still would like @bfredl signoff. Maybe mark a few more functions as fast?

@bfredl bfredl merged commit 30915cc into neovim:master May 26, 2022
@github-actions github-actions bot removed request for bfredl and justinmk May 26, 2022 13:03
@lewis6991
Copy link
Member Author

I want to hold off adding more functions until I understand what properties a function must have for it to be safe to run in fast.

Referencing an earlier comment:

@bfredl are you able to describe the conditions that determine whether a function can be allowed/disallowed from running in fast events? Is it just textlock?

@bfredl any advice before I start randomly adding functions to the list?

@lewis6991 lewis6991 deleted the fnfast branch May 26, 2022 13:04
@bfredl
Copy link
Member

bfredl commented May 26, 2022

I'd rather starting at the end at solving some problem (from either built-in or plugin Lua code), like iconv did.

@lewis6991
Copy link
Member Author

lewis6991 commented May 26, 2022

My main motivation here is to reduce the risk of Github issues that simply just involveE5560: vimL function must not be called in a lua loop callback, for some particular code path I forgot to schedule onto the main event. When you have a good async module, it becomes very easy to hit these.

I would rather we just mark all the functions we can to mitigate the potential of these issues. If a function really needs to run on the main event, then fine, but otherwise (so far) this has felt like death by a thousand paper cuts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lua stdlib

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants