?? mem_man.html
字號:
<HTML><HEAD><TITLE>Newmat09 - memory management</TITLE></HEAD><BODY><H2>Memory management - reference counting or status variable?</H2><A HREF="evalx.html"> next</A> -<A HREF="evalx.html"> skip</A> -<A HREF="design.html"> up</A> -<A HREF="index.html"> start</A><P>Consider the instruction<PRE> X = A + B + C;</PRE>To evaluate this a simple program will add <TT>A</TT>to <TT>B</TT> putting thetotal in a temporary <TT>T1</TT>. Then it will add <TT>T1</TT>to <TT>C</TT> creatinganother temporary <TT>T2</TT> which will be copied into <TT>X</TT>.<TT>T1</TT> and <TT>T2</TT> willsit around till the end of the execution of the statement and perhapsof the block. It would be faster if theprogram recognised that <TT>T1</TT> was temporary and storedthe sum of <TT>T1</TT>and <TT>C</TT> back into <TT>T1</TT> instead of creating <TT>T2</TT>and then avoided thefinal copy by just assigning the contents of <TT>T1</TT> to<TT>X</TT> rather than copying.In this case there will be no temporaries requiring deletion.(More precisely there will be a header to be deleted but nocontents).<P>For an instruction like<PRE> X = (A * B) + (C * D);</PRE>we can't easily avoid one temporary being left over, so wewould like this temporary deleted as quickly as possible.<P>I provide the functionality for doing all this by attaching a statusvariable to each matrix. This indicates if the matrix istemporary so that its memory is available for recycling or deleting.Any matrix operation checks the status variables of the matricesit is working with and recycles or deletes any temporary memory.<P>An alternative or additional approach would be to use<I>reference counting and delayed copying</I> - also knownas <I>copy on write</I>. If a program requests amatrix to be copied, the copy is delayed until an instruction isexecuted which modifies the memory of either the original matrixor the copy. If the original matrix is deleted before eithermatrix is modified, in effect, the values of the original matrix aretransfered to the copy without any actual copying taking place.This solves the difficult problem of returning anobject from a function without copying andsaves the unnecessary copying in the previous examples. <P>There are downsides to the delayed copying approach. Typically, fordelayed copying one uses a structure like the following:<PRE> Matrix | +------> Array Object | | | +------> Data array | | | +------- Counter | +------ Dimension information</PRE>where the arrows denote a pointer to a data structure. If one wantsto access the <I>Data array</I> one will need to track through twopointers.If one is going to write, one will have to check whether one needs tocopy first. This is not important when one is going to access thewhole array, say, for a add operation. But if one wants to access justa single element, then it imposes a significant additional overheadon that operation.Any subscript operation would need to check whether an update wasrequired - even read since it is hard for the compiler to tellwhether a subscript access is a read or write.<P>Some matrix libraries don't bother to do this. So if you write<TT>A = B;</TT> and then modify an element of one of <TT>A</TT>or <TT>B</TT>, then the same element of the other is also modified.I don't think this is acceptable behaviour.<P>Delayed copy does not provide the additional functionalityof my approach but I suppose it would be possible to have bothdelayed copy and tagging temporaries.<P>My approach does not automatically avoid all copying. In particular,you need use a special technique to return a matrix from a functionwithout copying.<P><A HREF="evalx.html"> next</A> -<A HREF="evalx.html"> skip</A> -<A HREF="design.html"> up</A> -<A HREF="index.html"> start</A></BODY></HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -