summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2014-03-07 18:51:32 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2014-03-07 18:51:32 +0900
commitac8d04fb6c2072c9af0b0587044360dde3b3b77d (patch)
treeab80df9179be235331935999dc4393670746a5ea /include
parent035898c7215c417e2ba24e759c033710ac74c6cc (diff)
parent4bc19d5fadaf85523774eac29520cca03a1516b1 (diff)
downloadmruby-ac8d04fb6c2072c9af0b0587044360dde3b3b77d.tar.gz
mruby-ac8d04fb6c2072c9af0b0587044360dde3b3b77d.zip
Merge pull request #1820 from ksss/string-embed
Embed small string
Diffstat (limited to 'include')
-rw-r--r--include/mruby/string.h44
1 files changed, 35 insertions, 9 deletions
diff --git a/include/mruby/string.h b/include/mruby/string.h
index 966f0bf77..7e7fb13d9 100644
--- a/include/mruby/string.h
+++ b/include/mruby/string.h
@@ -15,25 +15,51 @@ extern "C" {
extern const char mrb_digitmap[];
+/* (sizeof(mrb_int)*2+sizeof(char*))/sizeof(char)-1 */
+#if defined(MRB_INT16)
+# define RSTRING_EMBED_LEN_MAX 9
+#elif defined(MRB_INT64)
+# define RSTRING_EMBED_LEN_MAX 15
+#else
+# define RSTRING_EMBED_LEN_MAX 11
+#endif
+
struct RString {
MRB_OBJECT_HEADER;
- mrb_int len;
union {
- mrb_int capa;
- struct mrb_shared_string *shared;
- } aux;
- char *ptr;
+ struct {
+ mrb_int len;
+ union {
+ mrb_int capa;
+ struct mrb_shared_string *shared;
+ } aux;
+ char *ptr;
+ } heap;
+ char ary[RSTRING_EMBED_LEN_MAX + 1];
+ } as;
};
#define mrb_str_ptr(s) ((struct RString*)(mrb_ptr(s)))
#define RSTRING(s) ((struct RString*)(mrb_ptr(s)))
-#define RSTRING_PTR(s) (RSTRING(s)->ptr)
-#define RSTRING_LEN(s) (RSTRING(s)->len)
-#define RSTRING_CAPA(s) (RSTRING(s)->aux.capa)
-#define RSTRING_END(s) (RSTRING(s)->ptr + RSTRING(s)->len)
+#define RSTRING_PTR(s)\
+ ((RSTRING(s)->flags & MRB_STR_EMBED) ?\
+ RSTRING(s)->as.ary :\
+ RSTRING(s)->as.heap.ptr)
+#define RSTRING_LEN(s)\
+ ((RSTRING(s)->flags & MRB_STR_EMBED) ?\
+ (mrb_int)((RSTRING(s)->flags & MRB_STR_EMBED_LEN_MASK) >> MRB_STR_EMBED_LEN_SHIFT) :\
+ RSTRING(s)->as.heap.len)
+#define RSTRING_CAPA(s)\
+ ((RSTRING(s)->flags & MRB_STR_EMBED) ?\
+ RSTRING_EMBED_LEN_MAX :\
+ RSTRING(s)->as.heap.aux.capa)
+#define RSTRING_END(s) (RSTRING_PTR(s) + RSTRING_LEN(s))
#define MRB_STR_SHARED 1
#define MRB_STR_NOFREE 2
+#define MRB_STR_EMBED 4
+#define MRB_STR_EMBED_LEN_MASK 120
+#define MRB_STR_EMBED_LEN_SHIFT 3
void mrb_gc_free_str(mrb_state*, struct RString*);
void mrb_str_modify(mrb_state*, struct RString*);