Dan Amelang | 8 Apr 01:10

Array question

Hey everyone,

My brain's probably fried from coding for too many consecutive hours,
but this following looks kinda strange to me:

(from function/objects/Array.st):

Array size_: _size value_: _value
[
    self := self new: (SmallInteger value_: _size).
    _oops := _value.
    {
      int i;
      for (i= 0;  i < (int)v__size;  ++i)
        ((oop *)self->v__oops)[i]= *((oop **)v__value)[i];
    }.
]

What is that "_oops := _value." doing?

Dan

Ian Piumarta | 8 Apr 05:29

Re: Array question

Hi Dan,

> (from function/objects/Array.st):
>
> Array size_: _size value_: _value
> [
>     self := self new: (SmallInteger value_: _size).
>     _oops := _value.
>     {
>       int i;
>       for (i= 0;  i < (int)v__size;  ++i)
>         ((oop *)self->v__oops)[i]= *((oop **)v__value)[i];
>     }.
> ]

This is the constructor that the compiler invokes implicitly whenever  
you write an Array literal "#( ... )" in a program.  The argument  
_value points to a C array of oops corresponding to "..." in your  
constant and _size is an int reflecting the number of entries in the  
C array.

> What is that "_oops := _value." doing?

_oops is an instance variable of the receiver, which by line 2 is a  
new Arrray of size _size.  The line in question makes the C array  
containing "..." be the contents of the Array object.

Cheers,
Ian

(Continue reading)

Dan Amelang | 8 Apr 06:06

Re: Array question

On Mon, Apr 7, 2008 at 8:29 PM, Ian Piumarta <piumarta@...> wrote:
>
> > Array size_: _size value_: _value
> > [
> >    self := self new: (SmallInteger value_: _size).
> >    _oops := _value.
> >    {
> >      int i;
> >      for (i= 0;  i < (int)v__size;  ++i)
> >        ((oop *)self->v__oops)[i]= *((oop **)v__value)[i];
> >    }.
> > ]
> >
> > What is that "_oops := _value." doing?
> >
>
>  _oops is an instance variable of the receiver, which by line 2 is a new
> Arrray of size _size.  The line in question makes the C array containing
> "..." be the contents of the Array object.

Sorry to sound clueless, but if the line in question makes the C array
containing "..." be the contents of the Array object, why is the
following "for" loop necessary?

Dan

Michael FIG | 8 Apr 07:01

Re: Array question

Hi,

"Dan Amelang" <daniel.amelang@...> writes:

>> >        ((oop *)self->v__oops)[i]= *((oop **)v__value)[i];

[...]

> Sorry to sound clueless, but if the line in question makes the C array
> containing "..." be the contents of the Array object, why is the
> following "for" loop necessary?

It replaces each _oops element by its dereferenced value, equivalent
to (watch the stars):

   ((oop *)self->v__oops)[i] = *((oop **)self->v__oops)[i];

Don't ask me why _value is being reused instead of the
freshly-allocated _oops.  It would make more sense to me to omit
"_oops := _value", and save 1 line out of the 10,000 quota. ;)

--

-- 
Michael FIG <michael@...> //\
   http://michael.fig.org/    \//

Dan Amelang | 8 Apr 07:28

Re: Array question

On Mon, Apr 7, 2008 at 10:01 PM, Michael FIG <michael@...> wrote:
> Hi,
>
>
>  "Dan Amelang" <daniel.amelang@...> writes:
>
>  >> >        ((oop *)self->v__oops)[i]= *((oop **)v__value)[i];
>
>  [...]
>
>
>  > Sorry to sound clueless, but if the line in question makes the C array
>  > containing "..." be the contents of the Array object, why is the
>  > following "for" loop necessary?
>
>  It replaces each _oops element by its dereferenced value, equivalent
>  to (watch the stars):
>
>    ((oop *)self->v__oops)[i] = *((oop **)self->v__oops)[i];
>
>  Don't ask me why _value is being reused instead of the
>  freshly-allocated _oops.  It would make more sense to me to omit
>  "_oops := _value", and save 1 line out of the 10,000 quota. ;)

Along with saving a line (yea big deal), I think it would have made a
little more sense that way. The reuse of the parameter value was
throwing me off. Seeing the same memory address casted two different
ways and overriding itself all in one line was bizarre for me.
Especially after memory had already been freshly allocated for the
oops array. But to each his own.
(Continue reading)

Ian Piumarta | 8 Apr 09:16

Re: Array question

On Apr 7, 2008, at 10:28 PM, Dan Amelang wrote:

> The reuse of the parameter value was
> throwing me off. Seeing the same memory address casted two different
> ways and overriding itself all in one line was bizarre for me.
> But to each his own.

