|
| 1 | +local evo = require 'evolved' |
| 2 | +local basics = require 'develop.basics' |
| 3 | + |
| 4 | +evo.debug_mode(false) |
| 5 | + |
| 6 | +local N = 100000 |
| 7 | +local Ps = { 0, 10 } |
| 8 | + |
| 9 | +local f0 = evo.builder() |
| 10 | + :default(0) |
| 11 | + :build() |
| 12 | + |
| 13 | +local f1 = evo.builder() |
| 14 | + :default(0) |
| 15 | + :build() |
| 16 | + |
| 17 | +local f2 = evo.builder() |
| 18 | + :default(0) |
| 19 | + :build() |
| 20 | + |
| 21 | +local f3 = evo.builder() |
| 22 | + :default(0) |
| 23 | + :build() |
| 24 | + |
| 25 | +local ffi_ok, ffi = pcall(require, 'ffi') |
| 26 | + |
| 27 | +if ffi_ok then |
| 28 | + local FFI_DOUBLE_TYPEOF = ffi.typeof('double') |
| 29 | + local FFI_DOUBLE_SIZEOF = ffi.sizeof(FFI_DOUBLE_TYPEOF) |
| 30 | + local FFI_DOUBLE_STORAGE_TYPEOF = ffi.typeof('double[?]') |
| 31 | + |
| 32 | + ---@param src ffi.cdata*? |
| 33 | + ---@param src_size integer |
| 34 | + ---@param dst_size integer |
| 35 | + ---@return ffi.cdata*? |
| 36 | + local function FFI_DOUBLE_STORAGE_REALLOC(src, src_size, dst_size) |
| 37 | + if dst_size == 0 then |
| 38 | + -- freeing the src storage, just let the GC handle it |
| 39 | + return |
| 40 | + end |
| 41 | + |
| 42 | + -- to support 1-based indexing, allocate one extra element |
| 43 | + local dst = ffi.new(FFI_DOUBLE_STORAGE_TYPEOF, dst_size + 1) |
| 44 | + |
| 45 | + if src and src_size > 0 then |
| 46 | + -- handle both expanding and shrinking |
| 47 | + local min_size = math.min(src_size, dst_size) |
| 48 | + ffi.copy(dst + 1, src + 1, min_size * FFI_DOUBLE_SIZEOF) |
| 49 | + end |
| 50 | + |
| 51 | + return dst |
| 52 | + end |
| 53 | + |
| 54 | + ---@param src ffi.cdata* |
| 55 | + ---@param f integer |
| 56 | + ---@param e integer |
| 57 | + ---@param t integer |
| 58 | + ---@param dst ffi.cdata* |
| 59 | + local function FFI_DOUBLE_STORAGE_COMPMOVE(src, f, e, t, dst) |
| 60 | + ffi.copy(dst + t, src + f, (e - f + 1) * FFI_DOUBLE_SIZEOF) |
| 61 | + end |
| 62 | + |
| 63 | + evo.set(f0, evo.REALLOC, FFI_DOUBLE_STORAGE_REALLOC) |
| 64 | + evo.set(f0, evo.COMPMOVE, FFI_DOUBLE_STORAGE_COMPMOVE) |
| 65 | + |
| 66 | + evo.set(f1, evo.REALLOC, FFI_DOUBLE_STORAGE_REALLOC) |
| 67 | + evo.set(f1, evo.COMPMOVE, FFI_DOUBLE_STORAGE_COMPMOVE) |
| 68 | + |
| 69 | + evo.set(f2, evo.REALLOC, FFI_DOUBLE_STORAGE_REALLOC) |
| 70 | + evo.set(f2, evo.COMPMOVE, FFI_DOUBLE_STORAGE_COMPMOVE) |
| 71 | + |
| 72 | + evo.set(f3, evo.REALLOC, FFI_DOUBLE_STORAGE_REALLOC) |
| 73 | + evo.set(f3, evo.COMPMOVE, FFI_DOUBLE_STORAGE_COMPMOVE) |
| 74 | +end |
| 75 | + |
| 76 | +print '----------------------------------------' |
| 77 | + |
| 78 | +for _, P in ipairs(Ps) do |
| 79 | + basics.describe_bench(string.format('Other Benchmarks: SystemWith1Component, %d padding | %d entities', P, N), |
| 80 | + function(w) |
| 81 | + evo.process(w) |
| 82 | + end, function() |
| 83 | + local w = evo.builder() |
| 84 | + :set(evo.DESTRUCTION_POLICY, evo.DESTRUCTION_POLICY_DESTROY_ENTITY) |
| 85 | + :build() |
| 86 | + |
| 87 | + local count = 0 |
| 88 | + |
| 89 | + while count < N do |
| 90 | + evo.spawn({ [w] = true, [f1] = 0, }) |
| 91 | + count = count + 1 |
| 92 | + |
| 93 | + for _ = 0, P - 1 do |
| 94 | + evo.spawn({ [w] = true, [f0] = 0, }) |
| 95 | + count = count + 1 |
| 96 | + if count >= N then break end |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + evo.builder():set(w):group(w):include(f1) |
| 101 | + :execute(function(chunk, _, entity_count) |
| 102 | + local f1s = chunk:components(f1) |
| 103 | + |
| 104 | + for i = 1, entity_count do |
| 105 | + f1s[i] = f1s[i] + 1 |
| 106 | + end |
| 107 | + end):build() |
| 108 | + |
| 109 | + return w |
| 110 | + end, function(w) |
| 111 | + evo.destroy(w) |
| 112 | + end) |
| 113 | +end |
| 114 | + |
| 115 | +print '----------------------------------------' |
| 116 | + |
| 117 | +for _, P in ipairs(Ps) do |
| 118 | + basics.describe_bench(string.format('Other Benchmarks: SystemWith2Component, %d padding | %d entities', P, N), |
| 119 | + function(w) |
| 120 | + evo.process(w) |
| 121 | + end, function() |
| 122 | + local w = evo.builder() |
| 123 | + :set(evo.DESTRUCTION_POLICY, evo.DESTRUCTION_POLICY_DESTROY_ENTITY) |
| 124 | + :build() |
| 125 | + |
| 126 | + local count = 0 |
| 127 | + |
| 128 | + while count < N do |
| 129 | + evo.spawn({ [w] = true, [f1] = 0, [f2] = 1, }) |
| 130 | + count = count + 1 |
| 131 | + |
| 132 | + for j = 0, P - 1 do |
| 133 | + local m = j % 2 |
| 134 | + if m == 0 then |
| 135 | + evo.spawn({ [w] = true, [f1] = 0, }) |
| 136 | + elseif m == 1 then |
| 137 | + evo.spawn({ [w] = true, [f2] = 0, }) |
| 138 | + else |
| 139 | + assert(false) |
| 140 | + end |
| 141 | + count = count + 1 |
| 142 | + if count >= N then break end |
| 143 | + end |
| 144 | + end |
| 145 | + |
| 146 | + evo.builder():set(w):group(w):include(f1, f2) |
| 147 | + :execute(function(chunk, _, entity_count) |
| 148 | + local f1s, f2s = chunk:components(f1, f2) |
| 149 | + |
| 150 | + for i = 1, entity_count do |
| 151 | + f1s[i] = f1s[i] + f2s[i] |
| 152 | + end |
| 153 | + end):build() |
| 154 | + |
| 155 | + return w |
| 156 | + end, function(w) |
| 157 | + evo.destroy(w) |
| 158 | + end) |
| 159 | +end |
| 160 | + |
| 161 | +print '----------------------------------------' |
| 162 | + |
| 163 | +for _, P in ipairs(Ps) do |
| 164 | + basics.describe_bench(string.format('Other Benchmarks: SystemWith3Component, %d padding | %d entities', P, N), |
| 165 | + function(w) |
| 166 | + evo.process(w) |
| 167 | + end, function() |
| 168 | + local w = evo.builder() |
| 169 | + :set(evo.DESTRUCTION_POLICY, evo.DESTRUCTION_POLICY_DESTROY_ENTITY) |
| 170 | + :build() |
| 171 | + |
| 172 | + local count = 0 |
| 173 | + |
| 174 | + while count < N do |
| 175 | + evo.spawn({ [w] = true, [f1] = 0, [f2] = 1, [f3] = 1, }) |
| 176 | + count = count + 1 |
| 177 | + |
| 178 | + for j = 0, P - 1 do |
| 179 | + local m = j % 3 |
| 180 | + if m == 0 then |
| 181 | + evo.spawn({ [w] = true, [f1] = 0, }) |
| 182 | + elseif m == 1 then |
| 183 | + evo.spawn({ [w] = true, [f2] = 0, }) |
| 184 | + elseif m == 2 then |
| 185 | + evo.spawn({ [w] = true, [f3] = 0, }) |
| 186 | + else |
| 187 | + assert(false) |
| 188 | + end |
| 189 | + count = count + 1 |
| 190 | + if count >= N then break end |
| 191 | + end |
| 192 | + end |
| 193 | + |
| 194 | + evo.builder():set(w):group(w):include(f1, f2, f3) |
| 195 | + :execute(function(chunk, _, entity_count) |
| 196 | + local f1s, f2s, f3s = chunk:components(f1, f2, f3) |
| 197 | + |
| 198 | + for i = 1, entity_count do |
| 199 | + f1s[i] = f1s[i] + f2s[i] + f3s[i] |
| 200 | + end |
| 201 | + end):build() |
| 202 | + |
| 203 | + return w |
| 204 | + end, function(w) |
| 205 | + evo.destroy(w) |
| 206 | + end) |
| 207 | +end |
0 commit comments