table.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. /* Definitions for the table type.
  2. This file is part of khipu.
  3. khipu is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #include "khipu.hpp"
  14. KP_DECLS_BEGIN
  15. static inline object&
  16. tabvec_cnt (array *vecp)
  17. {
  18. return (*(vecp->data - 1));
  19. }
  20. static inline object&
  21. tabvec_size (array *vecp)
  22. {
  23. return (*(vecp->data - 2));
  24. }
  25. static inline object&
  26. tabvec_pidx (array *vecp)
  27. {
  28. return (*(vecp->data - 3));
  29. }
  30. static const int TABVEC_EXTRA = 3;
  31. template <typename T>
  32. static constexpr inline T
  33. tabvec_idx (T idx)
  34. {
  35. return (idx * 2);
  36. }
  37. static const uint32_t SECONDARY_KEYS[] = { 2, 3, 5, 7 };
  38. static const int N_SECONDARY_KEYS = KP_NELEM (SECONDARY_KEYS);
  39. static const uint32_t PRIMES[] =
  40. {
  41. 0xb, 0x25, 0x71, 0x15b, 0x419, 0xc4d, 0x24f5, 0x6ee3, 0x14cb3, 0x3e61d,
  42. 0xbb259, 0x23170f, 0x694531, 0x13bcf95, 0x3b36ec3, 0xb1a4c4b, 0x214ee4e3,
  43. 0x63ecaead
  44. };
  45. static int
  46. compute_hsize (uint32_t min_size, float mv_ratio, int *idxp)
  47. {
  48. int i1 = 0, i2 = (int)KP_NELEM (PRIMES);
  49. while (i1 < i2)
  50. {
  51. int step = (i1 + i2) >> 1;
  52. if (PRIMES[step] < min_size)
  53. i1 = step + 1;
  54. else
  55. i2 = step;
  56. }
  57. *idxp = i1;
  58. return ((int)(PRIMES[i1] * mv_ratio));
  59. }
  60. // Special values used by the table implementation.
  61. static symbol FREE_SYM;
  62. static symbol DELTV_SYM;
  63. #ifdef KP_ARCH_WIDE
  64. static const object FREE_HASH =
  65. ((object)typecode::SYMBOL << TYPE_SHIFT) | ((object)&FREE_SYM);
  66. static const object DELETED_VAL =
  67. ((object)typecode::SYMBOL << TYPE_SHIFT) | ((object)&DELTV_SYM);
  68. #else
  69. static const object FREE_HASH = ((object)&FREE_SYM) | 3;
  70. static const object DELETED_VAL = ((object)&DELTV_SYM) | 3;
  71. #endif
  72. static const object DELETED_KEY = FREE_HASH | EXTRA_BIT;
  73. #ifdef KP_HAS_ATOMIC_CASX
  74. static const size_t TABVEC_EXTRA_ROOM = TABVEC_EXTRA + 1;
  75. #else
  76. static const size_t TABVEC_EXTRA_ROOM = TABVEC_EXTRA;
  77. #endif
  78. static array*
  79. make_tabvec (interpreter *interp, int prime_idx)
  80. {
  81. uint32_t size = PRIMES[prime_idx],
  82. tsize = tabvec_idx (size) + TABVEC_EXTRA_ROOM;
  83. array *ret = array::alloc_raw (tsize);
  84. for (uint32_t i = 0; i < ret->len; ++i)
  85. ret->data[i] = FREE_HASH;
  86. ret->data += TABVEC_EXTRA, ret->len -= TABVEC_EXTRA;
  87. #ifdef KP_HAS_ATOMIC_CASX
  88. if ((uintptr_t)ret->data % 16)
  89. ++ret->data;
  90. --ret->len;
  91. #endif
  92. tabvec_size(ret) = size;
  93. tabvec_cnt(ret) = 0;
  94. tabvec_pidx(ret) = prime_idx;
  95. return (ret);
  96. }
  97. static inline object
  98. register_tabvec (interpreter *interp, array *vecp)
  99. {
  100. interp->alval = vecp->as_obj ();
  101. gc_register (interp, vecp, sizeof (*vecp) +
  102. (vecp->len + TABVEC_EXTRA_ROOM) * sizeof (object));
  103. return (interp->alval);
  104. }
  105. static result<object>
  106. alloc_empty_table (interpreter *interp, object tst, object hashfn)
  107. {
  108. auto eg = KP_TRY (evh_guard::make (interp));
  109. table *ret = alloch<table> ();
  110. lwlock_init (&ret->lock);
  111. ret->vector = deref (alloc_array (interp, 0));
  112. ret->cmpfct = tst == NIL ? fixint (0) : tst;
  113. ret->hashfct = hashfn == NIL ? fixint (0) : hashfn;
  114. ret->grow_limit = 0;
  115. ret->mv_ratio = 0.85f;
  116. interp->alval = ret->as_obj ();
  117. gc_register (interp, ret);
  118. return (interp->alval);
  119. }
  120. static inline result<bool>
  121. table_equal (interpreter *interp, const table *tp,
  122. object k1, object k2)
  123. {
  124. if (k1 == DELETED_KEY)
  125. return (false);
  126. // We only need to save K1, since K2 is the caller-provided key.
  127. valref tmp (interp, k1);
  128. if (kp_likely (tp->cmpfct == fixint (0)))
  129. return (equal (interp, k1, k2));
  130. KP_VTRY (interp->growstk (3));
  131. *interp->stkend++ = tp->cmpfct;
  132. *interp->stkend++ = k1;
  133. *interp->stkend++ = k2;
  134. KP_VTRY (call_n (interp, 2));
  135. return (interp->retval != NIL);
  136. }
  137. static inline result<uint32_t>
  138. table_hash (interpreter *interp, const table *tp, object key)
  139. {
  140. if (kp_likely (tp->hashfct == fixint (0)))
  141. return (xhash (interp, key));
  142. KP_VTRY (interp->growstk (2));
  143. *interp->stkend++ = tp->hashfct;
  144. *interp->stkend++ = key;
  145. KP_VTRY (call_n (interp, 1));
  146. int ret;
  147. if (as<int> (interp->retval, ret))
  148. return ((uint32_t)ret);
  149. else if (as<bigint> (interp->retval))
  150. return (hash_I (interp, interp->retval));
  151. return (interp->raise ("type-error", "hash function must return an integer"));
  152. }
  153. #ifndef KP_HAS_ATOMIC_CASX
  154. // Try to atomically set the key in the table vector.
  155. static inline bool
  156. setk_cond (array *vecp, int ix, atomic_t ex, atomic_t nv)
  157. {
  158. return (atomic_cas_bool ((atomic_t *)&vecp->data[ix + 0], ex, nv));
  159. }
  160. #endif
  161. // Try to atomically set the value in the table vector.
  162. static inline bool
  163. setv_cond (array *vecp, int ix, atomic_t ex, atomic_t nv)
  164. {
  165. return (atomic_cas_bool ((atomic_t *)&vecp->data[ix + 1], ex, nv));
  166. }
  167. static result<int>
  168. table_probe (interpreter *interp, const table *tp,
  169. array *vecp, object key, bool put_p, bool& empty)
  170. {
  171. uint32_t hashcode = KP_TRY (table_hash (interp, tp, key));
  172. int32_t entries = (int32_t)tabvec_size (vecp);
  173. int32_t idx = (int32_t)(hashcode % entries);
  174. int32_t vidx = tabvec_idx (idx);
  175. empty = false;
  176. object tmp = vecp->data[vidx];
  177. if (tmp == FREE_HASH)
  178. return (put_p ? (empty = true, vidx) : -1);
  179. else
  180. {
  181. bool eq = KP_TRY (table_equal (interp, tp, tmp, key));
  182. if (eq)
  183. return (vidx);
  184. }
  185. int32_t initial_idx = idx;
  186. uint32_t sec = SECONDARY_KEYS[hashcode % N_SECONDARY_KEYS];
  187. while (true)
  188. {
  189. if ((idx += sec) >= entries)
  190. idx -= entries;
  191. if (!put_p && idx == initial_idx)
  192. return (-1);
  193. vidx = tabvec_idx (idx);
  194. tmp = vecp->data[vidx];
  195. if (tmp == FREE_HASH)
  196. return (put_p ? (empty = true, vidx) : -1);
  197. bool eq = KP_TRY (table_equal (interp, tp, tmp, key));
  198. if (eq)
  199. return (vidx);
  200. }
  201. }
  202. static result<int>
  203. table_probe (interpreter *interp, const table *tp,
  204. array *vecp, object key, bool put_p)
  205. {
  206. bool dummy;
  207. return (table_probe (interp, tp, vecp, key, put_p, dummy));
  208. }
  209. static inline bool
  210. valid_key_p (object key)
  211. {
  212. return ((key & ~EXTRA_BIT) != FREE_HASH);
  213. }
  214. static result<int>
  215. growtab_probe (interpreter *interp, const table *tp,
  216. array *vecp, object key)
  217. {
  218. /* Same as above, only this function is called when migrating, which means
  219. * it cannot return failure. */
  220. uint32_t hashcode = KP_TRY (table_hash (interp, tp, key));
  221. int32_t entries = (int32_t)tabvec_size (vecp);
  222. int32_t idx = (int32_t)(hashcode % entries);
  223. int32_t vidx = tabvec_idx (idx);
  224. if (vecp->data[vidx] == FREE_HASH)
  225. return (vidx);
  226. for (uint32_t sec = SECONDARY_KEYS[hashcode % N_SECONDARY_KEYS] ; ; )
  227. {
  228. if ((idx += sec) >= entries)
  229. idx -= entries;
  230. vidx = tabvec_idx (idx);
  231. if (vecp->data[vidx] == FREE_HASH)
  232. return (vidx);
  233. }
  234. }
  235. static result<void>
  236. table_migrate_lk (interpreter *interp, table *tp)
  237. {
  238. array *oldvp = as_array (tp->vector);
  239. array *newvp = make_tabvec (interp, tabvec_pidx (oldvp) + 1);
  240. for (uint32_t i = tabvec_idx (0); i < oldvp->len; i += 2)
  241. {
  242. if (!valid_key_p (oldvp->data[i]))
  243. continue;
  244. int new_idx = KP_TRY (growtab_probe (interp, tp, newvp, oldvp->data[i]));
  245. newvp->data[new_idx + 0] = oldvp->data[i + 0];
  246. newvp->data[new_idx + 1] = oldvp->data[i + 1];
  247. }
  248. tp->grow_limit = (atomic_t)(tp->mv_ratio *
  249. tabvec_size (newvp) - tabvec_cnt (oldvp));
  250. tabvec_cnt(newvp) = tabvec_cnt (oldvp);
  251. tp->vector = register_tabvec (interp, newvp);
  252. return (0);
  253. }
  254. struct table_guard : public lwlock_guard
  255. {
  256. array *oldp;
  257. table_guard () : lwlock_guard (), oldp (nullptr)
  258. {
  259. }
  260. static result<table_guard>
  261. make (interpreter *interp, atomic_t *ptr)
  262. {
  263. table_guard ret;
  264. KP_VTRY (ret.set (interp, ptr));
  265. return (ret);
  266. }
  267. void set_vec (array *ap)
  268. {
  269. this->oldp = ap;
  270. }
  271. ~table_guard ()
  272. {
  273. if (!this->oldp)
  274. return;
  275. for (uint32_t i = tabvec_idx (0); i < this->oldp->len; i += 2)
  276. atomic_and ((atomic_t *)&oldp->data[i + 1], ~EXTRA_BIT);
  277. }
  278. };
  279. static result<void>
  280. table_migrate_mt (interpreter *interp, table *tp)
  281. {
  282. auto g = KP_TRY (table_guard::make (interp, &tp->lock));
  283. if (tp->grow_limit > 0)
  284. return (0);
  285. array *oldvp = as_array (tp->vector);
  286. array *newvp = make_tabvec (interp, tabvec_pidx (oldvp) + 1);
  287. int nelem = 0;
  288. g.set_vec (oldvp);
  289. for (uint32_t i = tabvec_idx (0); i < oldvp->len; i += 2)
  290. {
  291. object key = oldvp->data[i];
  292. object val = atomic_or ((atomic_t *)&oldvp->data[i + 1], EXTRA_BIT);
  293. /* No other thread can be migrating the table at this point,
  294. * so it's safe to do a simplified test here. */
  295. if (valid_key_p (key) && val != DELETED_VAL && val != FREE_HASH)
  296. {
  297. int new_idx = KP_TRY (growtab_probe (interp, tp, newvp, key));
  298. newvp->data[new_idx + 0] = key;
  299. newvp->data[new_idx + 1] = val;
  300. ++nelem;
  301. }
  302. }
  303. g.set_vec (nullptr);
  304. // Set up the new table.
  305. tabvec_cnt(newvp) = nelem;
  306. tp->grow_limit = (atomic_t)(tp->mv_ratio * tabvec_size (newvp)) - nelem;
  307. atomic_mfence_rel ();
  308. /* At this point, another thread may decrement the growth limit from
  309. * the wrong table vector. That's fine, it just means we'll have to
  310. * migrate sooner than necessary. */
  311. tp->vector = newvp->as_obj ();
  312. register_tabvec (interp, newvp);
  313. return (0);
  314. }
  315. static result<object>
  316. table_get_mt (interpreter *interp,
  317. table *tp, object key, object dfl)
  318. {
  319. array *vecp = as_array (tp->vector);
  320. int idx = KP_TRY (table_probe (interp, tp, vecp, key, false));
  321. if (idx < 0)
  322. return (dfl);
  323. object ret = vecp->data[idx + 1] & ~EXTRA_BIT;
  324. return (ret == DELETED_VAL || ret == FREE_HASH ? dfl : ret);
  325. }
  326. static result<object>
  327. table_get_lk (interpreter *interp, table *tp, object key, object dfl)
  328. {
  329. int idx = KP_TRY (table_probe (interp, tp, as_array (tp->vector),
  330. key, false));
  331. return (idx < 0 ? dfl : xaref (tp->vector, idx + 1));
  332. }
  333. result<object> table_get (interpreter *interp, object tab,
  334. object key, object dfl, bool mtsafe)
  335. {
  336. auto fn = mtsafe ? table_get_mt : table_get_lk;
  337. object ret = KP_TRY (fn (interp, as_table (tab), key, dfl));
  338. kp_return (ret);
  339. }
  340. result<object> get_u (interpreter *interp, object tab, object key, object dfl)
  341. {
  342. return (table_get (interp, tab, key,
  343. dfl == UNBOUND ? NIL : dfl, !singlethr_p ()));
  344. }
  345. static result<object>
  346. table_pop_mt (interpreter *interp, table *tp, object key, object dfl)
  347. {
  348. while (true)
  349. {
  350. array *vecp = as_array (tp->vector);
  351. int idx = KP_TRY (table_probe (interp, tp, vecp, key, false));
  352. if (idx < 0)
  353. return (dfl);
  354. else
  355. {
  356. object oldk = vecp->data[idx + 0];
  357. object oldv = vecp->data[idx + 1];
  358. if (!(oldv & EXTRA_BIT))
  359. { /* The table is not being migrated at the moment. Try to
  360. * delete the entry if it hasn't already been. */
  361. if (oldk == DELETED_KEY || oldv == FREE_HASH ||
  362. oldv == DELETED_VAL)
  363. return (dfl);
  364. else if (setv_cond (vecp, idx, oldv, DELETED_VAL))
  365. {
  366. atomic_add ((atomic_t *)&tabvec_cnt(vecp), -1);
  367. // Safe to set the key without atomic ops.
  368. vecp->data[idx] = DELETED_KEY;
  369. return (oldv);
  370. }
  371. continue;
  372. }
  373. // The table was being migrated - Retry.
  374. KP_VTRY (table_migrate_mt (interp, tp));
  375. }
  376. }
  377. }
  378. static result<object>
  379. table_pop_lk (interpreter *interp, table *tp, object key, object dfl)
  380. {
  381. int idx = KP_TRY (table_probe (interp, tp, as_array (tp->vector),
  382. key, false));
  383. if (idx < 0)
  384. return (dfl);
  385. array *vecp = as_array (tp->vector);
  386. object ret = vecp->data[idx + 1];
  387. vecp->data[idx + 0] = DELETED_KEY;
  388. vecp->data[idx + 1] = DELETED_VAL;
  389. --tabvec_cnt(vecp);
  390. return (ret);
  391. }
  392. result<object> table_pop (interpreter *interp, object tab,
  393. object key, object dfl, bool mtsafe)
  394. {
  395. table *tp = as_table (tab);
  396. if (kp_unlikely (tp->flagged_p (FLAGS_CONST)))
  397. return (interp->raise_const ());
  398. auto fn = mtsafe ? table_pop_mt : table_pop_lk;
  399. object ret = KP_TRY (fn (interp, tp, key, dfl));
  400. kp_return (ret);
  401. }
  402. result<object> npop_u (interpreter *interp, object tab, object key, object dfl)
  403. {
  404. return (table_pop (interp, tab, key, dfl, !singlethr_p ()));
  405. }
  406. template <typename Fn>
  407. static result<bool> table_update_lk (interpreter *interp, table *tp,
  408. object key, Fn& f)
  409. {
  410. bool empty;
  411. array *vecp = as_array (tp->vector);
  412. int idx = KP_TRY (table_probe (interp, tp, vecp, key, true, empty));
  413. if (empty)
  414. {
  415. vecp->data[idx + 0] = key;
  416. vecp->data[idx + 1] = KP_TRY (f.call (interp));
  417. if (--tp->grow_limit <= 0)
  418. {
  419. KP_VTRY (table_migrate_lk (interp, tp));
  420. vecp = as_array (tp->vector);
  421. }
  422. ++tabvec_cnt(vecp);
  423. }
  424. else
  425. vecp->data[idx + 1] = KP_TRY (f.call (interp, vecp->data[idx + 1]));
  426. return (empty);
  427. }
  428. struct table_inserter
  429. {
  430. object val;
  431. object call (interpreter *)
  432. {
  433. return (this->val);
  434. }
  435. object call (interpreter *, object)
  436. {
  437. return (this->val);
  438. }
  439. };
  440. template <typename Fn>
  441. static result<bool> table_update_mt (interpreter *interp,
  442. table *tp, object key, Fn& f)
  443. {
  444. while (true)
  445. {
  446. bool empty;
  447. array *vecp = as_array (tp->vector);
  448. int idx = KP_TRY (table_probe (interp, tp, vecp, key, true, empty));
  449. if (!empty)
  450. {
  451. object tmp = vecp->data[idx + 1];
  452. object nval;
  453. if ((tmp & EXTRA_BIT) != 0)
  454. ;
  455. else
  456. {
  457. if (tmp != DELETED_VAL)
  458. {
  459. nval = KP_TRY (f.call (interp, tmp));
  460. if (nval == tmp || setv_cond (vecp, idx, tmp, nval))
  461. return (empty);
  462. }
  463. continue;
  464. }
  465. }
  466. else if (tp->grow_limit > 0)
  467. { /* NOTE: If we fail here, then the growth limit will
  468. * end up too small. This simply means we may have to
  469. * migrate sooner than it's absolutely necessary, so it's
  470. * harmless. On the other hand, we must *NOT* try to
  471. * reincrement the limit back, because it risks ending
  472. * up too big, which can be harmful if, for instance, a
  473. * migration is done before the increment. */
  474. atomic_add (&tp->grow_limit, -1);
  475. object tmp = KP_TRY (f.call (interp));
  476. #ifdef KP_HAS_ATOMIC_CASX
  477. if (atomic_casx (&vecp->data[idx], FREE_HASH, FREE_HASH, key, tmp))
  478. #else
  479. if (setk_cond (vecp, idx, FREE_HASH, key) &&
  480. setv_cond (vecp, idx, FREE_HASH, tmp))
  481. #endif
  482. {
  483. atomic_add ((atomic_t *)&tabvec_cnt(vecp), 1);
  484. return (empty);
  485. }
  486. continue;
  487. }
  488. // The table needs migrating, or it was already being migrated.
  489. KP_VTRY (table_migrate_mt (interp, tp));
  490. }
  491. }
  492. result<bool> table_put (interpreter *interp, object tab,
  493. object key, object val, bool mtsafe)
  494. {
  495. table *tp = as_table (tab);
  496. if (kp_unlikely (tp->flagged_p (FLAGS_CONST)))
  497. return (interp->raise_const ());
  498. table_inserter ins;
  499. ins.val = val;
  500. auto fn = mtsafe ? table_update_mt<table_inserter> :
  501. table_update_lk<table_inserter>;
  502. bool ret = KP_TRY (fn (interp, as_table (tab), key, ins));
  503. if (ret)
  504. deref (gc_wbarrier (interp, tab, key));
  505. deref (gc_wbarrier (interp, tab, val));
  506. return (ret);
  507. }
  508. result<object> nput_u (interpreter *interp, object tab,
  509. object key, object val)
  510. {
  511. KP_TRY (table_put (interp, tab, key, val, !singlethr_p ()));
  512. kp_return (val);
  513. }
  514. static void
  515. table_clr_lk (interpreter *interp, table *tp)
  516. {
  517. array *vecp = make_tabvec (interp, 0);
  518. tp->grow_limit = (atomic_t)(tp->mv_ratio * tabvec_size (vecp));
  519. tp->vector = vecp->as_obj ();
  520. }
  521. static result<void>
  522. table_clr_mt (interpreter *interp, table *tp)
  523. {
  524. array *np = make_tabvec (interp, 0);
  525. auto g = KP_TRY (table_guard::make (interp, &tp->lock));
  526. array *vecp = as_array (tp->vector);
  527. for (uint32_t ix = tabvec_idx (0); ix < vecp->len; ix += 2)
  528. {
  529. vecp->data[ix + 1] = DELETED_VAL | EXTRA_BIT;
  530. atomic_mfence_rel ();
  531. vecp->data[ix + 0] = DELETED_KEY;
  532. }
  533. tp->grow_limit = (atomic_t)(tp->mv_ratio * tabvec_size (np));
  534. atomic_mfence_rel ();
  535. tp->vector = np->as_obj ();
  536. return (0);
  537. }
  538. result<void> table_clr (interpreter *interp, object tab, bool mtsafe)
  539. {
  540. table *tp = as_table (tab);
  541. if (kp_unlikely (tp->flagged_p (FLAGS_CONST)))
  542. return (interp->raise_const ());
  543. else if (mtsafe)
  544. KP_VTRY (table_clr_mt (interp, tp));
  545. else
  546. table_clr_lk (interp, tp);
  547. return (0);
  548. }
  549. struct table_nzapper
  550. {
  551. valref ret;
  552. object dfl;
  553. int stack_idx;
  554. int nargs;
  555. table_nzapper (interpreter *interp) : ret (interp)
  556. {
  557. }
  558. result<void> init (interpreter *interp, uint32_t flags,
  559. object fn, object *argv, int argc)
  560. {
  561. KP_VTRY (interp->growstk (argc + 1));
  562. *interp->stkend++ = fn;
  563. *interp->stkend++ = fixint (0);
  564. this->stack_idx = interp->stklen () - 1;
  565. if (flags & NZAP_DFL)
  566. {
  567. this->dfl = *argv++;
  568. --argc;
  569. }
  570. else
  571. this->dfl = NIL;
  572. for (int i = 0; i < argc; ++i)
  573. *interp->stkend++ = argv[i];
  574. this->nargs = argc + 1;
  575. return (0);
  576. }
  577. result<object> call (interpreter *interp, object prev)
  578. {
  579. interp->stack[this->stack_idx] = *this->ret = prev;
  580. KP_VTRY (call_n (interp, this->nargs));
  581. return (interp->retval);
  582. }
  583. result<object> call (interpreter *interp)
  584. {
  585. return (this->call (interp, this->dfl));
  586. }
  587. };
  588. result<object> nzap_u (interpreter *interp, object obj, object key,
  589. uint32_t flags, object fn, object *argv, int argc)
  590. {
  591. table *tp = as_table (obj);
  592. if (kp_unlikely (tp->flagged_p (FLAGS_CONST)))
  593. return (interp->raise_const ());
  594. table_nzapper nz (interp);
  595. KP_VTRY (nz.init (interp, flags, fn, argv, argc));
  596. auto fx = (flags & NZAP_NOMT) ? table_update_lk<table_nzapper> :
  597. table_update_mt<table_nzapper>;
  598. bool wb = KP_TRY (fx (interp, tp, key, nz));
  599. deref (gc_wbarrier (interp, obj, interp->retval));
  600. if (wb)
  601. deref (gc_wbarrier (interp, obj, key));
  602. if (flags & NZAP_PREV)
  603. interp->retval = *nz.ret;
  604. return (interp->retval);
  605. }
  606. uint32_t len_u (object tab)
  607. {
  608. return (as_int (tabvec_cnt (as_array (as_table(tab)->vector))));
  609. }
  610. static result<object>
  611. fill_table (interpreter *interp, table *tp, object *argv, int argc)
  612. {
  613. int idx;
  614. tp->grow_limit = compute_hsize (argc / 2 + 1, tp->mv_ratio, &idx);
  615. array *vecp = KP_TRY (make_tabvec (interp, idx));
  616. for (int i = 0; i < argc; i += 2)
  617. {
  618. object key = argv[i];
  619. idx = KP_TRY (growtab_probe (interp, tp, vecp, key));
  620. vecp->data[idx + 0] = key;
  621. vecp->data[idx + 1] = argv[i + 1];
  622. }
  623. if (argc % 2 != 0)
  624. {
  625. object key = argv[argc - 1];
  626. idx = KP_TRY (growtab_probe (interp, tp, vecp, key));
  627. vecp->data[idx + 0] = key;
  628. vecp->data[idx + 1] = NIL;
  629. ++argc;
  630. }
  631. tabvec_cnt(vecp) = fixint (argc / 2);
  632. tp->vector = register_tabvec (interp, vecp);
  633. kp_return (tp->as_obj ());
  634. }
  635. // (table eq_fn hash_fn [...args])
  636. result<object> table_fct (interpreter *interp, object *argv, int argc)
  637. {
  638. valref ret = KP_TRY (alloc_empty_table (interp, argv[0], argv[1]));
  639. return (fill_table (interp, as_table (*ret), argv + 2, argc - 2));
  640. }
  641. static inline size_t
  642. table_capacity (object vec, float mv_ratio)
  643. {
  644. return ((size_t)(as_array(vec)->len * mv_ratio));
  645. }
  646. size_t table::capacity () const
  647. {
  648. return (table_capacity (this->vector, this->mv_ratio));
  649. }
  650. table::iterator::iterator (interpreter *interp, object table) :
  651. c_key (interp, UNBOUND), c_val (interp, UNBOUND),
  652. vec (interp, as_table(table)->vector), idx (0)
  653. {
  654. ++*this;
  655. }
  656. static inline void
  657. iter_bump (const array *vecp, object& key, object& val, int& idx)
  658. {
  659. for (key = FREE_HASH; idx < (int)vecp->len; )
  660. {
  661. key = vecp->data[idx];
  662. val = vecp->data[idx + 1] & ~EXTRA_BIT;
  663. idx += 2;
  664. if (valid_key_p (key) && val != DELETED_VAL && val != FREE_HASH)
  665. return;
  666. }
  667. idx = -1;
  668. }
  669. table::iterator& table::iterator::operator++ ()
  670. {
  671. iter_bump (as_array (*this->vec), *this->c_key, *this->c_val, this->idx);
  672. return (*this);
  673. }
  674. table::iterator table::iterator::operator++ (int)
  675. {
  676. iterator ret { interpreter::self (), *this };
  677. ++*this;
  678. return (ret);
  679. }
  680. result<object> iter_u (interpreter *interp, object obj, object token, bool adv)
  681. {
  682. if (token == UNBOUND)
  683. {
  684. valref out_k (interp), out_v (interp), vec (interp, as_table(obj)->vector);
  685. int ix = 0;
  686. iter_bump (as_array (*vec), *out_k, *out_v, ix);
  687. if (!valid_key_p (*out_k))
  688. kp_return (NIL);
  689. object ret = KP_TRY (alloc_array (interp, 4));
  690. xaref(ret, 0) = *out_k;
  691. xaref(ret, 1) = *out_v;
  692. xaref(ret, 2) = *vec;
  693. xaref(ret, 3) = fixint (ix);
  694. kp_return (ret);
  695. }
  696. object vec, ival;
  697. if (!array_p (token) || len_a (token) != 4 ||
  698. !fixint_p (ival = xaref (token, 3)) || !array_p (vec = xaref (token, 2)))
  699. return (interp->raise ("arg-error", "invalid token"));
  700. else if (!adv)
  701. kp_return (xaref (token, 0));
  702. int ix = as_int (ival);
  703. object out_k, out_v;
  704. iter_bump (as_array (vec), out_k, out_v, ix);
  705. if (ix < 0)
  706. kp_return (NIL);
  707. xaref(token, 0) = out_k;
  708. xaref(token, 1) = out_v;
  709. xaref(token, 3) = fixint (ix);
  710. deref (gc_wbarrier (interp, token, out_k));
  711. deref (gc_wbarrier (interp, token, out_v));
  712. kp_return (token);
  713. }
  714. static const uint32_t TABLE_HASH_SEED = 1818386804;
  715. result<uint32_t> hash_u (interpreter *interp, object obj)
  716. {
  717. uint32_t ret = TABLE_HASH_SEED;
  718. uint32_t t1 = KP_TRY (xhash (interp, as_table(obj)->cmpfct));
  719. uint32_t t2 = KP_TRY (xhash (interp, as_table(obj)->hashfct));
  720. for (table::iterator it (interp, obj); it.valid (); ++it)
  721. {
  722. t1 = KP_TRY (xhash (interp, it.key ()));
  723. t2 = KP_TRY (xhash (interp, it.val ()));
  724. ret = mix_hash (ret, mix_hash (t1, t2));
  725. }
  726. return (ret);
  727. }
  728. result<int64_t> write_u (interpreter *interp, stream *strm,
  729. object tab, io_info& info)
  730. {
  731. table::iterator it (interp, tab);
  732. int64_t ret = 0;
  733. ret += KP_TRY (strm->putb (interp, '{'));
  734. if (it.valid ())
  735. while (true)
  736. {
  737. ret += KP_TRY (xwrite (interp, strm, it.key (), info));
  738. ret += KP_TRY (strm->putb (interp, ' '));
  739. ret += KP_TRY (xwrite (interp, strm, it.val (), info));
  740. if (!(++it).valid ())
  741. break;
  742. ret += KP_TRY (strm->putb (interp, ' '));
  743. }
  744. ret += KP_TRY (strm->putb (interp, '}'));
  745. return (ret);
  746. }
  747. result<object> copy_u (interpreter *interp, object obj, bool deep)
  748. {
  749. table::iterator it (interp, obj);
  750. sp_guard sg (interp);
  751. KP_VTRY (interp->growstk (2 + table_capacity (*it.vec,
  752. as_table(obj)->mv_ratio)));
  753. *interp->stkend++ = as_table(obj)->cmpfct;
  754. *interp->stkend++ = as_table(obj)->hashfct;
  755. if (deep)
  756. // We have to copy every key-value pair as well.
  757. for (; it.valid (); ++it)
  758. {
  759. *interp->stkend++ = KP_TRY (copy (interp, it.key (), true));
  760. *interp->stkend++ = KP_TRY (copy (interp, it.val (), true));
  761. }
  762. else
  763. for (; it.valid (); ++it)
  764. {
  765. *interp->stkend++ = it.key ();
  766. *interp->stkend++ = it.val ();
  767. }
  768. return (table_fct (interp, interp->stack + sg.sp, interp->stklen () - sg.sp));
  769. }
  770. result<int64_t> pack_u (interpreter *interp, stream *strm,
  771. object obj, pack_info& info)
  772. {
  773. pack_info::eviction_guard eg { info, true };
  774. table *tp = as_table (obj);
  775. int64_t ret = 0;
  776. ret += KP_TRY (tp->cmpfct == UNBOUND ?
  777. result<int64_t> (strm->putb (interp, PACK_NIL)) :
  778. xpack (interp, strm, tp->cmpfct, info));
  779. ret += KP_TRY (tp->hashfct == UNBOUND ?
  780. result<int64_t> (strm->putb (interp, PACK_NIL)) :
  781. xpack (interp, strm, tp->hashfct, info));
  782. ret += KP_TRY (strm->write (interp, &tp->mv_ratio));
  783. for (table::iterator it (interp, obj); it.valid (); ++it)
  784. {
  785. ret += KP_TRY (xpack (interp, strm, it.key (), info));
  786. ret += KP_TRY (xpack (interp, strm, it.val (), info));
  787. }
  788. ret += KP_TRY (strm->putb (interp, PACK_END));
  789. return (ret);
  790. }
  791. result<object> unpack_u (interpreter *interp, stream *strm,
  792. pack_info& info, bool save)
  793. {
  794. int tst = KP_TRY (strm->peekb (interp));
  795. valref e1 (interp, NIL), e2 (interp, NIL),
  796. saved_pos (interp, *info.offset);
  797. if (tst == PACK_NIL)
  798. deref (strm->getb (interp));
  799. else if (tst < 0)
  800. return (info.error ("failed to read table hasher"));
  801. else
  802. { *e1 = KP_TRY (xunpack (interp, strm, info)); }
  803. tst = KP_TRY (strm->peekb (interp));
  804. if (tst == PACK_NIL)
  805. KP_VTRY (strm->getb (interp));
  806. else if (tst < 0)
  807. return (info.error ("failed to read table tester"));
  808. else
  809. { *e2 = KP_TRY (xunpack (interp, strm, info)); }
  810. float mv_ratio;
  811. {
  812. bool rv = KP_TRY (strm->sread (interp, &mv_ratio));
  813. if (!rv)
  814. return (info.error ("failed to read table migration ratio"));
  815. }
  816. valref ret = KP_TRY (alloc_empty_table (interp, *e1, *e2));
  817. table *tp = as_table (*ret);
  818. tp->mv_ratio = mv_ratio;
  819. if (save)
  820. KP_VTRY (info.add_mapping (interp, *info.offset, *ret));
  821. sp_guard sg (interp);
  822. while (true)
  823. {
  824. tst = KP_TRY (strm->peekb (interp));
  825. if (tst == PACK_END)
  826. {
  827. deref (strm->getb (interp));
  828. break;
  829. }
  830. else
  831. {
  832. *e1 = KP_TRY (xunpack (interp, strm, info));
  833. *e2 = KP_TRY (xunpack (interp, strm, info));
  834. }
  835. KP_VTRY (interp->push (*e1),
  836. interp->push (*e2));
  837. }
  838. return (fill_table (interp, tp, interp->stack + sg.sp,
  839. interp->stklen () - sg.sp));
  840. }
  841. result<object> alloc_table (interpreter *interp, object eqfn, object hashfn)
  842. {
  843. KP_VTRY (interp->push (eqfn),
  844. interp->push (hashfn));
  845. return (table_fct (interp, interp->stkend - 2, 2));
  846. }
  847. static int
  848. do_init_tables (interpreter *interp)
  849. {
  850. static const unsigned char free_name[] = { 'f', 'r', 'e', 'e', 0 };
  851. static const unsigned char delt_name[] = { 'd', 'e', 'l', 't', 0 };
  852. static string free_str;
  853. static string delt_str;
  854. free_str.vo_type = delt_str.vo_type = typecode::STR;
  855. free_str.data = (unsigned char *)free_name;
  856. delt_str.data = (unsigned char *)delt_name;
  857. free_str.nbytes = free_str.len = KP_NELEM (free_name) - 1;
  858. delt_str.nbytes = delt_str.len = KP_NELEM (delt_name) - 1;
  859. FREE_SYM.vo_type = DELTV_SYM.vo_type = typecode::SYMBOL;
  860. FREE_SYM.name = ensure_mask(&free_str)->as_obj ();
  861. DELTV_SYM.name = ensure_mask(&delt_str)->as_obj ();
  862. FREE_SYM.pkg = DELTV_SYM.pkg = NIL;
  863. FREE_SYM.value = DELTV_SYM.value = UNBOUND;
  864. return (init_op::result_ok);
  865. }
  866. init_op init_tables (do_init_tables, "tables");
  867. KP_DECLS_END