diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-06-04 11:34:44 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-06-04 11:34:44 +0200 |
commit | 011593650caa66457694fc46352208b8607d8994 (patch) | |
tree | a642b8ff0fb2aed6df84867e48298c578581bded /src | |
parent | fc2fde434b73e9189dfa6b0c160e5fe72a7aee15 (diff) | |
download | lanes-011593650caa66457694fc46352208b8607d8994.tar.gz lanes-011593650caa66457694fc46352208b8607d8994.tar.bz2 lanes-011593650caa66457694fc46352208b8607d8994.zip |
Keeper code internal improvements
Diffstat (limited to 'src')
-rw-r--r-- | src/keeper.cpp | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/src/keeper.cpp b/src/keeper.cpp index 915d6b6..eba184f 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp | |||
@@ -79,7 +79,7 @@ class KeyUD | |||
79 | 79 | ||
80 | [[nodiscard]] static KeyUD* GetPtr(KeeperState K_, int idx_); | 80 | [[nodiscard]] static KeyUD* GetPtr(KeeperState K_, int idx_); |
81 | [[nodiscard]] static KeyUD* Create(KeeperState K_); | 81 | [[nodiscard]] static KeyUD* Create(KeeperState K_); |
82 | [[nodiscard]] static KeyUD* PrepareAccess(KeeperState K_, int idx_); | 82 | void prepareAccess(KeeperState K_); |
83 | void peek(KeeperState K_, int count_); | 83 | void peek(KeeperState K_, int count_); |
84 | void pop(KeeperState K_, int count_); | 84 | void pop(KeeperState K_, int count_); |
85 | void push(KeeperState K_, int count_); | 85 | void push(KeeperState K_, int count_); |
@@ -111,19 +111,14 @@ KeyUD* KeyUD::GetPtr(KeeperState const K_, int idx_) | |||
111 | 111 | ||
112 | // ################################################################################################# | 112 | // ################################################################################################# |
113 | 113 | ||
114 | // replaces the KeyUD by its uservalue on the stack | 114 | // expects 'this' on top of the stack |
115 | // TODO: make it a non-static member to be called after KeyUD::GetPtr() | 115 | // replaces it by its uservalue on the stack |
116 | KeyUD* KeyUD::PrepareAccess(KeeperState const K_, int const idx_) | 116 | void KeyUD::prepareAccess(KeeperState const K_) |
117 | { | 117 | { |
118 | KeyUD* const _key{ KeyUD::GetPtr(K_, idx_) }; | 118 | LUA_ASSERT(K_, KeyUD::GetPtr(K_, -1) == this); |
119 | if (_key) { | 119 | // we can replace the key userdata in the stack without fear of it being GCed, there are other references around |
120 | int const _idx{ lua_absindex(K_, idx_) }; | 120 | lua_getiuservalue(K_, -1, kContentsTableIndex); |
121 | STACK_GROW(K_, 1); | 121 | lua_replace(K_, -2); |
122 | // we can replace the key userdata in the stack without fear of it being GCed, there are other references around | ||
123 | lua_getiuservalue(K_, _idx, kContentsTableIndex); | ||
124 | lua_replace(K_, _idx); | ||
125 | } | ||
126 | return _key; | ||
127 | } | 122 | } |
128 | 123 | ||
129 | // ################################################################################################# | 124 | // ################################################################################################# |
@@ -266,7 +261,8 @@ int keeper_push_linda_storage(Linda& linda_, DestState L_) | |||
266 | InterCopyContext _c{ linda_.U, L_, SourceState{ _K }, {}, {}, {}, LookupMode::FromKeeper, {} }; | 261 | InterCopyContext _c{ linda_.U, L_, SourceState{ _K }, {}, {}, {}, LookupMode::FromKeeper, {} }; |
267 | lua_pushnil(_K); // _K: KeysDB nil L_: out | 262 | lua_pushnil(_K); // _K: KeysDB nil L_: out |
268 | while (lua_next(_K, -2)) { // _K: KeysDB key KeyUD L_: out | 263 | while (lua_next(_K, -2)) { // _K: KeysDB key KeyUD L_: out |
269 | KeyUD* const _key{ KeyUD::PrepareAccess(_K, -1) }; // _K: KeysDB key fifo L_: out | 264 | KeyUD* const _key{ KeyUD::GetPtr(_K, -1) }; |
265 | _key->prepareAccess(_K); // _K: KeysDB key fifo L_: out | ||
270 | lua_pushvalue(_K, -2); // _K: KeysDB key fifo key L_: out | 266 | lua_pushvalue(_K, -2); // _K: KeysDB key fifo key L_: out |
271 | std::ignore = _c.inter_move(1); // _K: KeysDB key fifo L_: out key | 267 | std::ignore = _c.inter_move(1); // _K: KeysDB key fifo L_: out key |
272 | STACK_CHECK(L_, 2); | 268 | STACK_CHECK(L_, 2); |
@@ -340,14 +336,14 @@ int keepercall_send(lua_State* const L_) | |||
340 | } | 336 | } |
341 | lua_remove(_K, -2); // _K: linda key args... KeyUD | 337 | lua_remove(_K, -2); // _K: linda key args... KeyUD |
342 | STACK_CHECK(_K, 1); | 338 | STACK_CHECK(_K, 1); |
343 | KeyUD* _key{ KeyUD::GetPtr(_K, -1) }; | 339 | KeyUD* const _key{ KeyUD::GetPtr(_K, -1) }; |
344 | if (_key->limit >= 0 && _key->count + _n > _key->limit) { // not enough room? | 340 | if (_key->limit >= 0 && _key->count + _n > _key->limit) { // not enough room? |
345 | // don't send anything | 341 | // don't send anything |
346 | lua_settop(_K, 0); // _K: | 342 | lua_settop(_K, 0); // _K: |
347 | lua_pushboolean(_K, 0); // _K: false | 343 | lua_pushboolean(_K, 0); // _K: false |
348 | } else { | 344 | } else { |
349 | // _key should remain unchanged | 345 | // _key should remain unchanged |
350 | _key = KeyUD::PrepareAccess(_K, -1); // _K: linda key args... fifo | 346 | _key->prepareAccess(_K); // _K: linda key args... fifo |
351 | lua_replace(_K, 2); // _K: linda fifo args... | 347 | lua_replace(_K, 2); // _K: linda fifo args... |
352 | _key->push(_K, _n); // _K: linda fifo | 348 | _key->push(_K, _n); // _K: linda fifo |
353 | lua_settop(_K, 0); // _K: | 349 | lua_settop(_K, 0); // _K: |
@@ -370,8 +366,9 @@ int keepercall_receive(lua_State* const L_) | |||
370 | for (int const _i : std::ranges::iota_view{ 2, _top + 1 }) { | 366 | for (int const _i : std::ranges::iota_view{ 2, _top + 1 }) { |
371 | lua_pushvalue(_K, _i); // _K: KeysDB keys... key[i] | 367 | lua_pushvalue(_K, _i); // _K: KeysDB keys... key[i] |
372 | lua_rawget(_K, 1); // _K: KeysDB keys... KeyUD | 368 | lua_rawget(_K, 1); // _K: KeysDB keys... KeyUD |
373 | KeyUD* const _key{ KeyUD::PrepareAccess(_K, -1) }; // _K: KeysDB keys... fifo | 369 | KeyUD* const _key{ KeyUD::GetPtr(_K, -1) }; |
374 | if (_key != nullptr && _key->count > 0) { | 370 | if (_key != nullptr && _key->count > 0) { |
371 | _key->prepareAccess(_K); // _K: KeysDB keys... fifo | ||
375 | _key->pop(_K, 1); // _K: KeysDB keys... val | 372 | _key->pop(_K, 1); // _K: KeysDB keys... val |
376 | if (!lua_isnil(_K, -1)) { | 373 | if (!lua_isnil(_K, -1)) { |
377 | lua_replace(_K, 1); // _K: val keys... | 374 | lua_replace(_K, 1); // _K: val keys... |
@@ -406,8 +403,9 @@ int keepercall_receive_batched(lua_State* const L_) | |||
406 | lua_pushvalue(_K, 1); // _K: key KeysDB key | 403 | lua_pushvalue(_K, 1); // _K: key KeysDB key |
407 | lua_rawget(_K, 2); // _K: key KeysDB KeyUD | 404 | lua_rawget(_K, 2); // _K: key KeysDB KeyUD |
408 | lua_remove(_K, 2); // _K: key KeyUD | 405 | lua_remove(_K, 2); // _K: key KeyUD |
409 | KeyUD* const _key{ KeyUD::PrepareAccess(_K, 2) }; // _K: key fifo | 406 | KeyUD* const _key{ KeyUD::GetPtr(_K, -1) }; |
410 | if (_key != nullptr && _key->count >= _min_count) { | 407 | if (_key != nullptr && _key->count >= _min_count) { |
408 | _key->prepareAccess(_K); // _K: key fifo | ||
411 | _key->pop(_K, std::min(_max_count, _key->count)); // _K: key val... | 409 | _key->pop(_K, std::min(_max_count, _key->count)); // _K: key val... |
412 | } else { | 410 | } else { |
413 | lua_settop(_K, 0); // _K: | 411 | lua_settop(_K, 0); // _K: |
@@ -435,13 +433,14 @@ int keepercall_limit(lua_State* const L_) | |||
435 | KeyUD* _key{ KeyUD::GetPtr(_K, -1) }; | 433 | KeyUD* _key{ KeyUD::GetPtr(_K, -1) }; |
436 | if (_key == nullptr) { // _K: KeysDB key nil | 434 | if (_key == nullptr) { // _K: KeysDB key nil |
437 | lua_pop(_K, 1); // _K: KeysDB key | 435 | lua_pop(_K, 1); // _K: KeysDB key |
438 | _key = KeyUD::Create(_K); // _K: KeysDB key fifo | 436 | _key = KeyUD::Create(_K); // _K: KeysDB key KeyUD |
439 | lua_rawset(_K, -3); // _K: KeysDB | 437 | lua_rawset(_K, -3); // _K: KeysDB |
440 | } | 438 | } |
441 | // remove any clutter on the stack | 439 | // remove any clutter on the stack |
442 | lua_settop(_K, 0); // _K: | 440 | lua_settop(_K, 0); // _K: |
443 | // return true if we decide that blocked threads waiting to write on that key should be awakened | 441 | // return true if we decide that blocked threads waiting to write on that key should be awakened |
444 | // this is the case if we detect the key was full but it is no longer the case | 442 | // this is the case if we detect the key was full but it is no longer the case |
443 | // TODO: make this KeyUD::changeLimit() -> was full (bool) | ||
445 | if ( | 444 | if ( |
446 | ((_key->limit >= 0) && (_key->count >= _key->limit)) // the key was full if limited and count exceeded the previous limit | 445 | ((_key->limit >= 0) && (_key->count >= _key->limit)) // the key was full if limited and count exceeded the previous limit |
447 | && ((_limit < 0) || (_key->count < _limit)) // the key is not full if unlimited or count is lower than the new limit | 446 | && ((_limit < 0) || (_key->count < _limit)) // the key is not full if unlimited or count is lower than the new limit |
@@ -502,8 +501,8 @@ int keepercall_set(lua_State* const L_) | |||
502 | // we create room if the KeyUD was full but we didn't refill it to the brim with new data | 501 | // we create room if the KeyUD was full but we didn't refill it to the brim with new data |
503 | _should_wake_writers = _key->reset(_K) && (_count < _key->limit); | 502 | _should_wake_writers = _key->reset(_K) && (_count < _key->limit); |
504 | } | 503 | } |
505 | std::ignore = KeyUD::PrepareAccess(_K, -1); // _K: KeysDB key val... fifo | 504 | _key->prepareAccess(_K); // _K: KeysDB key val... fifo |
506 | // move the fifo below the values we want to store | 505 | // move the fifo below the values we want to store. can be slow if there are a lot. TODO: optimize that if possible |
507 | lua_insert(_K, 3); // _K: KeysDB key fifo val... | 506 | lua_insert(_K, 3); // _K: KeysDB key fifo val... |
508 | _key->push(_K, _count); // _K: KeysDB key fifo | 507 | _key->push(_K, _count); // _K: KeysDB key fifo |
509 | } | 508 | } |
@@ -522,11 +521,12 @@ int keepercall_get(lua_State* const L_) | |||
522 | _count = static_cast<int>(lua_tointeger(_K, 3)); | 521 | _count = static_cast<int>(lua_tointeger(_K, 3)); |
523 | lua_pop(_K, 1); // _K: linda key | 522 | lua_pop(_K, 1); // _K: linda key |
524 | } | 523 | } |
525 | PushKeysDB(_K, 1); // _K: linda key KeyUD | 524 | PushKeysDB(_K, 1); // _K: linda key KeysDB |
526 | lua_replace(_K, 1); // _K: KeyUD key | 525 | lua_replace(_K, 1); // _K: KeysDB key |
527 | lua_rawget(_K, 1); // _K: KeyUD fifo | 526 | lua_rawget(_K, 1); // _K: KeysDB KeyUD |
528 | KeyUD* const _key{ KeyUD::PrepareAccess(_K, -1) }; // _K: KeyUD fifo | 527 | KeyUD* const _key{ KeyUD::GetPtr(_K, -1) }; |
529 | if (_key != nullptr && _key->count > 0) { | 528 | if (_key != nullptr && _key->count > 0) { |
529 | _key->prepareAccess(_K); // _K: KeysDB fifo | ||
530 | lua_remove(_K, 1); // _K: fifo | 530 | lua_remove(_K, 1); // _K: fifo |
531 | _count = std::min(_count, _key->count); | 531 | _count = std::min(_count, _key->count); |
532 | // read <count> value off the fifo | 532 | // read <count> value off the fifo |