ObjFW  Check-in [92c12b1d4f]

Overview
Comment:Optimize branch prediction for ifs inside loops.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 92c12b1d4fd060f14f01141c2011326f7cd66cc0ec46c3be4fa92700237f65ad
User & Date: js on 2008-11-29 11:59:34
Other Links: manifest | tags
Context
2008-11-30
15:17
Clean up imports. check-in: 4b8666fe15 user: js tags: trunk
2008-11-29
11:59
Optimize branch prediction for ifs inside loops. check-in: 92c12b1d4f user: js tags: trunk
2008-11-28
17:31
glibc needs one import more. check-in: ae389818d9 user: js tags: trunk
Changes

Modified src/OFMacros.h from [d433d98b76] to [9f2e3ed2ba].

24
25
26
27
28
29
30








}
#else
#define OF_BSWAP_V(buf, len)
#endif

#define OF_ROL(val, bits) \
	(((val) << (bits)) | ((val) >> (32 - (bits))))















>
>
>
>
>
>
>
>
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
}
#else
#define OF_BSWAP_V(buf, len)
#endif

#define OF_ROL(val, bits) \
	(((val) << (bits)) | ((val) >> (32 - (bits))))

#ifdef __GNUC__
#define OF_LIKELY(cond) __builtin_expect(!!(cond), 1)
#define OF_UNLIKELY(cond) __builtin_expect(!!(cond), 0)
#else
#define OF_LIKELY(cond) cond
#define OF_UNLIKELY(cond) cond
#endif

Modified src/OFObject.m from [c0fb34ec43] to [c722de15cb].

17
18
19
20
21
22
23

24
25
26
27
28
29
30
#import <objc/objc-api.h>
#ifdef HAVE_OBJC_RUNTIME_H
#import <objc/runtime.h>
#endif

#import "OFObject.h"
#import "OFExceptions.h"


@implementation OFObject
- init
{
	if ((self = [super init]) != nil) {
		__memchunks = NULL;
		__memchunks_size = 0;







>







17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#import <objc/objc-api.h>
#ifdef HAVE_OBJC_RUNTIME_H
#import <objc/runtime.h>
#endif

#import "OFObject.h"
#import "OFExceptions.h"
#import "OFMacros.h"

@implementation OFObject
- init
{
	if ((self = [super init]) != nil) {
		__memchunks = NULL;
		__memchunks_size = 0;
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
		[self freeMem: ptr];
		return NULL;
	}

	iter = __memchunks + __memchunks_size;

	while (iter-- > __memchunks) {
		if (*iter == ptr) {
			if ((ptr = realloc(ptr, size)) == NULL)
				[[OFNoMemException newWithObject: self
							 andSize: size] raise];
			
			*iter = ptr;
			return ptr;
		}
	}







|
|







107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
		[self freeMem: ptr];
		return NULL;
	}

	iter = __memchunks + __memchunks_size;

	while (iter-- > __memchunks) {
		if (OF_UNLIKELY(*iter == ptr)) {
			if (OF_UNLIKELY((ptr = realloc(ptr, size)) == NULL))
				[[OFNoMemException newWithObject: self
							 andSize: size] raise];
			
			*iter = ptr;
			return ptr;
		}
	}
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

	iter = __memchunks + __memchunks_size;
	i = __memchunks_size;

	while (iter-- > __memchunks) {
		i--;

		if (*iter == ptr) {
			memchunks_size = __memchunks_size - 1;
			last = __memchunks[memchunks_size];

			if (__memchunks_size == 0 ||
			    memchunks_size > SIZE_MAX / sizeof(void*))
				[[OFOutOfRangeException newWithObject: self]
				    raise];

			if (memchunks_size == 0) {
				free(ptr);
				free(__memchunks);

				__memchunks = NULL;
				__memchunks_size = 0;

				return self;
			}

			if ((memchunks = realloc(__memchunks,
			    memchunks_size * sizeof(void*))) == NULL)
				[[OFNoMemException newWithObject: self
							 andSize:
							     memchunks_size]
				    raise];

			free(ptr);
			__memchunks = memchunks;
			__memchunks[i] = last;
			__memchunks_size = memchunks_size;

			return self;
		}
	}

	[[OFMemNotPartOfObjException newWithObject: self
					andPointer: ptr] raise];
	return self	/* never reached, but makes gcc happy */;
}
@end







|



|
|



|









|
|
















|


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

	iter = __memchunks + __memchunks_size;
	i = __memchunks_size;

	while (iter-- > __memchunks) {
		i--;

		if (OF_UNLIKELY(*iter == ptr)) {
			memchunks_size = __memchunks_size - 1;
			last = __memchunks[memchunks_size];

			if (OF_UNLIKELY(__memchunks_size == 0 ||
			    memchunks_size > SIZE_MAX / sizeof(void*)))
				[[OFOutOfRangeException newWithObject: self]
				    raise];

			if (OF_UNLIKELY(memchunks_size == 0)) {
				free(ptr);
				free(__memchunks);

				__memchunks = NULL;
				__memchunks_size = 0;

				return self;
			}

			if (OF_UNLIKELY((memchunks = realloc(__memchunks,
			    memchunks_size * sizeof(void*))) == NULL))
				[[OFNoMemException newWithObject: self
							 andSize:
							     memchunks_size]
				    raise];

			free(ptr);
			__memchunks = memchunks;
			__memchunks[i] = last;
			__memchunks_size = memchunks_size;

			return self;
		}
	}

	[[OFMemNotPartOfObjException newWithObject: self
					andPointer: ptr] raise];
	return self;	/* never reached, but makes gcc happy */
}
@end

