cache-internal.h 3.0 KB

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