ObjFW  Check-in [f92e1c77a0]

Overview
Comment:Optimize OFAutoreleasePool.

It always allocates memory for 16 objects now. releaseObjects does not
free the memory allocated to store objects so that the same number of
objects can be stored in the autorelease pool again without the need to
allocate memory. This will give loops creating lots of objects a huge
performance boost.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f92e1c77a05d45ba835c540d03ef2a8829118ebf04f4aa961dadbe276686a10e
User & Date: js on 2010-07-08 09:20:54
Other Links: manifest | tags
Context
2010-07-08
09:44
Cache OFAutoreleasePool class. check-in: 3c5eb0ddb6 user: js tags: trunk
09:20
Optimize OFAutoreleasePool. check-in: f92e1c77a0 user: js tags: trunk
08:57
Don't retain + autorelease objects returned from collections. check-in: d4b379485a user: js tags: trunk
Changes

Modified src/OFAutoreleasePool.h from [80b6b20e53] to [20042a6dbe].

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
/*
 * Copyright (c) 2008 - 2010
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "OFObject.h"

@class OFMutableArray;

/**
 * \brief A pool that keeps track of objects to release.
 *
 * The OFAutoreleasePool class is a class that keeps track of objects that will
 * be released when the autorelease pool is released.
 *
 * Every thread has its own stack of autorelease pools.
 */
@interface OFAutoreleasePool: OFObject
{
	OFMutableArray *objects;
	OFAutoreleasePool *next, *prev;


}

/**
 * Adds an object to the autorelease pool at the top of the thread-specific
 * stack.
 *
 * \param obj The object to add to the autorelease pool













<
<










<

>
>







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
/*
 * Copyright (c) 2008 - 2010
 *   Jonathan Schleifer <js@webkeks.org>
 *
 * All rights reserved.
 *
 * This file is part of ObjFW. It may be distributed under the terms of the
 * Q Public License 1.0, which can be found in the file LICENSE included in
 * the packaging of this file.
 */

#import "OFObject.h"



/**
 * \brief A pool that keeps track of objects to release.
 *
 * The OFAutoreleasePool class is a class that keeps track of objects that will
 * be released when the autorelease pool is released.
 *
 * Every thread has its own stack of autorelease pools.
 */
@interface OFAutoreleasePool: OFObject
{

	OFAutoreleasePool *next, *prev;
	id *objects;
	size_t count, size;
}

/**
 * Adds an object to the autorelease pool at the top of the thread-specific
 * stack.
 *
 * \param obj The object to add to the autorelease pool
42
43
44
45
46
47
48







49
50
51
52
53
54
55
 *
 * \param obj The object to add to the autorelease pool
 */
- (void)addObject: (OFObject*)obj;

/**
 * Releases all objects in the autorelease pool.







 *
 * If a garbage collector is added in the future, it will tell the GC that now
 * is a good time to clean up, as this is often used after a lot of objects
 * have been added to the pool that should be released before the next iteration
 * of a loop, which adds objects again. Thus, it is usually a clean up call.
 */
- (void)releaseObjects;







>
>
>
>
>
>
>







41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
 *
 * \param obj The object to add to the autorelease pool
 */
- (void)addObject: (OFObject*)obj;

/**
 * Releases all objects in the autorelease pool.
 *
 * This does not free the memory allocated to store pointers to the objects in
 * the pool, so reusing the pool does not allocate any memory until the previous
 * number of objects is exceeded. It behaves this way to optimize loops that
 * always work with the same or similar number of objects and call relaseObjects
 * at the end of the loop, which is propably the most common case for
 * releaseObjects.
 *
 * If a garbage collector is added in the future, it will tell the GC that now
 * is a good time to clean up, as this is often used after a lot of objects
 * have been added to the pool that should be released before the next iteration
 * of a loop, which adds objects again. Thus, it is usually a clean up call.
 */
- (void)releaseObjects;

Modified src/OFAutoreleasePool.m from [e16884bf15] to [14bd700788].

15
16
17
18
19
20
21


22
23
24
25
26
27
28

#import "OFAutoreleasePool.h"
#import "OFArray.h"
#import "OFExceptions.h"

#ifdef OF_THREADS
# import "threading.h"



static of_tlskey_t first_key, last_key;
#else
static OFAutoreleasePool *first = nil, *last = nil;
#endif

@implementation OFAutoreleasePool







>
>







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#import "OFAutoreleasePool.h"
#import "OFArray.h"
#import "OFExceptions.h"

#ifdef OF_THREADS
# import "threading.h"

#define GROW_SIZE 16

static of_tlskey_t first_key, last_key;
#else
static OFAutoreleasePool *first = nil, *last = nil;
#endif

@implementation OFAutoreleasePool
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


153


154
155
156
157
158
159
160
161
#else
		first = self;
#endif
	}

	if (prev != nil)
		prev->next = self;










	return self;
}

- (void)addObject: (OFObject*)obj
{
	if (objects == nil)
		objects = [[OFMutableArray alloc] init];




	[objects addObject: obj];
	[obj release];

}

- (void)releaseObjects
{


	[next releaseObjects];


	[objects release];
	objects = nil;

}

- (void)release
{
	[self dealloc];
}

- (void)drain
{
	[self dealloc];
}

- (void)dealloc
{


	[next dealloc];


	[objects release];

	/*
	 * If of_tlskey_set fails, this is a real problem. The best we can do
	 * is to not change the pool below the current pool and stop
	 * deallocation. This way, new objects will be added to the current
	 * pool, but released when the pool below gets released - and maybe
	 * the pool itself will be released as well then, because maybe







>
>
>
>
>
>
>
>
>






|
|
>
>
>
|
|
|
>




>
>

>
>
|
|
>














>
>

>
>
|







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
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
#else
		first = self;
#endif
	}

	if (prev != nil)
		prev->next = self;

	size = GROW_SIZE;
	@try {
		objects = [self allocMemoryForNItems: GROW_SIZE
					    withSize: sizeof(id)];
	} @catch (OFException *e) {
		[self dealloc];
		@throw e;
	}

	return self;
}

- (void)addObject: (OFObject*)obj
{
	if (count + 1 > size) {
		objects = [self resizeMemory: objects
				    toNItems: size + GROW_SIZE
				    withSize: sizeof(id)];
		size += GROW_SIZE;
	}

	objects[count] = obj;
	count++;
}

- (void)releaseObjects
{
	size_t i;

	[next releaseObjects];

	for (i = 0; i < count; i++)
		[objects[i] release];

	count = 0;
}

- (void)release
{
	[self dealloc];
}

- (void)drain
{
	[self dealloc];
}

- (void)dealloc
{
	size_t i;

	[next dealloc];

	for (i = 0; i < count; i++)
		[objects[i] release];

	/*
	 * If of_tlskey_set fails, this is a real problem. The best we can do
	 * is to not change the pool below the current pool and stop
	 * deallocation. This way, new objects will be added to the current
	 * pool, but released when the pool below gets released - and maybe
	 * the pool itself will be released as well then, because maybe