File: small-string-optimization.hpp

package info (click to toggle)
higan 094-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 9,780 kB
  • ctags: 15,643
  • sloc: cpp: 103,963; ansic: 659; makefile: 531; sh: 25
file content (111 lines) | stat: -rwxr-xr-x 2,334 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#ifdef NALL_STRING_INTERNAL_HPP

/*
small string optimization (SSO) allocator
sizeof(string) == 16 (amd64)

utilizes a union to store small strings directly into text pointer
bypasses the need to allocate heap memory for small strings
requires extra computations, which can be slower for large strings

pros:
* potential for in-place resize
* no heap allocation when (capacity < 8)

cons:
* added overhead to fetch data()
* 32-bit platforms limited to (capacity < 4)
* pass-by-value requires heap allocation

*/

namespace nall {

char* string::data() {
  if(_capacity < SSO) return _text;
  return _data;
}

const char* string::data() const {
  if(_capacity < SSO) return _text;
  return _data;
}

void string::reserve(unsigned capacity) {
  if(capacity > _capacity) {
    if(capacity >= SSO) {
      capacity = bit::round(capacity + 1) - 1;
      if(_capacity < SSO) {
        char temp[SSO];
        memcpy(temp, _text, SSO);
        _data = (char*)malloc(capacity + 1);
        memcpy(_data, temp, SSO);
      } else {
        _data = (char*)realloc(_data, capacity + 1);
      }
    }
    _capacity = capacity;
    data()[_capacity] = 0;
  }
}

void string::resize(unsigned size) {
  reserve(size);
  data()[_size = size] = 0;
}

void string::reset() {
  if(_capacity >= SSO) free(_data);
  _data = nullptr;
  _capacity = SSO - 1;
  _size = 0;
}

string& string::operator=(const string& source) {
  if(&source == this) return *this;
  reset();
  if(source._capacity >= SSO) {
    _data = (char*)malloc(source._capacity + 1);
    _capacity = source._capacity;
    _size = source._size;
    memcpy(_data, source.data(), source.size() + 1);
  } else {
    memcpy(_text, source._text, SSO);
    _capacity = SSO - 1;
    _size = strlen(_text);
  }
  return *this;
}

string& string::operator=(string&& source) {
  if(&source == this) return *this;
  reset();
  memcpy(this, &source, sizeof(string));
  source._data = nullptr;
  source._capacity = SSO - 1;
  source._size = 0;
  return *this;
}

template<typename T, typename... Args> string::string(T&& source, Args&&... args) {
  construct();
  sprint(*this, std::forward<T>(source), std::forward<Args>(args)...);
}

string::string() {
  construct();
}

string::~string() {
  reset();
}

void string::construct() {
  _data = nullptr;
  _capacity = SSO - 1;
  _size = 0;
}

}

#endif