-
Notifications
You must be signed in to change notification settings - Fork 990
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
61 additions
and
56 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
\name{.internal.selfref} | ||
\alias{.internal.selfref} | ||
\title{Internal Self-Reference Attribute in data.table} | ||
\description{ | ||
The \code{.internal.selfref} attribute is an internal mechanism used by \code{data.table} to optimize memory management and performance. It acts as a pointer that allows \code{data.table} objects to reference their own memory location. While the \code{.internal.selfref} attribute may appear to always point to \code{NULL} when inspected directly, this is a result of its implementation in R's memory management system. The true significance of this attribute lies in its role in supporting reference semantics, which enables efficient in-place modification of \code{data.table} objects without unnecessary copying. | ||
The \code{.internal.selfref} attribute is deliberately structured so that \code{identical()} checks return \code{TRUE} for two \code{data.table} objects with identical contents, even when their attributes point to the same memory address. This behavior is achieved by storing the actual self-reference pointer in the \code{prot} part of an external pointer, wrapped in another external pointer to avoid creating visible reference loops. When a \code{data.table} is duplicated, its memory address changes, making it possible to detect the copy and handle it accordingly. | ||
} | ||
\details{ | ||
The \code{.internal.selfref} attribute is a pointer that ensures that \code{data.table} objects can be modified by reference without redundant memory allocation. This avoids copying when performing in-place modifications such as adding or updating columns, filtering rows, or performing joins. | ||
Key details about the \code{.internal.selfref} attribute: | ||
\itemize{ | ||
\item \code{p=NULL} is used instead of \code{R_NilValue}, allowing \code{data.table} to detect objects loaded from disk and ensure correct behavior. | ||
\item Wrapping the self-reference in another external pointer prevents infinite loops during \code{object.size} calculations. | ||
\item If the attribute is removed or corrupted, the next operation involving \code{:=} triggers a warning and creates a new self-reference after copying. | ||
} | ||
The \code{_selfrefok} function verifies the validity of the \code{.internal.selfref} attribute. It checks whether the attribute correctly references the current \code{data.table} object by comparing memory addresses. If the attribute is invalidated (e.g., due to duplication or corruption), \code{_selfrefok} triggers a repair mechanism to restore reference semantics, ensuring that in-place operations remain efficient. | ||
} | ||
\value{ | ||
The \code{.internal.selfref} attribute is an internal implementation detail and does not produce a value that users would typically interact with. It is invisible during regular \code{data.table} operations. | ||
} | ||
\seealso{ | ||
\code{\link{data.table}}, \code{\link{setkey}}, \code{\link{merge}}, \code{\link{[.data.table}} | ||
} | ||
\examples{ | ||
# Create a data.table | ||
dt <- data.table(A = 1:5, B = letters[1:5]) | ||
# Trace memory to check for reference semantics | ||
tracemem(dt) # Outputs the memory address of the data.table | ||
# Perform an in-place operation | ||
dt[, C := A * 2] # Add a new column in place | ||
# Verify no copying has occurred | ||
# (The output of tracemem should show no memory change) | ||
# Example of losing .internal.selfref (hypothetical, for illustration) | ||
dt_copy <- copy(dt) # Copy the data.table | ||
.Internal(inspect(dt_copy)) # Shows .internal.selfref attribute no longer matches | ||
} | ||
\keyword{internal} |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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