summaryrefslogtreecommitdiffstats
path: root/lib.h
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2022-10-06 20:43:58 -0700
committerKaz Kylheku <kaz@kylheku.com>2022-10-06 20:43:58 -0700
commitc126cf0d1e737c3eaad9fedf16071a397dcf3aad (patch)
treed5bf5dc9532579ee3df618b1ec9df3e089a371b3 /lib.h
parent6fd2b82e5e6ae996c4339a4d53307dd9610fe70d (diff)
downloadtxr-c126cf0d1e737c3eaad9fedf16071a397dcf3aad.tar.gz
txr-c126cf0d1e737c3eaad9fedf16071a397dcf3aad.tar.bz2
txr-c126cf0d1e737c3eaad9fedf16071a397dcf3aad.zip
strings: take advantage of malloc_usable_size
On platforms which have the malloc_usable_size function, we don't have to store the allocated size of an object; malloc provides us the allocated size (which may be larger than we requested). Here we take advantage of this for strings. And since we don't have to store the string allocated size any more, we use that field for something else: storing the hash code (for seed zero). This can speed up some hashing operations. * configure (have_malloc_usable_size): New variable. Configure test for have_malloc_usable size. We have to try several header files, too. We set the configure variable HAVE_MALLOC_USABLE_SIZE, and possibly HAVE_MALLOC_H or HAVE_MALLOC_NP_H. * lib.h (struct string): If HAVE_MALLOC_USABLE_SIZE is true, we define a member called hash insetad of alloc. Also, we change alloc to cnum. * lib.c: Include <malloc_np.h> if HAVE_MALLOC_NP_H is defined. (string_own, string, string_utf8, mkstring, mkustring, init_str, string_extend, string_finish, string_set_code, string_get_code, length_str, replace_str, chr_str_set): Fix code for both cases. On platforms with malloc_usable_size, we have the allocated size from malloc, so we don't have to retrieve it from the object or store it. Any operations which mutate the string must reset the hash field to zero; zero means "hash has not been calculated". * hash.c (equal_hash): Just retrive a string's hash value, if it is nonzero, otherwise calculate, cache it and return it. * gc.c (mark_obj): The alloc member of struct string is a machine integer now; no need to mark it.
Diffstat (limited to 'lib.h')
-rw-r--r--lib.h6
1 files changed, 5 insertions, 1 deletions
diff --git a/lib.h b/lib.h
index dc12c355..48320174 100644
--- a/lib.h
+++ b/lib.h
@@ -154,7 +154,11 @@ struct string {
obj_common;
wchar_t *str;
val len;
- val alloc;
+#if HAVE_MALLOC_USABLE_SIZE
+ ucnum hash;
+#else
+ cnum alloc;
+#endif
};
typedef struct {