github.com/arnodel/golua@v0.0.0-20230215163904-e0b5347eaaa1/lib/tablelib/lua/tablelib.quotas.lua (about)

     1  -- table.concat
     2  local function mk(s, n)
     3      local t = {}
     4      for i = 1, n do
     5          t[i] = s
     6      end
     7      return t
     8  end
     9  
    10  do
    11      s1000 = ("a"):rep(1000)
    12  
    13      -- table.concat uses memory
    14  
    15      local ctx, res = runtime.callcontext({kill={memory=5000}}, table.concat, mk(s1000, 4))
    16      print(ctx, #res)
    17      --> =done	4000
    18      print(ctx.used.memory >= 4000)
    19      --> =true
    20  
    21      print(runtime.callcontext({kill={memory=5000}}, table.concat, mk(s1000, 5)))
    22      --> =killed
    23  
    24      -- table.concat uses cpu
    25      local ctx, res = runtime.callcontext({kill={cpu=1000}}, table.concat, mk("x", 100))
    26      print(ctx, #res)
    27      --> =done	100
    28      print(ctx.used.cpu >= 100)
    29      --> =true
    30  
    31      print(runtime.callcontext({kill={cpu=1000}}, table.concat, mk("x", 1000)))
    32      --> =killed
    33  end
    34  
    35  -- table.insert
    36  do
    37      -- cpu is consumed when inserting at the front
    38      local ctx = runtime.callcontext({kill={cpu=1000}}, table.insert, mk("x", 100), 1, "new")
    39      print(ctx)
    40      --> =done
    41      print(ctx.used.cpu >= 100)
    42      --> =true
    43  
    44      print(runtime.callcontext({kill={cpu=1000}}, table.insert, mk("x", 1000), 1, "new"))
    45      --> =killed
    46  
    47      -- at the back, cpu doesn't depend on the size of the table
    48      local ctx1 = runtime.callcontext({kill={cpu=1000}}, table.insert, mk("x", 100), "new")
    49      print(ctx)
    50      --> =done
    51      print(ctx.used.cpu >= 100)
    52      --> =true
    53  
    54      local ctx2 = runtime.callcontext({kill={cpu=1000}}, table.insert, mk("x", 1000), "new")
    55      print(ctx2)
    56      --> =done
    57  
    58      print(ctx2.used.cpu / ctx1.used.cpu < 1.2)
    59      --> =true
    60  end
    61  
    62  -- table.move
    63  do
    64      -- table.move consumes memory
    65  
    66      local ctx = runtime.callcontext({kill={memory=10000}}, table.move, mk("x", 100), 1, 100, 101)
    67      print(ctx)
    68      --> =done
    69  
    70      print(runtime.callcontext({kill={memory=10000}}, table.move, mk("x", 1000), 1, 1000, 1001))
    71      --> =killed
    72  
    73      -- consumes less memory when ranges overlap
    74      ctx = runtime.callcontext({kill={memory=10000}}, table.move, mk("x", 1000), 1, 1000, 101)
    75      print(ctx)
    76      --> =done
    77  
    78      -- table.move consumes cpu
    79  
    80      local ctx = runtime.callcontext({kill={cpu=1000}}, table.move, mk("x", 100), 1, 100, 101)
    81      print(ctx)
    82      --> =done
    83  
    84      print(runtime.callcontext({kill={cpu=1000}}, table.move, mk("x", 1000), 1, 1000, 1001))
    85      --> =killed
    86  
    87      -- still consumes as much cpu when ranges overlap
    88      print(runtime.callcontext({kill={cpu=1000}}, table.move, mk("x", 1000), 1, 1000, 101))
    89      --> =killed
    90  end
    91  
    92  -- table.pack
    93  do
    94      --table.pack consumes memory
    95  
    96      local ctx = runtime.callcontext({kill={memory=10000}}, table.pack, table.unpack(mk("x", 100)))
    97      print(ctx)
    98      --> =done
    99  
   100      print(runtime.callcontext({kill={memory=5000}}, table.pack, table.unpack(mk("x", 200))))
   101      --> =killed
   102  
   103      --table.pack consumes cpu
   104  
   105      local ctx = runtime.callcontext({kill={cpu=200}}, table.pack, table.unpack(mk("x", 100)))
   106      print(ctx)
   107      --> =done
   108  
   109      print(runtime.callcontext({kill={cpu=200}}, table.pack, table.unpack(mk("x", 200))))
   110      --> =killed
   111  end
   112  
   113  -- table.remove
   114  do
   115      -- cpu is consumed when removing at the front
   116      local ctx = runtime.callcontext({kill={cpu=1000}}, table.remove, mk("x", 100), 1)
   117      print(ctx)
   118      --> =done
   119      print(ctx.used.cpu >= 100)
   120      --> =true
   121  
   122      print(runtime.callcontext({kill={cpu=1000}}, table.remove, mk("x", 1000), 1))
   123      --> =killed
   124  
   125      -- at the back, cpu doesn't depend on the size of the table
   126      local ctx1 = runtime.callcontext({kill={cpu=1000}}, table.remove, mk("x", 100))
   127      print(ctx)
   128      --> =done
   129      print(ctx.used.cpu >= 100)
   130      --> =true
   131  
   132      local ctx2 = runtime.callcontext({kill={cpu=1000}}, table.remove, mk("x", 1000))
   133      print(ctx2)
   134      --> =done
   135  
   136      print(ctx2.used.cpu / ctx1.used.cpu < 1.2)
   137      --> =true
   138  end
   139  
   140  -- table.sort
   141  do
   142      -- table.sort consumes cpu
   143      local function unsorted(n)
   144          t = {}
   145          for i = 1, n do
   146              t[i] = n - i
   147          end
   148          return t
   149      end
   150  
   151      print(runtime.callcontext({kill={cpu=1000}}, table.sort, unsorted(10)))
   152      --> =done
   153  
   154      print(runtime.callcontext({kill={cpu=1000}}, table.sort, unsorted(100)))
   155      --> =killed
   156  end
   157  
   158  --  table.unpack
   159  do
   160      -- table.unpack consumes no memory if the result is discarded. We wrap
   161      -- table.unpack in a function so as not to accumulate its results and
   162      -- consume memory in that way
   163      local ctx = runtime.callcontext({kill={memory=500}}, function(t) table.unpack(t) end, mk("x", 100))
   164      print(ctx)
   165      --> =done
   166  
   167      local ctx = runtime.callcontext({kill={memory=500}}, function(t) table.unpack(t) end, mk("x", 200))
   168      print(ctx)
   169      --> =done
   170  
   171      --table.unpack consumes cpu
   172      local ctx = runtime.callcontext({kill={cpu=200}}, table.unpack, mk("x", 100))
   173      print(ctx)
   174      --> =done
   175  
   176      print(runtime.callcontext({kill={cpu=200}}, table.unpack, mk("x", 200)))
   177      --> =killed
   178  end