[Rd] allocVector bug ?

Vladimir Dergachev vdergachev at rcgardis.com
Wed Nov 1 23:20:09 CET 2006

Hi all, 

  I was looking at the following piece of code in src/main/memory.c, function 
allocVector :

    if (size <= NodeClassSize[1]) {
	node_class = 1;
	alloc_size = NodeClassSize[1];
    else {
	node_class = LARGE_NODE_CLASS;
	alloc_size = size;
	for (i = 2; i < NUM_SMALL_NODE_CLASSES; i++) {
	    if (size <= NodeClassSize[i]) {
		node_class = i;
		alloc_size = NodeClassSize[i];

It appears that for LARGE_NODE_CLASS the variable alloc_size should not be 
size, but something far less as we are not using vector heap, but rather 
calling malloc directly in the code below (and from discussions I read on 
this mailing list I think that these two are different - please let me know 
if I am wrong).

So when allocate a large vector the garbage collector goes nuts trying to find 
all that space which is not going to be needed after all.

I made an experiment and replaced the line alloc_size=size with alloc_size=0.

R compiled fine (both 2.4.0 and 2.3.1) and passed make check with no issues 
(it all printed OK).

Furthermore, all allocVector calls completed in no time and my test case run 
very fast (22 seconds, as opposed to minutes). 

In addition, attach() was instantaneous which was wonderful.

Could anyone with deeper knowledge of R internals comment on whether this 
makes any sense ?

                           thank you very much !

                                        Vladimir Dergachev

More information about the R-devel mailing list