mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-04-30 03:40:34 +02:00
There are two goals: one, to use less memory per dynamic state in order to allow millions of dynamic states to be allocated in light-weight threading scenarios. The second goal is to prevent dynamic states from being actively mutated in two threads at once. This second goal does mean that dynamic states object that escape into scheme are now copies that won't receive further updates; an incompatible change, but one which we hope doesn't affect anyone. * libguile/cache-internal.h: New file. * libguile/fluids.c (is_dynamic_state, get_dynamic_state) (save_dynamic_state, restore_dynamic_state, add_entry) (copy_value_table): New functions. (scm_i_fluid_print, scm_i_dynamic_state_print): Move up. (new_fluid): No need for a number. (scm_fluid_p: scm_is_fluid): Inline IS_FLUID uses. (fluid_set_x, fluid_ref): Adapt to dynamic state changes. (scm_fluid_set_x, scm_fluid_unset_x): Call fluid_set_x. (scm_swap_fluid): Rewrite in terms of fluid_ref and fluid_set. (swap_fluid): Use internal fluid_set_x. (scm_i_make_initial_dynamic_state): Adapt to dynamic state representation change. (scm_dynamic_state_p, scm_is_dynamic_state): Use new accessors. (scm_current_dynamic_state): Use make_dynamic_state. (scm_dynwind_current_dynamic_state): Use new accessor. * libguile/fluids.h: Remove internal definitions. Add new struct definition. * libguile/threads.h (scm_i_thread): Use scm_t_dynamic_state for dynamic state. * libguile/threads.c (guilify_self_1, guilify_self_2): (scm_i_init_thread_for_guile, scm_init_guile): (scm_call_with_new_thread): (scm_init_threads, scm_init_threads_default_dynamic_state): Adapt to scm_i_thread change. (scm_i_with_guile, with_guile): Remove "and parent" suffix. (scm_i_reset_fluid): Remove unneeded function. * doc/ref/api-scheduling.texi (Fluids and Dynamic States): Remove scm_make_dynamic_state docs. Update current-dynamic-state docs. * libguile/vm-engine.c (vm_engine): Update fluid-ref and fluid-set! inlined fast paths for dynamic state changes. * libguile/vm.c (vm_error_unbound_fluid): Remove now-unused function. * NEWS: Update. * module/ice-9/deprecated.scm (make-dynamic-state): New definition. * libguile/deprecated.h: * libguile/deprecated.c (scm_make_dynamic_state): Move here. * libguile/__scm.h (scm_t_dynamic_state): New typedef. * libguile/dynstack.h: * libguile/dynstack.c (scm_dynstack_push_fluid): (scm_dynstack_unwind_fluid): Take raw dynstate in these internal functions. * libguile/throw.c (catch): Adapt to dynstack changes.
111 lines
3 KiB
C
111 lines
3 KiB
C
#ifndef SCM_CACHE_INTERNAL_H
|
||
#define SCM_CACHE_INTERNAL_H
|
||
|
||
/* Copyright (C) 2016
|
||
* Free Software Foundation, Inc.
|
||
*
|
||
* This library is free software; you can redistribute it and/or
|
||
* modify it under the terms of the GNU Lesser General Public License
|
||
* as published by the Free Software Foundation; either version 3 of
|
||
* the License, or (at your option) any later version.
|
||
*
|
||
* This library is distributed in the hope that it will be useful, but
|
||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
* Lesser General Public License for more details.
|
||
*
|
||
* You should have received a copy of the GNU Lesser General Public
|
||
* License along with this library; if not, write to the Free Software
|
||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
||
* 02110-1301 USA
|
||
*/
|
||
|
||
|
||
|
||
|
||
#include <string.h>
|
||
|
||
#include "libguile/__scm.h"
|
||
#include "libguile/gc.h"
|
||
#include "libguile/hash.h"
|
||
#include "libguile/threads.h"
|
||
|
||
|
||
/* A simple cache with 8 entries. The cache entries are stored in a
|
||
sorted vector. */
|
||
struct scm_cache_entry
|
||
{
|
||
scm_t_bits key;
|
||
scm_t_bits value;
|
||
};
|
||
|
||
#define SCM_CACHE_SIZE 8
|
||
|
||
struct scm_cache
|
||
{
|
||
scm_t_bits eviction_cookie;
|
||
struct scm_cache_entry entries[SCM_CACHE_SIZE];
|
||
};
|
||
|
||
static inline struct scm_cache*
|
||
scm_make_cache (void)
|
||
{
|
||
struct scm_cache *ret = scm_gc_typed_calloc (struct scm_cache);
|
||
ret->eviction_cookie = (scm_t_bits) ret;
|
||
return ret;
|
||
}
|
||
|
||
static inline int
|
||
scm_cache_full_p (struct scm_cache *cache)
|
||
{
|
||
return cache->entries[0].key != 0;
|
||
}
|
||
|
||
static inline void
|
||
scm_cache_evict_1 (struct scm_cache *cache, struct scm_cache_entry *evicted)
|
||
{
|
||
size_t idx;
|
||
cache->eviction_cookie = scm_ihashq (SCM_PACK (cache->eviction_cookie), -1);
|
||
idx = cache->eviction_cookie & (SCM_CACHE_SIZE - 1);
|
||
memcpy (evicted, cache->entries + idx, sizeof (*evicted));
|
||
memmove (cache->entries + 1,
|
||
cache->entries,
|
||
sizeof (cache->entries[0]) * idx);
|
||
cache->entries[0].key = 0;
|
||
cache->entries[0].value = 0;
|
||
}
|
||
|
||
static inline struct scm_cache_entry*
|
||
scm_cache_lookup (struct scm_cache *cache, SCM k)
|
||
{
|
||
scm_t_bits k_bits = SCM_UNPACK (k);
|
||
struct scm_cache_entry *entry = cache->entries;
|
||
/* Unrolled binary search, compiled to branchless cmp + cmov chain. */
|
||
if (entry[4].key <= k_bits) entry += 4;
|
||
if (entry[2].key <= k_bits) entry += 2;
|
||
if (entry[1].key <= k_bits) entry += 1;
|
||
return entry;
|
||
}
|
||
|
||
static inline void
|
||
scm_cache_insert (struct scm_cache *cache, SCM k, SCM v,
|
||
struct scm_cache_entry *evicted)
|
||
{
|
||
struct scm_cache_entry *entry;
|
||
|
||
if (scm_cache_full_p (cache))
|
||
scm_cache_evict_1 (cache, evicted);
|
||
entry = scm_cache_lookup (cache, k);
|
||
if (entry->key == SCM_UNPACK (k))
|
||
{
|
||
entry->value = SCM_UNPACK (v);
|
||
return;
|
||
}
|
||
memmove (cache->entries,
|
||
cache->entries + 1,
|
||
(entry - cache->entries) * sizeof (*entry));
|
||
entry->key = SCM_UNPACK (k);
|
||
entry->value = SCM_UNPACK (v);
|
||
}
|
||
|
||
#endif /* SCM_CACHE_INTERNAL_H */
|