This invention relates to a data structure and more particularly to a high performance data structure for an immutable list.
In object-oriented computer programming, an immutable object is an object whose state cannot be modified after it is created. Immutable objects can be useful in multi-threaded applications, where different threads will access objects simultaneously. Multiple threads can act on the data represented by an immutable object without the concern that the data will be changed by other threads. Immutable objects are therefore considered to be more “thread-safe” than mutable objects. Moreover, immutable objects do not need to be locked, and so the overhead of enforcing mutual exclusion (i.e., preventing concurrent use of a common resource in a multithreaded environment) is avoided.
An immutable list is an ordered collection of elements. The state of the immutable list, defined as order and identity of its elements, cannot be modified. However, attributes of elements inside the list can still be modified as long as these elements are mutable objects. The immutability of the list can be achieved in two ways, firstly by preventing any operation that modifies the state of the list using compile-time constructs, or secondly, by defensive copying of the entire list at run-time (copy-on-write). Using the copy-on-write technique, when a thread modifies the list through a particular reference, the system makes a real copy and sets the reference to refer to the new copy. The other threads are unaffected, because they still refer to the original list. The copy-on-write technique blends the advantages of mutable and immutable lists. A classic example of an immutable list is an instance of the Java String class.
Current logging and security applications require huge thread-safe collections and fast algorithms for comparing them. When a list contains a large amount of data, the cost of a few changes might become prohibitive since a full copy of the list has to be made. Even for rarely-changing collections, many current applications require fast search and comparison algorithms.
The present invention may be embodied as an apparatus that includes a memory that contains a data structure comprising a head reference, an ordered list of pages and an ordered list of elements. Each of said elements is linked to one of said pages. The head reference includes an address of a first page in said ordered list of pages. Each page includes an integer value and, for every page other than the last page, an address for the next page in said ordered list of pages. Each element linked to a given page, other than the last element linked to the page, includes an address for the next element linked to the same page.
The present invention may also be embodied as computer program product for recording a data structure comprising a head reference, an ordered list of pages and an ordered list of elements with each of the elements being linked to one of the pages. The computer program product includes a computer usable medium with computer usable program code recorded thereon and being configured to record, in said head reference, an address of a first page in said ordered list of pages; to record an integer value for each page; to record, for each page other than the last page, an address for the next page in said ordered list of pages; and to record, for each element linked to a given page other than the last element linked to the page, an address for the next element linked to the same page.
As will be appreciated by one skilled in the art, the present invention may be embodied as a method, system, or computer program product. Accordingly, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment (including firmware, resident software, micro-code, etc.) or an embodiment combining software and hardware aspects that may all generally be referred to herein as a “circuit,” “module” or “system.” Furthermore, the present invention may take the form of a computer program product on a computer-usable storage medium having computer-usable program code embodied in the medium.
Any suitable computer usable or computer readable medium may be utilized. The computer-usable or computer-readable medium may be, for example but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, device, or propagation medium. More specific examples (a non-exhaustive list) of the computer-readable medium would include the following: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or Flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a transmission media such as those supporting the Internet or an intranet, or a magnetic storage device. Note that the computer-usable or computer-readable medium could even be paper or another suitable medium upon which the program is printed, as the program can be electronically captured, via, for instance, optical scanning of the paper or other medium, then compiled, interpreted, or otherwise processed in a suitable manner, if necessary, and then stored in a computer memory. In the context of this document, a computer-usable or computer-readable medium may be any medium that can contain, store, communicate, propagate, or transport the program for use by or in connection with the instruction execution system, apparatus, or device. The computer-usable medium may include a propagated data signal with the computer-usable program code embodied therewith, either in baseband or as part of a carrier wave. The computer usable program code may be transmitted using any appropriate medium, including but not limited to the Internet, wireline, optical fiber cable, RF, etc.
Computer program code for carrying out operations of the present invention may be written in an object oriented programming language such as Java, Smalltalk, C++ or the like. However, the computer program code for carrying out operations of the present invention may also be written in conventional procedural programming languages, such as the “C” programming language or similar programming languages. The program code may execute entirely on the user's computer, partly on the user's computer, as a stand-alone software package, partly on the user's computer and partly on a remote computer or entirely on the remote computer or server. In the latter scenario, the remote computer may be connected to the user's computer through a local area network (LAN) or a wide area network (WAN), or the connection may be made to an external computer (for example, through the Internet using an Internet Service Provider).
The present invention is described below with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems) and computer program products according to embodiments of the invention. It will be understood that each block of the flowchart illustrations and/or block diagrams, and combinations of blocks in the flowchart illustrations and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions/acts specified in the flowchart and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function/act specified in the flowchart and/or block diagram block or blocks.
The computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions/acts specified in the flowchart and/or block diagram block or blocks.
Owing to the invention, it is possible to provide a data structure that is optimized for very long immutable lists. The data structure decomposes a conventional list into sublists called “pages” which are then chained together to form the complete list. By employing techniques involving page reuse between different instances of lists, the implementations are able to ensure that duplicate copies of pages are only created where necessary. Although a small overhead in memory allocation is necessary for the paged structure, the benefits in speed and memory allocation during add and delete operations is dramatic. A performance study carried out on the new data structure shows a six times speed boost compared to a conventional list.
The “paged” approach also supports optimization of comparing and hashing algorithms. For example, during a comparison of two lists, shared pages will be checked only by the equality of their references. Moreover, pages could contain a check-sum value of all the hash values of the elements in the page, boosting the execution speed of the equality test also for not shared pages.
The data structure is created so that the head reference 12 comprises an address of a first page (P0) and each page 14, except the final page (P4), includes an address of a subsequent page 14. Each page 14 includes a stored integer equal to the number of elements 16 linked to that page 14. Each page 14 having elements 16 linked to it includes an address of the first linked element 16. For example, in the case of page P0, the address of element R0 is stored by that page. Each element 16 that is linked to a page 14, except the final element 16, includes an address of a subsequent element 16.
The data structure 10 shown in
In the conventional list of
In a preferred embodiment, the data structure 10, in relation to those pages 14 having linked elements 16, is constructed so that the stored integer for each page, except the final page, is identical. This means that all of the pages, except the last page (P2 in
A second significant advantage of the data structure 10 is that if a new structure is created following a change such as an addition of an element, which creates a new structure, then this can be stored extremely efficiently.
In
If a user or program that is accessing the data structure 10 wishes to insert a new element in the ordered list of elements, then a new list will be created.
The naming and numbering of pages 14 and reference elements 16 are irrelevant to the logical and relative positioning of the elements 16. The index position of each element 16 will not be hard-coded in the list elements but will be calculated as the list is scanned. As shown in
Deletion operations that are performed on the data structure 10 will follow a pattern similar to addition methods: the page where the element has to be deleted will be copied and the element removed. If the page becomes empty, then the page will be removed all together. A new list is created which effectively has a new ordered list of pages, again all or all except one, being linked to the elements in the original list.
It is easy to carry out a ListA.append(List B) operation. The new list will be constituted by the pages of List A followed by those ones of List B. No copying of elements is needed. In order to improve appending operations, the list object will contain also a pointer to the last non-empty page so that it won't be necessary to scan the full length of the list while appending an element.
The data structure can be searched with a find method of the list in the form of a simple linear search. The page size is a parameter that could be specified when constructing the list. It is possible to implement an autonomic behavior where the page size changes as the list grows.
The flowchart and block diagrams in the Figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams and/or flowchart illustration, and combinations of blocks in the block diagrams and/or flowchart illustration, can be implemented by special purpose hardware-based systems that perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The terminology used herein is for the purpose of describing particular embodiments only and is not intended to be limiting of the invention. As used herein, the singular forms “a”, “an” and “the” are intended to include the plural forms as well, unless the context clearly indicates otherwise. It will be further understood that the terms “comprises” and/or “comprising,” when used in this specification, specify the presence of stated features, integers, steps, operations, elements, and/or components, but do not preclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and/or groups thereof.
The corresponding structures, materials, acts, and equivalents of all means or step plus function elements in the claims below are intended to include any structure, material, or act for performing the function in combination with other claimed elements as specifically claimed. The description of the present invention has been presented for purposes of illustration and description, but is not intended to be exhaustive or limited to the invention in the form disclosed. Many modifications and variations will be apparent to those of ordinary skill in the art without departing from the scope and spirit of the invention. The embodiment was chosen and described in order to best explain the principles of the invention and the practical application, and to enable others of ordinary skill in the art to understand the invention for various embodiments with various modifications as are suited to the particular use contemplated.
Having thus described the invention of the present application in detail and by reference to preferred embodiments thereof, it will be apparent that modifications and variations are possible without departing from the scope of the invention defined in the appended claims.
Number | Date | Country | Kind |
---|---|---|---|
07109140.0 | May 2007 | EP | regional |