mruby/mruby — pull request #1820 by ksss (2014-03-06 14:22:40+00:00)
Title: Embed small string

I have implemented embed string like CRuby.

### change RString structure

```c:include/mruby/string.h
struct RString {
MRB_OBJECT_HEADER;
union {
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;
};
```

`s->as.heap.*` can use when normal or nofree string.

`s->as.ary` can use when embed string.

`RSTRING_EMBED_LEN_MAX` is under not increase sizeof struct RString.(max 15)

using `s->flags` 3~7/21 bit.

- 3rd bit is embed flag.
- 4th~7th bits is embed string length.

`mrb_str_new` make a embed or normal string by length.

### good

- no break compatibility in mruby-API
- reduce allocate and memory

```
$ uname -a
Linux localhost.localdomain 2.6.32-279.el6.x86_64 #1 SMP Fri Jun 22 12:19:21 UTC
```

```
$ valgrind --leak-check=full build/host/test/mrbtest
```

before e40428af7b02870663a6c052496c176c22c5e875
```
total heap usage: 105,388 allocs, 105,388 frees, 4,325,360,922 bytes allocated
```

after 54132e436494b4797ca53cbd934963e7d752ac42 (no change test/*)
```
total heap usage: 95,095 allocs, 95,095 frees, 4,325,323,735 bytes allocated
```

### bad

- not simple
- break compatibility in C-API

RString C-API must be change.
`RSTRING_PTR(s)` no change
`RSTRING(s)->ptr` => `RSTRING_STR(s)`
`RSTRING(s)->len` => `RSTRING_LEN(s)`