【发布时间】:2012-01-20 03:32:52
【问题描述】:
我有一些代码可以将大型(数 GB)XML 文件转换为另一种格式。
除此之外,我需要在哈希表中存储 1 或 2 GB 的浮点数(每个条目有两个浮点数),并将 int 作为值的键。
目前,我正在使用 NSMutableDictionary 和一个包含两个浮点数的自定义类:
// create the dictionary
NSMutableDictionary *points = [[NSMutableDictionary alloc] init];
// add an entry (the data is read from an XML file using libxml)
int pointId = 213453;
float x = 42.313554;
float y = -21.135213;
MyPoint *point = [[MyPoint alloc] initWithX:x Y:y];
[points setObject:point forKey:[NSNumber numberWithInt:pointId]];
[point release];
// retrieve an entry (this happens later on while parsing the same XML file)
int pointId = 213453;
float x;
float y;
MyPoint *point = [points objectForKey:[NSNumber numberWithInt:pointId]];
x = point.x;
y = point.y;
这个数据集占用了我现在正在使用的 XML 文件大约 800MB 的 RAM,而且执行起来需要相当长的时间。我希望有更好的性能,但更重要的是我需要降低内存消耗,以便处理更大的 XML 文件。
objc_msg_send 就在代码配置文件中,- [NSNumber numberWithInt:] 也是如此,我确信我可以通过完全避免对象来降低内存使用量,但我对 C 编程了解不多(这个项目肯定在教我!)。
如何用高效的 C 数据结构替换 NSMuableDictionary、NSNumber MyPoint?没有任何第三方库依赖?
我还希望能够将这个数据结构写入磁盘上的文件,这样我就可以处理一个不完全适合内存的数据集,但我可能没有这个能力也可以生活。
(对于不熟悉 Objective-C 的人,NSMutableDictionary 类只能存储 Obj-C 对象,并且键也必须是对象。NSNumber 和 MyPoint 是哑容器类,允许 NSMutableDictionary 使用 float和 int 值。)
编辑:
我已经尝试使用 CFMutableDictionary 来存储结构,根据 apple's sample code。当字典为空时,它的性能很好。但随着字典的增长,它变得越来越慢。大约 25% 通过解析文件(字典中约 400 万个项目)它开始突突,比文件中的早期慢两个数量级。
NSMutableDictionary 没有相同的性能问题。 Instruments 展示了很多应用哈希和比较字典键的活动(下面的intEqual() 方法)。比较一个 int 很快,所以经常执行它是非常错误的。
这是我创建字典的代码:
typedef struct {
float lat;
float lon;
} AGPrimitiveCoord;
void agPrimitveCoordRelease(CFAllocatorRef allocator, const void *ptr) {
CFAllocatorDeallocate(allocator, (AGPrimitiveCoord *)ptr);
}
Boolean agPrimitveCoordEqual(const void *ptr1, const void *ptr2) {
AGPrimitiveCoord *p1 = (AGPrimitiveCoord *)ptr1;
AGPrimitiveCoord *p2 = (AGPrimitiveCoord *)ptr2;
return (fabsf(p1->lat - p2->lat) < 0.0000001 && fabsf(p1->lon - p2->lon) < 0.0000001);
}
Boolean intEqual(const void *ptr1, const void *ptr2) {
return (int)ptr1 == (int)ptr2;
}
CFHashCode intHash(const void *ptr) {
return (CFHashCode)((int)ptr);
}
// init storage dictionary
CFDictionaryKeyCallBacks intKeyCallBacks = {0, NULL, NULL, NULL, intEqual, intHash};
CFDictionaryValueCallBacks agPrimitveCoordValueCallBacks = {0, NULL /*agPrimitveCoordRetain*/, agPrimitveCoordRelease, NULL, agPrimitveCoordEqual};
temporaryNodeStore = CFDictionaryCreateMutable(NULL, 0, &intKeyCallBacks, &agPrimitveCoordValueCallBacks);
// add an item to the dictionary
- (void)parserRecordNode:(int)nodeId lat:(float)lat lon:(float)lon
{
AGPrimitiveCoord *coordPtr = (AGPrimitiveCoord *)CFAllocatorAllocate(NULL, sizeof(AGPrimitiveCoord), 0);
coordPtr->lat = lat;
coordPtr->lon = lon;
CFDictionarySetValue(temporaryNodeStore, (void *)nodeId, coordPtr);
}
编辑 2:
性能问题是由于 Apple 的示例代码中几乎无用的哈希实现造成的。我通过使用这个来提高性能:
// hash algorithm from http://burtleburtle.net/bob/hash/integer.html
uint32_t a = abs((int)ptr);
a = (a+0x7ed55d16) + (a<<12);
a = (a^0xc761c23c) ^ (a>>19);
a = (a+0x165667b1) + (a<<5);
a = (a+0xd3a2646c) ^ (a<<9);
a = (a+0xfd7046c5) + (a<<3);
a = (a^0xb55a4f09) ^ (a>>16);
【问题讨论】:
-
部署后这个文件会改变吗?我正在考虑预处理。
-
我永远不会在部署中使用此代码。这是一个命令行工具,它创建一个与我的应用程序一起部署的数据库(我从 openstreetmap.org 的 ~300GB 数据集中提取了几百兆字节)
标签: objective-c c performance hashmap nsmutabledictionary