Lazily restore serialized S7 classes - #746
Draft
hadley wants to merge 2 commits into
Draft
Conversation
hadley
marked this pull request as draft
July 30, 2026 19:01
| if (class == NULL) | ||
| return; | ||
|
|
||
| R_ReleaseObject(class); |
Member
There was a problem hiding this comment.
I don't think we anticipate users creating many class definitions, but it's worth noting that R_ReleaseObject is quite expensive, and if we ever need to we could do something much more efficient here using a protected link list.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #743. Review only the changes relative to
issue-742-shared-class-reference.This replaces the environment-backed class reference from #743 with a per-object external pointer. Its live address is the S7 class object. R serialization clears that address, allowing package classes to be restored without serializing their definitions.
For package classes, the serialized tag contains the package/name identity. On first class access after restoration, S7 resolves the current namespace binding, updates the object dispatch vector, validates that object once, and caches the resolved class. Each object has its own pointer, so every restored object is validated independently. If validation fails, the pointer is cleared so a later access retries rather than caching an invalid restoration.
Classes with
package = NULLcannot be looked up after restoration. Their class definition is therefore retained in one shared environment in the pointer protected field and serialized with their instances. R deduplicates that environment when multiple instances are serialized together. This removes the weak-reference/session-registry machinery from the original prototype and supports restoration in a fresh R session.Benchmarks against #743 (construction timings average two paired runs):
Validation:
devtools::test(): 1,298 passeddevtools::check(): 0 errors; existing test dependency warning and existing/environmental notes onlyair format .git diff --check