Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precompile lua functions to prevent wasted CPU cycles #328

Merged
merged 1 commit into from
May 20, 2023

Conversation

tonyhb
Copy link
Contributor

@tonyhb tonyhb commented May 17, 2023

miniredis.runLuaScript calls lua.*LState.DoString() for every lua invocation, even if scripts are evaluated via checksums.

In turn, lua parses and compiles the same scripts on each call, which can cause high CPU usage on script-heavy test instances.

This PR uses sync.Map to store pre-compiled lua functions for each script, using the same logic as the Lua library itself: tul/gopher-lua@7a6135d

This reduces CPU usage by about 50%.

Before:

Screenshot 2023-05-16 at 18 24 25

After:

Screenshot 2023-05-16 at 18 28 09

This takes runLuaScript's cumulative time from 1.19s to 0.68s, or about a 57% improvement.

tonyhb added a commit to inngest/inngest that referenced this pull request May 17, 2023
This reduces queue ticks from 10ms to 500ms in development, lowering the
number of checks by 50x.  This reduces CPU usage in development, as
miniredis is CPU heavy.

Also, alicebob/miniredis#328 should be
upstreamed when merged, further lowering CPU usage.
@alicebob
Copy link
Owner

Hi,

thanks for the clear PR! It looks good, but some error messages changed. The errors can't have the exact same wording as Redis, but they should use the same -ERR .. prefix. Nothing major, if you could have a look at that, great, otherwise I have a better look another time.

@tonyhb
Copy link
Contributor Author

tonyhb commented May 18, 2023

@alicebob thanks for the quick review! believe that i've fixed that error, appreciate the heads up :)

@tonyhb tonyhb force-pushed the perf/precompile-lua-functions branch from c207b0a to 7c30851 Compare May 18, 2023 12:59
tonyhb added a commit to inngest/inngest that referenced this pull request May 18, 2023
Perf: reduce queue ticks when using dev server to 500ms

This reduces queue ticks from 10ms to 500ms in development, lowering the
number of checks by 50x.  This reduces CPU usage in development, as
miniredis is CPU heavy.

Also, alicebob/miniredis#328 should be
upstreamed when merged, further lowering CPU usage.
@alicebob
Copy link
Owner

Hi,

thanks for the fix. Just the test failure left. I see the Lua code eventually does this in DoString():

return ls.Load(strings.NewReader(source), "")

https://github.com/yuin/gopher-lua/blob/v1.1.0/auxlib.go#L387

so maybe do it that way?

@tonyhb
Copy link
Contributor Author

tonyhb commented May 19, 2023

Aha, didn't notice that — sorry! All fixed. Had to change the name of the input script to match before as you mentioned. Thank you!

`miniredis.runLuaScript` calls lua.*LState.DoString() for every lua
invocation, even if scripts are evaluated via checksums.

In turn, lua parses and compiles the same scripts on each call, which
can cause high CPU usage on script-heavy test instances.

This PR uses `sync.Map` to store pre-compiled lua functions for each
script, using the same logic as the Lua library itself:
tul/gopher-lua@7a6135d

This reduces CPU usage by about 50%.
@tonyhb tonyhb force-pushed the perf/precompile-lua-functions branch from 723ab68 to 536e717 Compare May 19, 2023 18:53
@alicebob alicebob merged commit a3211de into alicebob:master May 20, 2023
4 checks passed
@alicebob
Copy link
Owner

Thanks! It's now in master.

@tonyhb tonyhb deleted the perf/precompile-lua-functions branch May 24, 2023 00:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants