How to quickly delete an interface in a dynamic array? Delphi 2007

Hello all,

I have a very large dynamic array that contains a list of
interfaces. I want to delete one interface from the list quickly
and the list must remain sorted.

What's the best/fastest/most elegant way to do it? Everything
I can think of involves Move() and pointer arithmetic.

I was hoping for an elegant array.delete() function but the
helpfile lets me down.

My current sloooow method is to move the element that I want
to delete to the end of the array (consecutively swapping it with
the next element) and then calling setlength() to truncate the
array, hoping/praying that the compiler will properly call
_release() on the item that's going to be chopped off.

I can't verify if the compiler does that, by the way, and the
documentation is... well, not really helpful again (sigh).

The method is unbearably slow because of the many
unnecessary addref/release calls involved. I'm really
tempted to use a move() if I don't find anything better.













-- 
Arthur Hoornweg

(In order to reply per e-mail, please just remove the ".net"
  from my e-mail address. Leave the rest of the address intact
  including the "antispam" part. I had to take this measure to
  counteract unsollicited mail.)
0
Arthur
1/5/2009 10:31:36 AM
embarcadero.delphi.win32 2183 articles. 0 followers. Follow

7 Replies
521 Views

Similar Articles

[PageSpeed] 10

"Arthur Hoornweg" <antispam.hoornweg@casema.nl.net> schreef in bericht 
news:62773@forums.codegear.com...
> Hello all,
>
> I have a very large dynamic array that contains a list of
> interfaces. I want to delete one interface from the list quickly
> and the list must remain sorted.
>
> What's the best/fastest/most elegant way to do it? Everything
> I can think of involves Move() and pointer arithmetic.
>
> I was hoping for an elegant array.delete() function but the
> helpfile lets me down.
>
> My current sloooow method is to move the element that I want
> to delete to the end of the array (consecutively swapping it with
> the next element) and then calling setlength() to truncate the
> array, hoping/praying that the compiler will properly call
> _release() on the item that's going to be chopped off.
>
> I can't verify if the compiler does that, by the way, and the
> documentation is... well, not really helpful again (sigh).
>
> The method is unbearably slow because of the many
> unnecessary addref/release calls involved. I'm really
> tempted to use a move() if I don't find anything better.
>

So the array contains pointers. It doesn't really matter.
When you delete an item you will have to move the remainder down one 
position since you want to keep it sorted. Move procedure is your best 
option since it can overwrite its own area and doesn't do any checking.
SetLength(array,length(array)-1) will just release the obsolete 4 bytes at 
the end. It will not move the array around in memory unless it has a 
reference count>1. The release of this last position in the array is a bit 
of an overhead. Maybe you can change your code so that it can cope with a 
NIL element. But then, if you can, there may not be a need to move that NIL 
to the end of the array in the first place.
Freeing the object pointed to (an interface in your case) will be your 
responsibility I guess.
Tom
0
Tom
1/5/2009 2:04:01 PM
Arthur Hoornweg wrote:
> I have a very large dynamic array that contains a list of
> interfaces. I want to delete one interface from the list quickly
> and the list must remain sorted.
> 
> What's the best/fastest/most elegant way to do it? Everything
> I can think of involves Move() and pointer arithmetic.

Yep. Move is the way to go. No need for pointer arithmetic, though.

> I was hoping for an elegant array.delete() function but the
> helpfile lets me down.

Arrays aren't objects. They don't have methods. Maybe you're interested 
in a list?

> My current sloooow method is to move the element that I want
> to delete to the end of the array (consecutively swapping it with
> the next element) and then calling setlength() to truncate the
> array, hoping/praying that the compiler will properly call
> _release() on the item that's going to be chopped off.

You don't have to hope or pray. It always finalizes the removed 
elements. For that same reason, you don't need to "clear" the array 
before it goes out of scope.

> I can't verify if the compiler does that, by the way, and the
> documentation is... well, not really helpful again (sigh).

The _compiler_ doesn't do it. SetLength does. It's _DynArraySetLength in 
System.pas, and the line you're interested in is the one that calls 
FinalizeArray.

> The method is unbearably slow because of the many
> unnecessary addref/release calls involved. I'm really
> tempted to use a move() if I don't find anything better.