FWIW, here's the rationale behind the way the code is written.

	Array size_: _size value_: _value
	[
	    self := self new: (SmallInteger value_: _size).

Let's just ignore the fact that new storage is allocated here -- it's  
irrelevant to the alias and casting that is proving to be so confusing.

	    _oops := _value.

That line says: "the contents of the new object share space with the  
template contents generated by the Compiler."  (1)

	    {
	      int i;
	      for (i= 0;  i < (int)v__size;  ++i)
	        ((oop *)self->v__oops)[i]= *((oop **)v__value)[i];

That loop says: "the contents of the new object are derived from the  
template contents by indirecting through each entry in the  
template."  (2)

(Continue reading)

Alessandro Warth | 8 Apr 07:28

Re: Array question


Don't ask me why _value is being reused instead of the
freshly-allocated _oops.  It would make more sense to me to omit
"_oops := _value", and save 1 line out of the 10,000 quota. ;)

Yeah, that looks like an "oops" (sorry, I couldn't resist). First, new: makes _oops point to a freshly-allocated chunk of memory,

Array new: capacity
[
    self := super new.
    size := capacity.
    _oops := self _palloc: size
]

then we make it point elsewhere, creating unnecessary work for the GC.

Cheers,
Alex




--
Michael FIG <michael-y8U0i/lyZOM@public.gmane.org> //\
  http://michael.fig.org/    \//

_______________________________________________
fonc mailing list
fonc-uVco7kAcSAQ@public.gmane.org
http://vpri.org/mailman/listinfo/fonc

<div>
<br><div class="gmail_quote">
<blockquote class="gmail_quote">
Don't ask me why _value is being reused instead of the<br>
freshly-allocated _oops. &nbsp;It would make more sense to me to omit<br>
"_oops := _value", and save 1 line out of the 10,000 quota. ;)</blockquote>
<div>
<br>Yeah, that looks like an "oops" (sorry, I couldn't resist). First, <span>new:</span> makes <span>_oops</span> point to a freshly-allocated chunk of memory,<br><br><div>Array new: capacity<br>[<br>&nbsp;&nbsp;&nbsp; self := super new.<br>&nbsp;&nbsp;&nbsp; size := capacity.<br>&nbsp;&nbsp;&nbsp; _oops := self _palloc: size<br>]<br>
</div>
<br>then we make it point elsewhere, creating unnecessary work for the GC.<br><br>Cheers,<br>Alex<br><br><br>
</div>
<blockquote class="gmail_quote">
<br><br>
--<br>
Michael FIG &lt;<a href="mailto:michael@...">michael@...</a>&gt; //\<br>
 &nbsp; <a href="http://michael.fig.org/" target="_blank">http://michael.fig.org/</a> &nbsp; &nbsp;\//<br><div>
<div></div>
<div class="Wj3C7c">
<br>
_______________________________________________<br>
fonc mailing list<br><a href="mailto:fonc@...">fonc@...</a><br><a href="http://vpri.org/mailman/listinfo/fonc" target="_blank">http://vpri.org/mailman/listinfo/fonc</a><br>
</div>
</div>
</blockquote>
</div>
<br>
</div>
Ian Piumarta | 8 Apr 08:55

Re: Array question

On Apr 7, 2008, at 10:28 PM, Alessandro Warth wrote:

> Yeah, that looks like an "oops" (sorry, I couldn't resist). First,  
> new: makes _oops point to a freshly-allocated chunk of memory,
> then we make it point elsewhere, creating unnecessary work for the GC.

The Compiler tried to create unique contents for any given literal no  
matter how many times that literal appears in the program.  If you  
don't allocate new storage you alias each occurrence of a given  
literal, so

a := #(1 2 3).
b := #(1 2 3).
a at: 1 put: 4.  "(b at: 1) == 4"

which is probably not what you wanted.  The removal of the assignment  
in size_:value_: is the correct fix.  The assignment was accidentally  
left in when the method was copied from st80/Array.st, where no new  
allocation of the contents array is done and aliasing of the contents  
is not a problem because literals are immutable and the above  
scenario becomes illegal.

HTH,
Ian

Ian Piumarta | 8 Apr 08:41

Re: Array question

On Apr 7, 2008, at 10:01 PM, Michael FIG wrote:

> Don't ask me why _value is being reused instead of the
> freshly-allocated _oops.  It would make more sense to me to omit
> "_oops := _value", and save 1 line out of the 10,000 quota. ;)

Oh, now I see.  Good point.  The assignment of _oops should be removed.

Cheers,
Ian


Gmane