cache-internal.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #ifndef SCM_CACHE_INTERNAL_H
  2. #define SCM_CACHE_INTERNAL_H
  3. /* Copyright 2016,2018
  4. Free Software Foundation, Inc.
  5. This file is part of Guile.
  6. Guile is free software: you can redistribute it and/or modify it
  7. under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Guile is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with Guile. If not, see
  16. <https://www.gnu.org/licenses/>. */
  17. #include <string.h>
  18. #include "libguile/gc.h"
  19. #include "libguile/hash.h"
  20. /* A simple cache with 8 entries. The cache entries are stored in a
  21. sorted vector. */
  22. struct scm_cache_entry
  23. {
  24. scm_t_bits key;
  25. scm_t_bits value;
  26. };
  27. #define SCM_CACHE_SIZE 16
  28. struct scm_cache
  29. {
  30. scm_t_bits eviction_cookie;
  31. struct scm_cache_entry entries[SCM_CACHE_SIZE];
  32. };
  33. static inline struct scm_cache*
  34. scm_make_cache (void)
  35. {
  36. struct scm_cache *ret = scm_gc_typed_calloc (struct scm_cache);
  37. ret->eviction_cookie = (scm_t_bits) ret;
  38. return ret;
  39. }
  40. static inline int
  41. scm_cache_full_p (struct scm_cache *cache)
  42. {
  43. return cache->entries[0].key != 0;
  44. }
  45. static inline void
  46. scm_cache_evict_1 (struct scm_cache *cache, struct scm_cache_entry *evicted)
  47. {
  48. size_t idx;
  49. cache->eviction_cookie = scm_ihashq (SCM_PACK (cache->eviction_cookie), -1);
  50. idx = cache->eviction_cookie & (SCM_CACHE_SIZE - 1);
  51. memcpy (evicted, cache->entries + idx, sizeof (*evicted));
  52. memmove (cache->entries + 1,
  53. cache->entries,
  54. sizeof (cache->entries[0]) * idx);
  55. cache->entries[0].key = 0;
  56. cache->entries[0].value = 0;
  57. }
  58. static inline struct scm_cache_entry*
  59. scm_cache_lookup (struct scm_cache *cache, SCM k)
  60. {
  61. scm_t_bits k_bits = SCM_UNPACK (k);
  62. struct scm_cache_entry *entry = cache->entries;
  63. /* Unrolled binary search, compiled to branchless cmp + cmov chain. */
  64. if (entry[8].key <= k_bits) entry += 8;
  65. if (entry[4].key <= k_bits) entry += 4;
  66. if (entry[2].key <= k_bits) entry += 2;
  67. if (entry[1].key <= k_bits) entry += 1;
  68. return entry;
  69. }
  70. static inline void
  71. scm_cache_insert (struct scm_cache *cache, SCM k, SCM v,
  72. struct scm_cache_entry *evicted)
  73. {
  74. struct scm_cache_entry *entry;
  75. if (scm_cache_full_p (cache))
  76. scm_cache_evict_1 (cache, evicted);
  77. entry = scm_cache_lookup (cache, k);
  78. if (entry->key == SCM_UNPACK (k))
  79. {
  80. entry->value = SCM_UNPACK (v);
  81. return;
  82. }
  83. memmove (cache->entries,
  84. cache->entries + 1,
  85. (entry - cache->entries) * sizeof (*entry));
  86. entry->key = SCM_UNPACK (k);
  87. entry->value = SCM_UNPACK (v);
  88. }
  89. #endif /* SCM_CACHE_INTERNAL_H */