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

Use raw table access for fresh tables in the generated code #14

Open
mjanicek opened this issue Oct 24, 2016 · 0 comments
Open

Use raw table access for fresh tables in the generated code #14

mjanicek opened this issue Oct 24, 2016 · 0 comments

Comments

@mjanicek
Copy link
Owner

The compiler currently routes all non-constructor table accesses via Dispatch, in order to correctly handle possible metamethod calls. When it is known that the table cannot have a metatable, its accesses may be raw.

Raw table accesses are preferable to non-raw accesses, since the former does not require the insertion of resumption points, making the generated bytecode smaller and simpler (fewer entry points), giving the JVM more opportunities to perform JIT optimisations.

Consider the following Lua snippet (the first 80 numbers in the Fibonacci series):

local t = {1, 1}
for i = 3, 80 do
  t[i] = t[i - 2] + t[i - 1]
end

Here, all accesses of t can be raw, since the freshly-allocated t does not have a metatable, and t does not participate in any operation that may modify its metatable.

If the snippet above was followed by

for k, v in ipairs(t) do
  print(k, v)
end
print(#t)  -- non-raw, t escaped

then the #t operation would not be raw, since t has been passed to ipairs as an argument: and its contents including the metatable may have been modified.

How to go about this

This feature could be implemented by a simple escape analysis.

The compiler should keep track of the "status" of table locals and temporaries. Tables are constructed fresh; table operations (t[x], t[x] = y and #t) on fresh tables are raw and do not change the table status. Whenever a table participates in a call (as an argument, e.g., f(t)) or in operation that may involve a metamethod call (e.g., t + 1) their status changes to dirty. Table operations on dirty tables are invoked via Dispatch, i.e., with checking for metamethods.

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

No branches or pull requests

1 participant