dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH] st.c (new_size): use next_pow2 function
@ 2014-10-02 18:55 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2014-10-02 18:55 UTC (permalink / raw)
  To: spew

Reduces object size slightly:

   text	   data	    bss	    dec	    hex	filename
2782359	  22400	  71880	2876639	 2be4df	ruby.orig
2781831	  22400	  71880	2876111	 2be2cf	ruby.pow2

This is not a performance-critical function, but the
smaller icache footprint seems worth it.
---
 st.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/st.c b/st.c
index fa3fa3f..072a587 100644
--- a/st.c
+++ b/st.c
@@ -141,13 +141,30 @@ remove_safe_packed_entry(st_table *table, st_index_t i, st_data_t never)
 }
 
 static st_index_t
+next_pow2(st_index_t x)
+{
+    x |= x >> 1;
+    x |= x >> 2;
+    x |= x >> 4;
+    x |= x >> 8;
+    x |= x >> 16;
+    if (sizeof(st_index_t) == 8) {
+	x |= x >> 32;
+    }
+    return x + 1;
+}
+
+static st_index_t
 new_size(st_index_t size)
 {
-    st_index_t i;
+    st_index_t n;
 
-    for (i=3; i<31; i++) {
-	if ((st_index_t)(1<<i) > size) return 1<<i;
-    }
+    if (size && (size & ~(size - 1)) == size)
+	return size;
+
+    n = next_pow2(size);
+    if (n > size)
+	return n;
 #ifndef NOT_RUBY
     rb_raise(rb_eRuntimeError, "st_table too big");
 #endif
-- 
EW

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2014-10-02 18:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-10-02 18:55 [PATCH] st.c (new_size): use next_pow2 function Eric Wong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).