如何序列化大对象/数组JSON [英] How to serialize large objects/arrays to JSON
问题描述
我的应用程序需要生产具有大数据
类型的数组对象的属性JSON。该数组需要保留在内存中,因为它收集DB输出,一旦阵列完成后才能确定一些属性。
并发症:数组是数值为基础,必须在JSON输出出现正因为如此,所以直 json_en code()
不是一个选项。 p>
要实现这一点在低规格的机器一样RasPi我看着微调内存消耗:
- 使用
SPLFixedArray
- 使用
字符串
和包()
这两种方法都照顾阵列存储内存问题,但是,当谈到在JSON编码失败。
我看着落实 JsonSerializable
但因为它迫使用户返回的结果中,然后连接codeD以JSON我回来
公共职能jsonSerialize(){
返回$这个 - >的toArray();
}
具有相同的存储器的问题。
zendframework / Component_ZendJson
看起来很有希望,因为它看起来具有对象的toJSON()
方法来提供它们自己的编码为字符串
而不是对象
。
我不知道是否有更好的选择不给内存问题?
在我的调查中,我已经看了5种不同的方法存储在内存中的元组的大型阵列,其结果(在50K采样记录)总结如下:
-
的天真的
导出使用JSON是json_en code简单
阵列(阵列(),()数组)
内存:18.5MB(大)结果
时间:100毫秒〜建立和转储阵列(Windows计算机) -
的 SPL库的
这个方法存储在一切嵌套的
SPLFixedArrays
:SPLFixedArray [SPLFixedArray]
。 JSON导出已完成延长的Zend \\ Json的\\ EN codeR
通过执行的toJSON
方法。内存:15.5MB(仍然较大)结果
时间:〜1.3s,X10慢 -
SPL库
2相似,但不是内部
SPLFixedArray
从PHP的包使用包装的字符串()
功能。内存:3.5MB( 5倍,而)结果
时间:〜1.3s,X10慢 - 显然包()
是嵌套数组同样放缓 -
SPL库
2相似,但不是内部
SPLFixedArray
实际元组简单地写为连续值的根阵列。内存:3.25MB(再小)结果
时间:〜0.7秒,仅5233慢 - 我们有一个赢家 -
包()
3相似,但不是根本
SPLFixedArray
收拾一切都变成使用PHP的pack()的一个字符串
功能。但这显然需要有关的知识和个人阵列的固定,相同的结构。内存:1.25MB(真的很小 - 只有1/12的原厂内存)结果
时间:〜1.7S,X16慢
结论
虽然(5)提供了最佳的内存使用率它也是非常缓慢的。对于我而言,我看中了(4),这是原来的内存消耗按钮5〜6倍时慢JSON编码是考虑到问责也是20%左右。一个可以接受的妥协。
My app needs to produce json of an object that has a large data
property of type array. The array needs to remain in memory as it collects DB output and some properties can only be determined once the array is completed.
Complication: the array is numerically-based and must appear as such in the json output, therefore straight json_encode()
is not an option.
To make this possible on low-spec machines like RasPi I've looked into trimming memory consumption:
- Use
SPLFixedArray
- Use
string
andpack()
Both approaches take care of the array storage memory problem but fail when it comes to encoding in JSON.
I've looked into implementing JsonSerializable
but as it forces users to return the result which is then encoded in Json I'm back to
public function jsonSerialize() {
return $this->toArray();
}
which has the same memory problems.
zendframework/Component_ZendJson
looks promising as it looks for objects having a toJson()
method to provide their own encoding as string
instead of object
.
I'm wondering if there are better options that don't give memory issues?
In my investigation I've looked at 5 different approaches for storing large arrays of tuples in memory, summarized here with their results (sampled at 50k records):
Naive
Exporting json is straightforward with json_encode using
array(array(), array())
Memory: 18.5MB (huge)
Time: ~100ms to build and dump the array (Windows PC)SPL Library
This approach stores everything in nested
SPLFixedArrays
:SPLFixedArray[SPLFixedArray]
. JSON export was done extendingZend\Json\Encoder
by implementing thetoJson
method.Memory: 15.5MB (still large)
Time: ~1.3s, x10 slowerSPL Library
Similar to 2, but instead of the inner
SPLFixedArray
uses packed strings from PHP'spack()
function.Memory: 3.5MB (5 times smaller)
Time: ~1.3s, x10 slower - apparentlypack()
is similarly slow as nested array.SPL Library
Similar to 2, but instead of the inner
SPLFixedArray
the actual Tuples are simply written as sequential values to the root array.Memory: 3.25MB (again smaller)
Time: ~0.7s, only x6 slower - do we have a winner here?pack()
Similar to 3, but instead of the root
SPLFixedArray
pack everything into a single string using PHP'spack()
function. This does obviously need knowledge about and a fixed, identical structure of the individual arrays.Memory: 1.25MB (really small - only 1/12th of original memory)
Time: ~1.7s, x16 slower
CONCLUSION
While (5) offers best memory utilization it is also extremely slow. For my purposes I've settled on (4) which is about 20% of original memory consumption but- when JSON encoding is taken into account- also 5~6 times slower. An acceptable compromise.
这篇关于如何序列化大对象/数组JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!