I demonstrate exactly how to do that.

http://pages.cs.wisc.edu/~rkennedy/array-delete

Destroy the element you're interested in, use Move to slide everything 
over to fill that spot, and then clear the last element such that when 
SetLength calls FinalizeArray on that last element, it thinks there's 
nothing to do (since the final element will also be the penultimate 
element with no change in its reference count).

-- 
Rob
0
Rob
1/5/2009 3:15:55 PM
Tom deNeef wrote:
> "Arthur Hoornweg" <antispam.hoornweg@casema.nl.net> schreef in bericht 
> news:62773@forums.codegear.com...
>> I have a very large dynamic array that contains a list of
>> interfaces. I want to delete one interface from the list quickly
>> and the list must remain sorted.
>>
>> What's the best/fastest/most elegant way to do it? Everything
>> I can think of involves Move() and pointer arithmetic.
>>
>> I was hoping for an elegant array.delete() function but the
>> helpfile lets me down.
>>
>> My current sloooow method is to move the element that I want
>> to delete to the end of the array (consecutively swapping it with
>> the next element) and then calling setlength() to truncate the
>> array, hoping/praying that the compiler will properly call
>> _release() on the item that's going to be chopped off.
>>
>> I can't verify if the compiler does that, by the way, and the
>> documentation is... well, not really helpful again (sigh).
>>
>> The method is unbearably slow because of the many
>> unnecessary addref/release calls involved. I'm really
>> tempted to use a move() if I don't find anything better.
> 
> So the array contains pointers. It doesn't really matter.

It matter quite a bit, actually, because those aren't just pointers. 
They're interface references.

> When you delete an item you will have to move the remainder down one 
> position since you want to keep it sorted. Move procedure is your best 
> option since it can overwrite its own area and doesn't do any checking.
> SetLength(array,length(array)-1) will just release the obsolete 4 bytes at 
> the end.

The trouble is that after you call Move, those last four bytes aren't 
obsolete. They're also the same as the next-to-last four bytes -- they 
still refer to a valid interface -- so you have two references where you 
used to have one, and you haven't told that object to adjust its 
reference count accordingly. When you call SetLength, it will call 
_Release on that last item, which might destroy it.

> It will not move the array around in memory unless it has a 
> reference count>1. The release of this last position in the array is a bit 
> of an overhead. Maybe you can change your code so that it can cope with a 
> NIL element. But then, if you can, there may not be a need to move that NIL 
> to the end of the array in the first place.
> Freeing the object pointed to (an interface in your case) will be your 
> responsibility I guess.

With interfaces, freeing the object is the object's own responsibility, 
when its reference count reaches zero.

-- 
Rob
0
Rob
1/5/2009 3:21:39 PM
> I have a very large dynamic array that contains a list of
> interfaces. I want to delete one interface from the list quickly
> and the list must remain sorted.

Apart from the solutions given, depending on your exact needs, the best 
solution might be to eliminate the problem rather than solve it: 
deleting in a large array is intrinsically inefficient, so it's not 
something you want to do in the first place.

For instance:
* If you do a lot of random insertions/deletions, move to a chained list 
structure, or any other structure where deletion can be performed in 
constant time.
* If you can live with holes in the array, just nil the reference. When 
the array becomes too sparse, pack it.

Eric
0
Eric
1/5/2009 4:21:31 PM
Arthur Hoornweg wrote:

> Hello all,
> 
> I have a very large dynamic array that contains a list of
> interfaces. I want to delete one interface from the list quickly
> and the list must remain sorted.
> 
> What's the best/fastest/most elegant way to do it? Everything
> I can think of involves Move() and pointer arithmetic.

With an array there is no other way. But you could use a TInterfaceList
instead. That does in fact do the same when you delete an item, but it
is all hidden from view and correctly handles all reference counting
issues.



-- 
Peter Below (TeamB)  
Don't be a vampire (http://slash7.com/pages/vampires), 
use the newsgroup archives :
http://www.tamaracka.com/search.htm
http://groups.google.com
0
Peter
1/5/2009 6:15:29 PM
Peter Below wrote:
> With an array there is no other way. But you could use a TInterfaceList
> instead. 

