NSArray 上的 NSKeyedArchiver 具有较大的大小开销

发布于 2024-08-31 07:36:11 字数 734 浏览 4 评论 0原文

我在 Mac OS X 程序中使用 NSKeyedArchiver 为 iPhone 应用程序生成数据。我发现默认情况下生成的档案比我预期的要大得多。示例:

NSMutableArray * ar = [NSMutableArray arrayWithCapacity:10];

for (int i = 0; i < 100000; i++) {
    NSString * s = [NSString stringWithFormat:@"item%06d", i];
    [ar addObject:s];
}
[NSKeyedArchiver archiveRootObject:ar toFile: @"NSKeyedArchiver.test"];

这存储了 10 * 100000 = 1M 字节的有用数据,但生成的文件的大小几乎为 3 MB。开销似乎随着数组中项目的数量而增加。在本例中,对于 1000 个项目,文件约为 22k。

“file”报告它是“Apple 二进制属性列表”(不是 XML 格式)。

有没有一种简单的方法来防止这种巨大的开销?我想使用 NSKeyedArchiver 因为它提供的简单性。我可以将数据写入我自己的非通用二进制格式,但这不是很优雅。此外,将数据聚合成大块并将它们提供给 NSKeyedArchiver 应该可以工作,但同样,这有点击败了使用 simple&easy&ready to use archiver 的点。我是否缺少一些可以减少这种开销的方法调用或使用模式?

I'm using NSKeyedArchiver in Mac OS X program which generates data for iPhone application. I found out that by default, resulting archives are much bigger than I expected. Example:

NSMutableArray * ar = [NSMutableArray arrayWithCapacity:10];

for (int i = 0; i < 100000; i++) {
    NSString * s = [NSString stringWithFormat:@"item%06d", i];
    [ar addObject:s];
}
[NSKeyedArchiver archiveRootObject:ar toFile: @"NSKeyedArchiver.test"];

This stores 10 * 100000 = 1M bytes of useful data, yet the size of the resulting file is almost three megabytes. The overhead seems to be growing with number of items in the array. In this case, for 1000 items, the file was about 22k.

"file" reports that it is a "Apple binary property list" (not the XML format).

Is there an simple way to prevent this huge overhead? I wanted to use the NSKeyedArchiver for the simplicity it provides. I can write data to my own, non-generic, binary format, but that's not very elegant. Also, aggregating the data into large chunks and feeding these to the NSKeyedArchiver should work, but again, that kinda beats the point of using simple&easy&ready to use archiver. Am I missing some method call or usage pattern that would reduce this overhead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

路还长,别太狂 2024-09-07 07:36:12

我提交了一个错误来跟踪这个问题。

除此之外,NSKeyedArchiver 是为归档对象网络而设计的。例如,如果一个对象在图表中出现两次,则在取消存档时您仍然会发现这种情况。您可能会看到这种独特性的开销。

对于分层结构化数据而不是任意对象网络,请尝试 NSPropertyListSerialization。我看到二进制 plist 有 1.8MB。

I filed a bug to track this.

That aside, NSKeyedArchiver is designed for archiving object networks. Like, if an object appears twice in the graph, on unarchiving you'll still find that is the case. You're probably seeing overhead for this kind of uniquing.

For hierarchical structured data as opposed to arbitrary object networks, try NSPropertyListSerialization. I see 1.8MB for a binary plist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文