diff options
| author | Roberto I <roberto@inf.puc-rio.br> | 2025-09-24 18:33:08 -0300 |
|---|---|---|
| committer | Roberto I <roberto@inf.puc-rio.br> | 2025-09-24 18:33:08 -0300 |
| commit | 25c54fe60e22d05cdfaa48c64372d354efa59547 (patch) | |
| tree | 3ccaeded5e4363db358f73b7c8fc6b9f414a2f2a /manual | |
| parent | 0cc3c9447cca9abae9738ee77c24d88801c3916c (diff) | |
| download | lua-25c54fe60e22d05cdfaa48c64372d354efa59547.tar.gz lua-25c54fe60e22d05cdfaa48c64372d354efa59547.tar.bz2 lua-25c54fe60e22d05cdfaa48c64372d354efa59547.zip | |
Optimization for vararg tables
A vararg table can be virtual. If the vararg table is used only as
a base in indexing expressions, the code does not need to create an
actual table for it. Instead, it compiles the indexing expressions
into direct accesses to the internal vararg data.
Diffstat (limited to 'manual')
| -rw-r--r-- | manual/manual.of | 91 |
1 files changed, 61 insertions, 30 deletions
diff --git a/manual/manual.of b/manual/manual.of index 3c704118..beea41f9 100644 --- a/manual/manual.of +++ b/manual/manual.of | |||
| @@ -2262,7 +2262,7 @@ return x or f(x) -- results adjusted to 1 | |||
| 2262 | 2262 | ||
| 2263 | @sect3{func-def| @title{Function Definitions} | 2263 | @sect3{func-def| @title{Function Definitions} |
| 2264 | 2264 | ||
| 2265 | The syntax for function definition is | 2265 | The syntax for a function definition is |
| 2266 | @Produc{ | 2266 | @Produc{ |
| 2267 | @producname{functiondef}@producbody{@Rw{function} funcbody} | 2267 | @producname{functiondef}@producbody{@Rw{function} funcbody} |
| 2268 | @producname{funcbody}@producbody{@bnfter{(} @bnfopt{parlist} @bnfter{)} block @Rw{end}} | 2268 | @producname{funcbody}@producbody{@bnfter{(} @bnfopt{parlist} @bnfter{)} block @Rw{end}} |
| @@ -2315,6 +2315,18 @@ translates to | |||
| 2315 | global f; f = function () @rep{body} end | 2315 | global f; f = function () @rep{body} end |
| 2316 | } | 2316 | } |
| 2317 | 2317 | ||
| 2318 | The @emphx{colon} syntax | ||
| 2319 | is used to emulate @def{methods}, | ||
| 2320 | adding an implicit extra parameter @idx{self} to the function. | ||
| 2321 | Thus, the statement | ||
| 2322 | @verbatim{ | ||
| 2323 | function t.a.b.c:f (@rep{params}) @rep{body} end | ||
| 2324 | } | ||
| 2325 | is syntactic sugar for | ||
| 2326 | @verbatim{ | ||
| 2327 | t.a.b.c.f = function (self, @rep{params}) @rep{body} end | ||
| 2328 | } | ||
| 2329 | |||
| 2318 | A function definition is an executable expression, | 2330 | A function definition is an executable expression, |
| 2319 | whose value has type @emph{function}. | 2331 | whose value has type @emph{function}. |
| 2320 | When Lua precompiles a chunk, | 2332 | When Lua precompiles a chunk, |
| @@ -2325,11 +2337,25 @@ the function is @emph{instantiated} (or @emph{closed}). | |||
| 2325 | This function instance, or @emphx{closure}, | 2337 | This function instance, or @emphx{closure}, |
| 2326 | is the final value of the expression. | 2338 | is the final value of the expression. |
| 2327 | 2339 | ||
| 2340 | Results are returned using the @Rw{return} statement @see{control}. | ||
| 2341 | If control reaches the end of a function | ||
| 2342 | without encountering a @Rw{return} statement, | ||
| 2343 | then the function returns with no results. | ||
| 2344 | |||
| 2345 | @index{multiple return} | ||
| 2346 | There is a system-dependent limit on the number of values | ||
| 2347 | that a function may return. | ||
| 2348 | This limit is guaranteed to be at least 1000. | ||
| 2349 | |||
| 2350 | @sect4{@title{Parameters} | ||
| 2351 | |||
| 2328 | Parameters act as local variables that are | 2352 | Parameters act as local variables that are |
| 2329 | initialized with the argument values: | 2353 | initialized with the argument values: |
| 2330 | @Produc{ | 2354 | @Produc{ |
| 2331 | @producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} @bnfter{...}} @Or | 2355 | @producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} varargparam} @Or |
| 2332 | @bnfter{...}} | 2356 | varargparam} |
| 2357 | @producname{varargparam}@producbody{@bnfter{...} | ||
| 2358 | @bnfopt{@bnfter{|} @bnfNter{Name}}} | ||
| 2333 | } | 2359 | } |
| 2334 | When a Lua function is called, | 2360 | When a Lua function is called, |
| 2335 | it adjusts its list of @x{arguments} to | 2361 | it adjusts its list of @x{arguments} to |
| @@ -2339,11 +2365,12 @@ which is indicated by three dots (@Char{...}) | |||
| 2339 | at the end of its parameter list. | 2365 | at the end of its parameter list. |
| 2340 | A variadic function does not adjust its argument list; | 2366 | A variadic function does not adjust its argument list; |
| 2341 | instead, it collects all extra arguments and supplies them | 2367 | instead, it collects all extra arguments and supplies them |
| 2342 | to the function through a @def{vararg expression}, | 2368 | to the function through a @def{vararg expression} and, |
| 2343 | which is also written as three dots. | 2369 | if present, a @def{vararg table}. |
| 2344 | The value of this expression is a list of all actual extra arguments, | ||
| 2345 | similar to a function with multiple results @see{multires}. | ||
| 2346 | 2370 | ||
| 2371 | A vararg expression is also written as three dots, | ||
| 2372 | and its value is a list of all actual extra arguments, | ||
| 2373 | similar to a function with multiple results @see{multires}. | ||
| 2347 | 2374 | ||
| 2348 | As an example, consider the following definitions: | 2375 | As an example, consider the following definitions: |
| 2349 | @verbatim{ | 2376 | @verbatim{ |
| @@ -2368,26 +2395,27 @@ g(3, 4, 5, 8) a=3, b=4, ... -> 5 8 | |||
| 2368 | g(5, r()) a=5, b=1, ... -> 2 3 | 2395 | g(5, r()) a=5, b=1, ... -> 2 3 |
| 2369 | } | 2396 | } |
| 2370 | 2397 | ||
| 2371 | Results are returned using the @Rw{return} statement @see{control}. | 2398 | The presence of a varag table in a variadic function is indicated |
| 2372 | If control reaches the end of a function | 2399 | by the @T{|name} syntax after the three dots. |
| 2373 | without encountering a @Rw{return} statement, | 2400 | When present, |
| 2374 | then the function returns with no results. | 2401 | a vararg table behaves like a read-only local variable |
| 2375 | 2402 | with the given name that is initialized with a table. | |
| 2376 | @index{multiple return} | 2403 | In that table, |
| 2377 | There is a system-dependent limit on the number of values | 2404 | the values at indices 1, 2, etc. are the extra arguments, |
| 2378 | that a function may return. | 2405 | and the value at index @St{n} is the number of extra arguments. |
| 2379 | This limit is guaranteed to be at least 1000. | 2406 | In other words, the code behaves as if the function started with |
| 2380 | 2407 | the following statement, | |
| 2381 | The @emphx{colon} syntax | 2408 | assuming the standard behavior of @Lid{table.pack}: |
| 2382 | is used to emulate @def{methods}, | ||
| 2383 | adding an implicit extra parameter @idx{self} to the function. | ||
| 2384 | Thus, the statement | ||
| 2385 | @verbatim{ | 2409 | @verbatim{ |
| 2386 | function t.a.b.c:f (@rep{params}) @rep{body} end | 2410 | local <const> name = table.pack(...) |
| 2387 | } | 2411 | } |
| 2388 | is syntactic sugar for | 2412 | |
| 2389 | @verbatim{ | 2413 | As an optimization, |
| 2390 | t.a.b.c.f = function (self, @rep{params}) @rep{body} end | 2414 | if the vararg table is used only as a base in indexing expressions |
| 2415 | (the @T{t} in @T{t[exp]} or @T{t.id}) and it is not an upvalue, | ||
| 2416 | the code does not create an actual table and instead translates | ||
| 2417 | the indexing expressions into accesses to the internal vararg data. | ||
| 2418 | |||
| 2391 | } | 2419 | } |
| 2392 | 2420 | ||
| 2393 | } | 2421 | } |
| @@ -2422,7 +2450,7 @@ for instance @T{foo(e1, e2, e3)} @see{functioncall}.} | |||
| 2422 | for instance @T{a , b, c = e1, e2, e3} @see{assignment}.} | 2450 | for instance @T{a , b, c = e1, e2, e3} @see{assignment}.} |
| 2423 | 2451 | ||
| 2424 | @item{A local or global declaration, | 2452 | @item{A local or global declaration, |
| 2425 | which is a special case of multiple assignment.} | 2453 | which is similar to a multiple assignment.} |
| 2426 | 2454 | ||
| 2427 | @item{The initial values in a generic @rw{for} loop, | 2455 | @item{The initial values in a generic @rw{for} loop, |
| 2428 | for instance @T{for k in e1, e2, e3 do ... end} @see{for}.} | 2456 | for instance @T{for k in e1, e2, e3 do ... end} @see{for}.} |
| @@ -3016,7 +3044,7 @@ typedef void * (*lua_Alloc) (void *ud, | |||
| 3016 | size_t osize, | 3044 | size_t osize, |
| 3017 | size_t nsize);| | 3045 | size_t nsize);| |
| 3018 | 3046 | ||
| 3019 | The type of the @x{memory-allocation function} used by Lua states. | 3047 | The type of the @x{memory-allocator function} used by Lua states. |
| 3020 | The allocator function must provide a | 3048 | The allocator function must provide a |
| 3021 | functionality similar to @id{realloc}, | 3049 | functionality similar to @id{realloc}, |
| 3022 | but not exactly the same. | 3050 | but not exactly the same. |
| @@ -3482,7 +3510,7 @@ This function should not be called by a finalizer. | |||
| 3482 | @APIEntry{lua_Alloc lua_getallocf (lua_State *L, void **ud);| | 3510 | @APIEntry{lua_Alloc lua_getallocf (lua_State *L, void **ud);| |
| 3483 | @apii{0,0,-} | 3511 | @apii{0,0,-} |
| 3484 | 3512 | ||
| 3485 | Returns the @x{memory-allocation function} of a given state. | 3513 | Returns the @x{memory-allocator function} of a given state. |
| 3486 | If @id{ud} is not @id{NULL}, Lua stores in @T{*ud} the | 3514 | If @id{ud} is not @id{NULL}, Lua stores in @T{*ud} the |
| 3487 | opaque pointer given when the memory-allocator function was set. | 3515 | opaque pointer given when the memory-allocator function was set. |
| 3488 | 3516 | ||
| @@ -9732,8 +9760,11 @@ and @bnfNter{LiteralString}, see @See{lexical}.) | |||
| 9732 | 9760 | ||
| 9733 | @producname{funcbody}@producbody{@bnfter{(} @bnfopt{parlist} @bnfter{)} block @Rw{end}} | 9761 | @producname{funcbody}@producbody{@bnfter{(} @bnfopt{parlist} @bnfter{)} block @Rw{end}} |
| 9734 | 9762 | ||
| 9735 | @producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} @bnfter{...}} | 9763 | @producname{parlist}@producbody{namelist @bnfopt{@bnfter{,} varargparam} @Or |
| 9736 | @Or @bnfter{...}} | 9764 | varargparam} |
| 9765 | |||
| 9766 | @producname{varargparam}@producbody{@bnfter{...} | ||
| 9767 | @bnfopt{@bnfter{|} @bnfNter{Name}}} | ||
| 9737 | 9768 | ||
| 9738 | @producname{tableconstructor}@producbody{@bnfter{@Open} @bnfopt{fieldlist} @bnfter{@Close}} | 9769 | @producname{tableconstructor}@producbody{@bnfter{@Open} @bnfopt{fieldlist} @bnfter{@Close}} |
| 9739 | 9770 | ||