I have moved 100% this way, for all cases, interfaces, objects, etc 
should all be in TLists and never in an Array. It's just too much ugly 
code to write otherwise.  Write your Move(..) code wrong and it's lots 
of really fun hours spent in the debugger heading your way.


Warren
0
Warren
1/5/2009 6:30:22 PM
Peter Below wrote:

> With an array there is no other way. But you could use a TInterfaceList
> instead. That does in fact do the same when you delete an item, but it
> is all hidden from view and correctly handles all reference counting
> issues.


Thanks, I'll do that.

My reason for using a dynamic array in the fist place was
the ease with which I can swap entries.



-- 
Arthur Hoornweg

(In order to reply per e-mail, please just remove the ".net"
  from my e-mail address. Leave the rest of the address intact
  including the "antispam" part. I had to take this measure to
  counteract unsollicited mail.)
0
Arthur
1/6/2009 2:06:07 PM
Reply:

Similar Artilces:

Migrate from Delphi 2007 for Win32 to Delphi XE
we use Delphi 2007 for Win32 to support legacy (32Bit) OWL-based pascal applications (yes i know it was a mistake not to switch to VCL 15 years ago). could our applications still be opened and compiled with Delphi XE? The existing projects are all plain Pascal-Code, coming back from the times of Turbo Pascal for Windows and later on Borland Pascal. Are there any improvements we could profit from (i.e IDE, Debugger)? Thanks Andrej > {quote:title=Andrej Dimic wrote:}{quote} > could our applications still be opened and compiled with Delphi XE? I'm not sure, but I guess ...

Delphi 2007 to Delphi 7
I've written a class in Delphi 2007 that is not supported in Delphi 7. What would be the best way to achive what I've done in Delphi 2007 in Delphi 7? Thanks, Tom type BondConstants = class { Bond Types } type BondType = record const TREASURY = 3; AGENCY = 0; CORP = 1; MUNI = 2; SBA = 5; MBS = 4; CMO = 6; end; { Day Count Methods } type DayCount = record const ACTUAL_360 = 2; ACTUAL_365 = 1; ACTUAL_ACTUAL = 1; d30_360 = 0; ...

Delphi 4 to Delphi 2007
Hello, I will have to port a D4 application (with source) to D2007. what kind of problem could I face ? I will have to go to customer site tommorow to analyse its source code to quote the work, what should I care of to hestimate the porting time ? Thanks John Terry wrote: > Hello, > I will have to port a D4 application (with source) to D2007. > what kind of problem could I face ? > I will have to go to customer site tommorow to analyse its source code > to quote the work, what should I care of to hestimate the porting time ? You can probably do it by just changi...