Modified src/OFXMLFactory.m from [5ac96ead53] to [117b639361].

12
13
14
15
16
17
18

19
20
21
22
23
24
25
#import <stdarg.h>
#import <stddef.h>
#import <stdlib.h>
#import <string.h>

#import "OFXMLFactory.h"
#import "OFExceptions.h"


/*
 * We don't use OFString in this file for performance reasons!
 *
 * We already have a clue about how big the resulting string will get, so we
 * can prealloc and only resize when really necessary - OFString would always
 * resize when we append, which would be slow here.







>







12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#import <stdarg.h>
#import <stddef.h>
#import <stdlib.h>
#import <string.h>

#import "OFXMLFactory.h"
#import "OFExceptions.h"
#import "OFMacros.h"

/*
 * We don't use OFString in this file for performance reasons!
 *
 * We already have a clue about how big the resulting string will get, so we
 * can prealloc and only resize when really necessary - OFString would always
 * resize when we append, which would be slow here.
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

134
135
136
137
138
139

140
141
142
143
144
145

146
147
148
149
150
151
152
	if ((ret = malloc(len + 1)) == NULL)
		[[OFNoMemException newWithObject: nil
					 andSize: len + 1] raise];

	for (i = j = 0; i < len; i++) {
		switch (s[i]) {
		case '<':
			if (!xf_add2chars(&ret, &nlen, &j, "&lt;"))
				[[OFNoMemException newWithObject: nil
							 andSize: nlen + 4]
				    raise];
			break;
		case '>':
			if (!xf_add2chars(&ret, &nlen, &j, "&gt;"))
				[[OFNoMemException newWithObject: nil
							 andSize: nlen + 4]
				    raise];
			break;
		case '"':
			if (!xf_add2chars(&ret, &nlen, &j, "&quot;"))

				[[OFNoMemException newWithObject: nil
							 andSize: nlen + 6]
				    raise];
			break;
		case '\'':
			if (!xf_add2chars(&ret, &nlen, &j, "&apos;"))

				[[OFNoMemException newWithObject: nil
							 andSize: nlen + 6]
				    raise];
			break;
		case '&':
			if (!xf_add2chars(&ret, &nlen, &j, "&amp;"))

				[[OFNoMemException newWithObject: nil
							 andSize: nlen + 5]
				    raise];
			break;
		default:
			ret[j++] = s[i];
			break;







|





|





|
>





|
>





|
>







115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
	if ((ret = malloc(len + 1)) == NULL)
		[[OFNoMemException newWithObject: nil
					 andSize: len + 1] raise];

	for (i = j = 0; i < len; i++) {
		switch (s[i]) {
		case '<':
			if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j, "&lt;")))
				[[OFNoMemException newWithObject: nil
							 andSize: nlen + 4]
				    raise];
			break;
		case '>':
			if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j, "&gt;")))
				[[OFNoMemException newWithObject: nil
							 andSize: nlen + 4]
				    raise];
			break;
		case '"':
			if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j,
			    "&quot;")))
				[[OFNoMemException newWithObject: nil
							 andSize: nlen + 6]
				    raise];
			break;
		case '\'':
			if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j,
			    "&apos;")))
				[[OFNoMemException newWithObject: nil
							 andSize: nlen + 6]
				    raise];
			break;
		case '&':
			if (OF_UNLIKELY(!xf_add2chars(&ret, &nlen, &j,
			    "&amp;")))
				[[OFNoMemException newWithObject: nil
							 andSize: nlen + 5]
				    raise];
			break;
		default:
			ret[j++] = s[i];
			break;
169
170
171
172
173
174
175
176

177
178
179
180
181
182
183
184

185
186
187
188
189
190
191
192

193
194
195
196
197
198
199
200

201
202
203
204
205
206
207
208

209
210
211
212
213
214
215
		[[OFNoMemException newWithObject: nil
					 andSize: (len + 1) * sizeof(wchar_t)]
		     raise];

	for (i = j = 0; i < len; i++) {
		switch (s[i]) {
		case L'<':
			if (!xf_add2wchars(&ret, &nlen, &j, L"&lt;"))

				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 4) *
								  sizeof(
								  wchar_t)]
				    raise];
			break;
		case L'>':
			if (!xf_add2wchars(&ret, &nlen, &j, L"&gt;"))

				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 4) *
								  sizeof(
								  wchar_t)]
				    raise];
			break;
		case L'"':
			if (!xf_add2wchars(&ret, &nlen, &j, L"&quot;"))

				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 6) *
								  sizeof(
								  wchar_t)]
				    raise];
			break;
		case L'\'':
			if (!xf_add2wchars(&ret, &nlen, &j, L"&apos;"))

				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 6) *
								  sizeof(
								  wchar_t)]
				    raise];
			break;
		case L'&':
			if (!xf_add2wchars(&ret, &nlen, &j, L"&amp;"))

				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 5) *
								  sizeof(
								  wchar_t)]
				    raise];
			break;
		default:







|
>







|
>







|
>







|
>







|
>







173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
		[[OFNoMemException newWithObject: nil
					 andSize: (len + 1) * sizeof(wchar_t)]
		     raise];

	for (i = j = 0; i < len; i++) {
		switch (s[i]) {
		case L'<':
			if (OF_UNLIKELY(!xf_add2wchars(&ret, &nlen, &j,
			    L"&lt;")))
				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 4) *
								  sizeof(
								  wchar_t)]
				    raise];
			break;
		case L'>':
			if (OF_UNLIKELY(!xf_add2wchars(&ret, &nlen, &j,
			    L"&gt;")))
				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 4) *
								  sizeof(
								  wchar_t)]
				    raise];
			break;
		case L'"':
			if (OF_UNLIKELY(!xf_add2wchars(&ret, &nlen, &j,
			    L"&quot;")))
				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 6) *
								  sizeof(
								  wchar_t)]
				    raise];
			break;
		case L'\'':
			if (OF_UNLIKELY(!xf_add2wchars(&ret, &nlen, &j,
			    L"&apos;")))
				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 6) *
								  sizeof(
								  wchar_t)]
				    raise];
			break;
		case L'&':
			if (OF_UNLIKELY(!xf_add2wchars(&ret, &nlen, &j,
			    L"&amp;")))
				[[OFNoMemException newWithObject: nil
							 andSize: (nlen + 5) *
								  sizeof(
								  wchar_t)]
				    raise];
			break;
		default:
243
244
245
246
247
248
249

250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267

	/* Arguments */
	va_start(args, data);
	while ((arg = va_arg(args, char*)) != NULL &&
	    (val = va_arg(args, char*)) != NULL) {
		char *esc_val;


		if ((esc_val = [OFXMLFactory escapeCString: val]) == NULL) {
			/*
			 * escapeCString already throws an exception,
			 * no need to throw a second one here.
			 */
			free(xml);
			return NULL;
		}

		if (!xf_resize_chars(&xml, &len, 1 + strlen(arg) + 2 +
		    strlen(esc_val) + 1)) {
			free(esc_val);
			[[OFNoMemException newWithObject: nil
						 andSize: len + 1 +
							  strlen(arg) + 2 +
							  strlen(esc_val) + 1]
			    raise];
		}







>
|








|
|







252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

	/* Arguments */
	va_start(args, data);
	while ((arg = va_arg(args, char*)) != NULL &&
	    (val = va_arg(args, char*)) != NULL) {
		char *esc_val;

		if (OF_UNLIKELY((esc_val =
		    [OFXMLFactory escapeCString: val]) == NULL)) {
			/*
			 * escapeCString already throws an exception,
			 * no need to throw a second one here.
			 */
			free(xml);
			return NULL;
		}

		if (OF_UNLIKELY(!xf_resize_chars(&xml, &len, 1 + strlen(arg) + 
		    2 + strlen(esc_val) + 1))) {
			free(esc_val);
			[[OFNoMemException newWithObject: nil
						 andSize: len + 1 +
							  strlen(arg) + 2 +
							  strlen(esc_val) + 1]
			    raise];
		}
338
339
340
341
342
343
344

345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362

	/* Arguments */
	va_start(args, data);
	while ((arg = va_arg(args, wchar_t*)) != NULL &&
	    (val = va_arg(args, wchar_t*)) != NULL) {
		wchar_t *esc_val;


		if ((esc_val = [OFXMLFactory escapeWideCString: val]) == NULL) {
			/*
			 * escapeWideCString already throws an exception,
			 * no need to throw a second one here.
			 */
			free(xml);
			return NULL;
		}

		if (!xf_resize_wchars(&xml, &len, 1 + wcslen(arg) + 2 +
		    wcslen(esc_val) + 1)) {
			free(esc_val);
			[[OFNoMemException newWithObject: nil
						 andSize: (len + 1 +
							  wcslen(arg) + 2 +
							  wcslen(esc_val) + 1) *
							  sizeof(wchar_t)]
			    raise];







>
|








|
|







348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373

	/* Arguments */
	va_start(args, data);
	while ((arg = va_arg(args, wchar_t*)) != NULL &&
	    (val = va_arg(args, wchar_t*)) != NULL) {
		wchar_t *esc_val;

		if (OF_UNLIKELY((esc_val =
		    [OFXMLFactory escapeWideCString: val]) == NULL)) {
			/*
			 * escapeWideCString already throws an exception,
			 * no need to throw a second one here.
			 */
			free(xml);
			return NULL;
		}

		if (OF_UNLIKELY(!xf_resize_wchars(&xml, &len, 1 + wcslen(arg) +
		    2 + wcslen(esc_val) + 1))) {
			free(esc_val);
			[[OFNoMemException newWithObject: nil
						 andSize: (len + 1 +
							  wcslen(arg) + 2 +
							  wcslen(esc_val) + 1) *
							  sizeof(wchar_t)]
			    raise];
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
		[[OFNoMemException newWithObject: nil
					 andSize: len] raise];

	memcpy(ret, strs[0], len - 1);
	pos = len - 1;

	for (i = 1; strs[i] != NULL; i++) {
		if (!xf_add2chars(&ret, &len, &pos, strs[i])) {
			free(ret);
			[[OFNoMemException newWithObject: nil
						 andSize: len + strlen(strs[i])]
			    raise];
		}
	}








|







441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
		[[OFNoMemException newWithObject: nil
					 andSize: len] raise];

	memcpy(ret, strs[0], len - 1);
	pos = len - 1;

	for (i = 1; strs[i] != NULL; i++) {
		if (OF_UNLIKELY(!xf_add2chars(&ret, &len, &pos, strs[i]))) {
			free(ret);
			[[OFNoMemException newWithObject: nil
						 andSize: len + strlen(strs[i])]
			    raise];
		}
	}