[comp.sys.mac.programmer] NewPtr

freek@fwi.uva.nl (Freek Wiedijk) (12/21/89)

Why is the following (Turbo) Pascal program so slow?

  program Slow;
  {$R-}{$I-}{$U-}
  uses Memtypes,QuickDraw,OSIntf;
  var
   s,g: Size;
   p: Ptr;
  begin
   MaxApplZone;
   s:=CompactMem(MaxMem(g));
   p:=NewPtr(s);
   if MemError=memFullErr then SysBeep(2)
  end {Slow}.

Why does APDA Smalltalk start so slowly?
Why does the NewPtr sometimes fail (i.e. the program beeps)?
Why doesn't MaxMem compact the heap (in contradiction with IM-II-38)?
Why is a Mac better than a PC and a NeXT? :-)

--
Freek "the Pistol Major" Wiedijk                  Path: uunet!fwi.uva.nl!freek
#P:+/ = #+/P?*+/ = i<<*+/P?*+/ = +/i<<**P?*+/ = +/(i<<*P?)*+/ = +/+/(i<<*P?)**

chewy@apple.com (Paul Snively) (12/21/89)

In article <299@fwi.uva.nl> freek@fwi.uva.nl (Freek Wiedijk) writes:
> Why is the following (Turbo) Pascal program so slow?
> 
>   program Slow;
>   {$R-}{$I-}{$U-}
>   uses Memtypes,QuickDraw,OSIntf;
>   var
>    s,g: Size;
>    p: Ptr;
>   begin
>    MaxApplZone;
>    s:=CompactMem(MaxMem(g));
>    p:=NewPtr(s);
>    if MemError=memFullErr then SysBeep(2)
>   end {Slow}.

Because you asked it to be.  First you're asking for the largest 
application zone possible in your environment, then you're asking it how 
much space might be available if it were to be compacted, then you're 
compacting it (think about this; it's pretty slow) then you're asking it 
to allocate a pointer that's as large as what CompactMem returned (also 
slow because it, too, may have to shuffle memory around).

In article <299@fwi.uva.nl> freek@fwi.uva.nl (Freek Wiedijk) writes:
> Why does APDA Smalltalk start so slowly?

Because Smalltalk is a VERY large system which is only semi-compiled; when 
you boot it, you load a little bytecode interpreter, which then loads a 
large image file, and that completes the boot, which includes stuff like 
handling virtual memory, which is itself a slow process.

In article <299@fwi.uva.nl> freek@fwi.uva.nl (Freek Wiedijk) writes:
> Why does the NewPtr sometimes fail (i.e. the program beeps)?

There's a certain amount of overhead for creating blocks in the heap (for 
example, blocks take a minumum of 12 physical bytes, even if you allocate 
a block of zero bytes).  Also, the memory manager tries to at least word 
align, if not longword align, blocks.  If you were to subtract some 
constant from s before trying to do a NewPtr of it, it would probably 
work, if the constant were something like, say, 24 or so.

In article <299@fwi.uva.nl> freek@fwi.uva.nl (Freek Wiedijk) writes:
> Why doesn't MaxMem compact the heap (in contradiction with IM-II-38)?

Are you SURE that it doesn't?

In article <299@fwi.uva.nl> freek@fwi.uva.nl (Freek Wiedijk) writes:
> Why is a Mac better than a PC and a NeXT? :-)

The answer to both is outside the scope of this posting. :-)

__________________________________________________________________________
Just because I work for Apple Computer, Inc. doesn't mean that they 
believe what I believe or vice-versa.
__________________________________________________________________________
C++ -- The language in which only friends can access your private members.
__________________________________________________________________________