Delphi 2007 RTL in Delphi 7
I read somewhere that it is possible to use Delphi 2007's (or for that matter Delphi 2006's) RTL in Delphi 7 by just recompiling the source of RTL. Is this really possible? What are the benefits that we can derive using a higher version's RTL? TIA Yogi Yang Yogi Yang wrote: > I read somewhere that it is possible to use Delphi 2007's (or for > that matter Delphi 2006's) RTL in Delphi 7 by just recompiling the > source of RTL. If it actually compiles in Delphi 7, I guess it would be possible. OTOH, if one has Delphi 2007 already, I don't s...

is there a Delphi 2007
Would be nice to have a bundle of the latest Delphi packages. Gilbert Padilla wrote: > Would be nice to have a bundle of the latest Delphi packages. > I expect Delphi 2007 will only be on sale until the full RAD Studio 2009 edition is published, including Delphi 2009.net. W ...

Delphi 2010 w Delphi 2007
I have D2007 installed on my laptop (XP sp3 running on dual core Athlon). Can I install D2010 on this machine without it affecting D2007? Will D2010 affect D2007 in any way? Thanks, Randall Carpenter > {quote:title=Randall Carpenter wrote:}{quote} > I have D2007 installed on my laptop (XP sp3 running on dual core Athlon). > Can I install D2010 on this machine without it affecting D2007? Will D2010 > affect D2007 in any way? Won't hurt a thing. I have D7, RAD Studio 2007, RAD Studio 2009, and RAD Studio 2010 all on my desktop system and they coexist fine. Jus...

Migrate from Delphi 2007 to Delphi 2010
Hi All, Thanks in advance for your help. Below is my query, Currently I am using Delphi 2007 and i want to migrate to Delphi 2010. 1. What all things i need to take care while doing this? 2. What all third party components will get impacted? 3. Any known issues in Delphi 2010 which might impact the cause? 4. Any changes in database operation required as i am using Oracle? Hope I have post this query under correct category. If not sorry for the trouble and could you please suggest me the correct category for this? Looking forward for your response. Have a great day. Th...

Moving from Delphi 7 to Delphi 2007
Is there any compelling reason to move (quite a large project) from Delphi 7 to Delphi 2007? Specifically, is there better Vista/Win7/Win8 integration provided by Delphi 2007? (In the project I am already using a custom manifest file for Vista/Win7 etc.. and not the std XPMan component) Does 2007 add any new bugs to the scene? Thanks Rael Rael wrote: > Is there any compelling reason to move (quite a large project) from > Delphi 7 to Delphi 2007? If you are going to upgrade, why not upgrade to something newer? > In the project I am already using a custom man...

Delphi 2007 to Delphi 2009 Conversion
CharArrayPtr = ^CharArray; CharArray = array[0..MaxInt-1] of Char; How to convert to Delphi 2009? Bill Bill Miller wrote: > CharArrayPtr = ^CharArray; > CharArray = array[0..MaxInt-1] of Char; > > How to convert to Delphi 2009? > > Bill it depends heavelly on how it is used fearther down in your project and what you want to convert it to. I you want to simple convert the declarations ignoring UNicode altogether then the only think you have to change is the CharArray declaratin from a Char to an AnsiChar eg CharArrayPtr = ^CharArray; CharArray = array[0..MaxInt-1] of AnsiChar; //----------- this changed This Shoold be enough that the procedures using it and are expecting ANSI characters in the string will work with out any changes. regards Yannis. -- "The difference between pornography and erotica is lighting." -- Gloria Leonard > it depends heavelly on how it is used fearther down in your project and > what you want to convert it to. > > I you want to simple convert the declarations ignoring UNicode > altogether then the only think you have to change is the CharArray > declaratin from a Char to an AnsiChar eg > > CharArrayPtr = ^CharArray; > CharArray = array[0..MaxInt-1] of AnsiChar; //----------- this changed > > This Shoold be enough that the procedures using it and are expecting > ANSI characters in the string will work with out any changes. Hi Ya...

Delphi 2009 alongside Delphi 2007
Hi all, I am about to install Delphi 2009 on the same development system as Delphi 2007, before I embark is there anything I should be aware of so as to not break my Delphi 2007 installation and projects ? Thanks JT John Taylor wrote: > Hi all, > > I am about to install Delphi 2009 on the same development system > as Delphi 2007, before I embark is there anything I should be aware > of so as to not break my Delphi 2007 installation and projects ? As long as you install into a separate folder there is no problem with the two versions getting into each other...

delphi Win32 using delphi .NET dll
Hi, I'm trying to use a delphi.NET dll in delphi.WIN32. I am currently using CodeGear Delphi 2007 with version2(base version) of .NET I can get the dll to import into the WIN32 application the only problem is when i include things such as: "using Classes,DateUtils, SysUtils" in the .NET dll the win32 application will instantly hang when any of the dll functions are called. Any help would be great thanks. Also I have tried this example and it also crashes for me? http://cc.embarcadero.com/Item/22688 -Braden I also found this.. "The problem is that, wehn you instal...

Converting Delphi for Win32 to Delphi .Net(Prism)
Hi, I am currently migrating a project from Delphi for Win32 to Delphi.net. Part of my code currently goes into a directory and pulls out a random file from this directory and loads the contents of the file for me. This code doesn't seem to work in Delphi.Net. It uses PString and a number of functions in SysUtils that don't seem to be present in Delphi.net's SysUtils file. If anyone can help me please, it would be greatly appreciated! Many thanks, Jonathan Mackey Jonathan Mackey a écrit : > I am currently migrating a project from Delphi for Win32 to &...

Code Porting
Hi people, Basically, I am moving an app from Delphi 5 to Delphi 2009 which is all great (yeah right!) and I am having a couple of issues with some thirdparty components that we have the source code for. Being that I wanted to make the task as easy as possible, I was able to download the component in Delphi 2007 format. I have now managed to port that to 2009 so it compiles only with warnings and no errors. So all is good at this moment. Now I am trying to install the component into the IDE by using the install menu option from the project manager. This is where I run into errors. ...

Delphi.NET loading Delphi.Win32 Driver
Hi, What I'm trying to do is marshal an array of cardinal (or integer) back into managed memory from a win32 dll. I know how to pass managed memory into a win32 dll {code} var aa : array of Integer; Buffer : IntPtr; begin SetLength(aa,2); aa[0] := 1; aa[1] := 80; if not Supports(ExtractFilePath(Application.ExeName)+'Win32_Library\SDK_Driver.Win32.io', TypeOf(IMyFunctions), MyFunctions) then Exit; //loads the driver into memory. MyFunctions contains the method names found in the SDK_Driver. Buffer := Marshal.AllocHGlobal(2 * {Marshal.SystemDefaultC...

Migration from Delphi 2007 Rad to Delphi XE Where is Quickreports
Have installed and finally registered Delphi Xe, I am looking for Quickreports but am unable to find it anywhere, I asked the Distributer and was told its packaged in Delphi XE, Well I should have gone to specsavers......cos I cant see it any where ? > {quote:title=Colin Coleman wrote:}{quote} > Have installed and finally registered Delphi Xe, I am looking for Quickreports but am unable to find it anywhere, I asked the Distributer and was told its packaged in Delphi XE, Well I should have gone to specsavers......cos I cant see it any where ? Hello Colin, Delphi X...

can i use delphi 2007 bpls in delphi 2009
can i use delphi 2007 bpls in delphi 2009 ? "Gaetan SIRY" skrev i meddelelsen news:8703@forums.codegear.com... > can i use delphi 2007 bpls in delphi 2009 ? No. -- Finn Tolderlund The compatiblity between D2006 and D2007 was unique. Normaly they can't be compatible because new version enhance/change the API. And D2009 defines String = Unicode where older version has String = Ansistring so D200x/Dx-BPL are 200% incompatible with D2009. ...

Can I update my Delphi 2007 lincense to Delphi 2009?
Hi all!! I have 12 lincense of Delphi 2007. When I bought these licenses to your partner told us that we could update to Delphi 2009 free. But, now I try to do that and I can't do. Are there any reason for that? Can I update my version of Delphi? > {quote:title=Oscar Estevez wrote:}{quote} > Hi all!! > > I have 12 lincense of Delphi 2007. When I bought these licenses to your partner told us that we could update to Delphi 2009 free. But, now I try to do that and I can't do. > Are there any reason for that? > > Can I update my version of Delphi? ...

Delphi and Delphi for .Net
It seems that Delphi for .Net is slower than Delphi Win32 native applicaiton. I would like to know is it true all .Net application is slower than Win32 native applicaiton or it is Delphi for .Net only. Your information is great appreciated, Inung On 2011-06-21 18:20:17 +0100, Inung Huang said: > It seems that Delphi for .Net is slower than Delphi Win32 native applicaiton. > I would like to know is it true all .Net application is slower than > Win32 native applicaiton or it is Delphi for .Net only. If you are only running the code in the application once then, yes, yo...

What we need to learn as we migrate from Delphi 2007 to Delphi 2012
As we prepare to move from Delphi 2007 to the Delphi of next summer/fall, and 64bit some day, we are looking for a list of improvements to Delphi (from IDE to the language) that have been done from Delphi 2007 to Delphi XE, as well as any planned ones for the next version. We understand the daunting migration to Unicode that awaits us but know that we have missed some "new and improved" items where we have to get all our developers up to speed on that may not be related to Unicode. So, we will need to plan some training before we jump in. Is there such a list out ...

How to use a delphi XE3 obj file in delphi 2007?
Hi, I'm migrating an application from delphi 2007 to delphi XE3 and I would like to do it in several steps. To do this I need to use a part of my new code (XE3) in the current delphi 2007 application. So I want to use the .obj file from my XE3 file. I know I have to use the {$L 'Filename'} and I know that I have to declare the functions that I want to use in external in my D2007 project. I wanted to make an easy and simple example to see how it works with only 1 function 'hello world' compiled in obj with XE3 but I can't find the way to use it in D2007. In D200...

Memory usage in Delphi 2007 vs. Delphi 5
Going from Delphi 5 to Delphi 2007 and finding something way unexpected. The reportedly *same code, and same data,* uses about 104 MB in Delphi 5 and about 400 MB in Delphi 2007 according to the Windows Task Manager. Now, I know that it is not the most accurate way of measuring memory usage, but it gives us an idea of the proportion of memory use in Delphi 2007. Before we go deeper, we just created 20,000 objects and added them to an object list in Delphi 5 and 2007. The result in memory use still looks like 50 MB (Delphi 5) to 178 MB (Delphi 2007). We've downloaded ...

Delphi 2007 to Delphi XE2 paint order change
Hello all, I have a program here that does some very specific work in the paint handlers and requires that the controls are painted from bottom to top. This worked just fine under Delphi 2007 but now that we have migrated to Delphi XE2, it no longer works. Tracing the paint order, we discovered that there are cases when the bottom most component (the form for instance) is drawn last instead of first, which means that the painting code we do in the program gets a black area for the bottom most control instead of its background. This was observed with D2007 and DXE2 on the same c...

Win32 program: Delphi 7 vs Delphi XE5
How is a D7 Win32 program compared to a Delphi XE5 one in terms of stability and performance? Is Delphi XE5 good enough for a big ERP project with several DLL's and hundreds of units and forms? Thanks in advance Am 26.12.2013 15:02, schrieb lior ilan: > How is a D7 Win32 program compared to a Delphi XE5 one in terms of stability > and performance? > Is Delphi XE5 good enough for a big ERP project with several DLL's and > hundreds of units and forms? > Thanks in advance > Hello, XE5 has increased functionality. Stability seems to be ok for most ...

Restore Delphi 2007 dbxpress after testing Delphi 2009
Over on another thread I mentioned that I was having problems with dbxpress after testing Delphi 2009. It started that I noticed that Delphi 2007 was compiling against the newer version of dbxpress drivers (dbxmys.dll, rather than dbxmys30.dll). This was spotted after testing an application on a colleagues machine that had the older driver, but not the newer. The application was "pure" 2007. This continued after uninstalling Delphi 2009. It was suggested that Delphi 2009 had overwritten my +dbxdrivers.ini+ file - and looking at it that looks like the problem. It was also...

Web resources about - How to quickly delete an interface in a dynamic array? Delphi 2007 - embarcadero.delphi.win32

Interface - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

PayPal for iOS updated w/ completely new iPhone 6-optimized interface, much more
At long last PayPal has updated its iOS app with a completely redesigned interface that optimizes the experience for iPhone 6/iPhone 6s and 6 ...

Waze 4.0 unveiled with revamped user interface
... post, Waze has uploaded a video straight to YouTube to tell us what’s new. Basically, we are looking at a complete refresh to the user interface, ...

LibreOffice 5.1 reorganizes user interface, reduces start-up times
... 5.1 32-bit . It comes just under six months after version 5.0 debuted back in August yet unveils some major changes to the user interface as ...

ERP software gets modern data, mobile and social user interface
The user interface is one area where ERP software vendors are differentiating themselves, according to a Technology Evaluation Centers analyst. ...

ERP software gets modern data, mobile and social user interface
... tied to its mainframe-era roots. But in recent years, ERP vendors have tried mightily to overcome that reputation by modernizing the user interface, ...

LibreOffice 5.1 Offers Reorganized User Interface for Its Apps
LibreOffice 5.1, the latest iteration of the open-source office suite, adds new user interface elements. We examine highlights of the release. ...

AMD is supposedly planning a 32-core CPU with an eight-channel DDR4 interface
AMD may be planning a new 32-core server CPU once Zen launches but whether that's a good thing or not depends on whether its an Intel bunker ...

Hints Of Apple's Futuristic iPhone Interface Revealed
The US Patent Office has released a batch of patents assigned to Apple, and one of them details a new way of identifying yourself and allowing ...

Where is that sound coming from? Signal Interface’s acoustic imager will show you
... industrial site pinpoints sound coming from a passing truck as well as from equipment on the other side of a buildling. (Credit: Signal Interface ...

Resources last updated: 3/10/2016 8:02:29 AM