From hin-tak.leung at cimr.cam.ac.uk  Tue Sep  4 01:18:04 2007
From: hin-tak.leung at cimr.cam.ac.uk (Hin-Tak Leung)
Date: Tue, 04 Sep 2007 00:18:04 +0100
Subject: [Rd] Consistency of serialize(): please enlighten me
In-Reply-To: <59d7961d0708311249p798300e3i97c12620c93b82d2@mail.gmail.com>
References: <59d7961d0708311245v5f03dd4djca143eb4d5c29180@mail.gmail.com>
	<59d7961d0708311249p798300e3i97c12620c93b82d2@mail.gmail.com>
Message-ID: <46DC962C.7030900@cimr.cam.ac.uk>

I have a couple of ideas - serialize() can store references (and some 
simple assignment are just stored as references until one tries to 
modify part of the copy, i.e. in a copy-on-write manner); ocassionally,
it will also store the package name as an attribute to the class name in
which the class was defined. Maybe neither of this is the case, but what
does a hexdump tell you? (just printing the result of rawToChar() to the
console).

Henrik Bengtsson wrote:
> Forgot...
> 
> On 8/31/07, Henrik Bengtsson <hb at stat.berkeley.edu> wrote:
>> Hi,
>>
>> I am puzzled with serialize().  It comes down generating identical
>> hash codes for (apparently) identical objects using digest::digest(),
>> which in turn relies on serialize().  Here is an example illustration
>> the issue:
>>
>> ser <- function(object, ...) {
>>   list(
>>     names = names(object),
>>     namesRaw = charToRaw(names(object)),
>>     ser = serialize(names(object), connection=NULL, ascii=FALSE)
>>   )
>> } # ser()
>>
>> # Object to be serialized
>> key <- key0 <- list(abc="Hello");
>>
>> # Store results
>> d <- list();
>>
>> # 1. As is
>> d[[1]] <- ser(key);
>>
>> # 2. Set names and redo (hardwired: identical to what's already there)
>> names(key) <- "abc";
>> d[[2]] <- ser(key);
>>
>> # 3. Set names and redo (generic: char->raw->char)
>> key <- key0;
>> names(key) <- sapply(names(key), FUN=function(name) rawToChar(charToRaw(name)));
>> d[[3]] <- ser(key);
>>
>> # All names are identical
>> for (kk in 2:length(d))
>>   stopifnot(identical(d[[1]]$names, d[[kk]]$names));
>>
>> # All raw names are identical
>> for (kk in 2:length(d))
>>   stopifnot(identical(d[[1]]$namesRaw, d[[kk]]$namesRaw));
>>
>> # But, the serialized names differ.
>> print(identical(d[[1]]$ser, d[[2]]$ser));
>> print(identical(d[[1]]$ser, d[[3]]$ser));
>> print(identical(d[[2]]$ser, d[[3]]$ser));
> 
> With R version 2.6.0 Under development (unstable) (2007-08-23 r42614) I get:
> [1] TRUE
> [1] FALSE
> [1] FALSE
> 
> and with R version 2.5.1 Patched (2007-07-19 r42284):
> [1] FALSE
> [1] FALSE
> [1] TRUE
> 
>> So, it seems like there is some extra information in the names
>> attribute that is part of the serialization.  Is it possible to show
>> they differ at the R level?  What is that extra information?
>> Promises...?
>>
>> Please enlighten me.
>>
>> Henrik
>>
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] Consistency of serialize(): please enlighten me
Hin-Tak Leung