可序列化对象放在捆绑包中时是否总是被序列化?

发布于 2024-09-15 17:07:44 字数 440 浏览 0 评论 0原文

我们想知道当将 Bundle 与可序列化或可打包对象一起使用时,编组实际上何时发生?是不是一放到包里就可以了?由于包主要用于在两个屏幕之间简单地传递数据(我们在这里甚至不讨论 IPC!),编组对象似乎没有多大意义,因为它始终保留在内存中,不?

我们是否正确地假设,仅当

  1. 数据必须传递到另一个进程(例如在 RMI 期间)或
  2. 组件(活动或服务)被破坏且实例状态必须写入磁盘时,才会发生 编组(无论是 Java 序列化还是 Android 打包) ?

我见过 Android 框架工程师(我相信是 Dianne Hackborn)说应该使用 Parcelable 而不是 Serialized,因为前者要快得多。快多少?如果对象在大多数情况下都没有被整理(假设我们对此的假设是正确的),这会产生影响吗?

We were wondering if when using Bundle with serializable or parcelable objects, when does the marshalling actually happen? As soon as you put it in the bundle? Since bundles are mostly used to simply pass around data between two screens (we're not even talking about IPC here!), there doesn't seem to be much point in marshalling an object, since it stays in memory all the time, no?

Are we right in assuming that marshalling (be it Java serializing or Android parcelling) only happens if

  1. the data must be passed to another process, e.g. during RMI, or
  2. the component (activity or service) gets destroyed and instance state must be written to disk?

I've seen Android framework engineers (I believe it was Dianne Hackborn) say that one should use Parcelable instead of Serializable because the former is much faster. How much faster? And will this even make a difference if the object isn't marshalled most of the time anyway (assuming our assumptions about this were right)?

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

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

发布评论

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

评论(2

客…行舟 2024-09-22 17:07:45

我想我已经明白了。我基本上花了最后一天和今天的大部分时间来调试 Android ParcelBundle 源代码,它的工作原理如下:

  • Bundle 基本上只是一个包装器围绕HashMap,但它支持打包(即编组)内部映射及其内容
  • ,如果您将一个值放入Bundle中,它会首先解包该内部映射,然后简单地将值放入该地
  • 图解包地图是惰性发生的:只有当您尝试访问它时(例如通过调用bundle.putParcelable()),它才会解包它。即使如此,它也只会解包地图本身,而不会解包其值。仅当您尝试实际访问这些值时(例如使用bundle.getParcelable("key")),它也会解包该值。换句话说,如果您对 Bundle 内的某些内容进行打包,并且您不再访问这些值,则不会发生解包。

所以一般来说:,一个值不能简单地通过将其放入 Bundle 中来进行打包。相反,当将 Bundle 传递给另一个组件(活动或服务;为什么 Android 会这样做,我不知道,因为技术上没有 IPC 发生)或者当它必须被打包时,会发生打包。

I think I've figured it out. I basically spent the entire last day and most of today on debugging through the Android Parcel and Bundle source code, and here is how it works:

  • a Bundle is basically just a wrapper around a HashMap, but it supports to parcel (i.e. marshal) that internal map and its contents
  • if you put a value into a Bundle, it will first unparcel this internal map, and then simply put the value into that map
  • unparceling the map happens lazily: it will only unparcel it if you're trying to access it (e.g. by calling bundle.putParcelable()). Even then, it will only unparcel the map itself, but not its values. Only when you try to actually access these values (e.g. using bundle.getParcelable("key")) it will unparcel the value, too. In other words, if you parcel something that's inside a Bundle, unparcelling will not occur if you never access these values again.

So generally: NO, a value is not parceled simply by putting it into a Bundle. Instead, parcelling happens when passing the Bundle to another component (activity or service; why Android does that, I don't know, since no IPC is happening technically.) or when it otherwise has to be parcelled.

原谅我要高飞 2024-09-22 17:07:45

我认为这会立即发生。我认为性能的提高是因为可序列化需要反射才能工作。我认为这与可序列化和可外部化之间的性能差异相同。

I think it happens right away. And I think the performance increase is because of serializable needed reflection to work. I think it's the same as the performance difference between serializable and externalizable.

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