Specializing Ropes for Ruby

Total Page:16

File Type:pdf, Size:1020Kb

Specializing Ropes for Ruby Specializing Ropes for Ruby Authors removed for review Abstract The choice of string representation must strike a balance Ropes are a data structure for representing character strings between potentially competing concerns. Even modest im- via a binary tree of operation-labeled nodes. Both the nodes provements to either string memory consumption or perfor- and the trees constructed from them are immutable, mak- mance of common string operations could have a tremen- ing ropes a persistent data structure. Ropes were designed dous effect on overall efficiency. to perform well with large strings, and in particular, con- The predominant string representation is as a thin veneer catenation of large strings. We present our findings in us- over a contiguous byte array. As a consequence, the runtime ing ropes to implement mutable strings in JRuby+Truffle, an performance of string operations in such systems follows implementation of the Ruby programming language using that of a byte array while the use cases may not. a self-specializing abstract syntax tree interpreter and dy- The Cedar programming language [4] introduced ropes [1] namic compilation. We extend ropes to support Ruby lan- as an alternative string representation in order to better match guage features such as encodings and refine operations to the design goals of the language. By modeling strings as an better support typical Ruby programs. We also use ropes to immutable binary tree where the leaves represent sequences work around underlying limitations of the JVM platform in of characters and interior nodes represent lazy operations, representing strings. Finally, we evaluate the performance of such as concatenation, the language was able to drastically our implementation of ropes and demonstrate that they per- alter the cost of common string operations. However, in or- form 0.9x – 9.4x as fast as byte array-based string represen- der to satisfy performance goals for different use cases, ropes tations in representative benchmarks. were introduced as a data type distinct from strings with lim- ited compatibility between the two. In summary, this paper Categories and Subject Descriptors D.3.4 [Programming makes the following contributions: Languages]: Processors—Run-time environments • We apply the ropes data structure to represent strings in the Ruby programming language and show how self- Keywords Virtual Machine, Interpreter, Ropes, Strings, optimizing AST interpreters facilitate their implementa- Truffle, Graal, Ruby, Java tion. • We introduce new lazy operations that can be represented 1. Introduction as nodes in a rope. Strings of character data are one of the most frequently used • We show how ropes allow further optimizations to meta- data types in general purpose programming languages. Pro- programming operations that are heavily used in id- viding a textual representation of natural language, they are a iomatic Ruby. convenient form for interacting with users at I/O boundaries. • We demonstrate that ropes, an immutable data structure, They are often used for internal system operations as well, have utility in languages with mutable string data types. particularly where program identifiers are conveniently rep- • We show that ropes can reduce the cost of string opera- resented as strings, such as in metaprogramming. Some lan- tions involving multi-byte characters. guages rely so heavily on strings that they are glibly referred to as “stringly typed.” 2. Background This section discusses traditional string representations and ropes in more detail, along with the complications intro- duced by the presence of multi-byte characters. We then present Ruby’s string semantics. Finally, we introduce our execution environment: JRuby+Truffle. 2.1 String Representation The classical representation of strings is as an array of bytes [Copyright notice will appear here once ’preprint’ option is removed.] with an optional terminal character. These strings require 1 2016/4/23 contiguous memory cells and optimize for compactness. other type of encoding, some of the advantages inherent Consequently, they have the same advantages arrays have, to byte arrays, such as constant-time character access, are such as constant-time element access and data pre-fetching. muted. By extending this simple representation with additional Ropes, as implemented in Cedar, suffer from some of metadata, such as string byte length or head offsets, the set the same encoding issues as their byte array counterparts. of fast operations available to strings can be expanded. E.g., E.g., accessing a character by index cannot be performed string truncation can simply update the length value rather by a simple binary search. Instead, a full treewalk must be than require a new array allocation and byte copy. performed so the byte sequences can be processed. The byte array representation is not well-suited for all applications, however. In some languages, such as Java, the 2.4 Ruby length of an array may have an upper-bound that is smaller Ruby1 is an object-oriented, dynamically-typed program- than addressable memory, placing a similar limit on the ming language with mutable, encoding-aware strings as a length of a string. Requiring contiguous memory may also core data type. Ruby strings also serve as the language’s byte prevent allocations if a free block cannot be found. Addition- array type via a special binary encoding. ally, operations that require byte copying, such as string con- Ruby strings can be interned to another data type known catenation, may lead to excessive memory fragmentation. as a symbol. Due to their performance advantages and more compact syntax, symbols are used frequently in idiomatic 2.2 Ropes Ruby. However, the symbol API is deliberately quite limited Ropes [1] are a persistent data structure [5] that can be used and symbols cannot be easily composed without construct- as an alternative implementation of strings. They were de- ing a string as a temporary buffer. Additionally, symbols signed for the Cedar programming language [4], with the were not eligible for garbage collection in Ruby versions explicit goal of improving runtime performance over large prior to 2.2.0. Subsequently, Ruby programs often consist strings. They were inspired by other immutable string repre- of a mixture of strings and symbols to perform similar tasks. sentations, but introduced the efficient sequencing of opera- The programmer must decide which type to use as a careful tions by representing them as operation-labeled nodes linked balance between code concerns, such as concision and value together in a binary tree. safety, and VM concerns, such as performance, garbage col- Since ropes are chained together via pointers, the upper- lection, and memory reuse. bound on the length of a rope is the total amount of address- able memory. Moreover, the nodes in a rope do not need to 2.5 JRuby+Truffle reside in contiguous memory. The price of this flexibility is Due to its rich features, such as metaprogramming, com- a heavier base data structure. Depending on the application, prehensive collection APIs, and a pure object-oriented de- however, it may be possible to recuperate that cost via de- sign, Ruby has been an attractive target for language im- duplication, as the immutable nature of ropes means they plementors and researchers. JRuby2 is an open source, al- can be reused by the runtime. ternative implementation of Ruby that uses its own com- Beyond the additional overhead of the data structure, pilation phases to apply Ruby-level optimizations before ropes suffer from some inefficiencies that mutable byte ar- generating Java bytecode, including extensive use of the rays do not. In the pathological case of transforming each invokedynamic instruction. JRuby+Truffle [7] is devel- character in a string, the rope would devolve to an allocated oped as part of the JRuby project, providing an alternative node for each character. The Cedar environment employed backend for the JRuby runtime that eschews the guest lan- heuristics to recognize a handful of such problematic usage guage compiler in favor of a self-optimizing AST interpreter patterns and provide specialized operations for them. written using the Truffle framework [9]. To achieve peak per- 2.3 Encodings formance, JRuby+Truffle must be paired with GraalVM [8] — a convenient distribution that bundles together OpenJDK The byte array representation of strings is optimal as long as and the Graal dynamic compiler. characters can be represented within the space of a byte. As JRuby+Truffle achieves much of its performance via spe- computers have broadened their reach to a more diverse au- cialized implementations of core Ruby methods. Most often dience, the set of characters that must be representable has these specializations are based upon argument types so that grown well beyond the 256 afforded by a byte. Encodings polymorphic calls can be distilled down to their constituent have been developed as a means of expanding the set of rep- cases without incurring the overhead of unused code paths. resentable characters by interpreting byte sequences, rather However, it can also specialize on attributes associated with than individual bytes, as characters. These byte sequences values. In our implementation, the various rope cases are can either be fixed-width or variable-width. distinct types allowing for type specialization, but they also When encodings are taken into account, traditional string representations can be seen as being optimized for fixed- 1 Ruby, Yukihiro Matsumoto and others, https://www.ruby-lang.org/ width encodings with a character width of 1 byte. For any 2 JRuby, Charles Nutter, Thomas Enebo and others, http://jruby.org/ 2 2016/4/23 track metadata that we can specialize on, such as character The reference Ruby implementation, MRI, is written in width. Being able to specialize on both structure and data C and uses a pointer and length field. In some cases the allows us to tailor the code generated for each call site. pointer can be copy-on-write shared by referring to the same character array, however this is only implemented for simple 3.
Recommended publications
  • Traits: Experience with a Language Feature
    7UDLWV([SHULHQFHZLWKD/DQJXDJH)HDWXUH (PHUVRQ50XUSK\+LOO $QGUHZ3%ODFN 7KH(YHUJUHHQ6WDWH&ROOHJH 2*,6FKRRORI6FLHQFH1(QJLQHHULQJ$ (YHUJUHHQ3DUNZD\1: 2UHJRQ+HDOWKDQG6FLHQFH8QLYHUVLW\ 2O\PSLD$:$ 1::DONHU5G PXUHPH#HYHUJUHHQHGX %HDYHUWRQ$25 EODFN#FVHRJLHGX ABSTRACT the desired semantics of that method changes, or if a bug is This paper reports our experiences using traits, collections of found, the programmer must track down and fix every copy. By pure methods designed to promote reuse and understandability reusing a method, behavior can be defined and maintained in in object-oriented programs. Traits had previously been used to one place. refactor the Smalltalk collection hierarchy, but only by the crea- tors of traits themselves. This experience report represents the In object-oriented programming, inheritance is the normal way first independent test of these language features. Murphy-Hill of reusing methods—classes inherit methods from other classes. implemented a substantial multi-class data structure called ropes Single inheritance is the most basic and most widespread type of that makes significant use of traits. We found that traits im- inheritance. It allows methods to be shared among classes in an proved understandability and reduced the number of methods elegant and efficient way, but does not always allow for maxi- that needed to be written by 46%. mum reuse. Consider a small example. In Squeak [7], a dialect of Smalltalk, Categories and Subject Descriptors the class &ROOHFWLRQ is the superclass of all the classes that $UUD\ +HDS D.2.3 [Programming Languages]: Coding Tools and Tech- implement collection data structures, including , , 6HW niques - object-oriented programming and . The property of being empty is common to many ob- jects—it simply requires that the object have a size method, and D.3.3 [Programming Languages]: Language Constructs and that the method returns zero.
    [Show full text]
  • Data Structure
    EDUSAT LEARNING RESOURCE MATERIAL ON DATA STRUCTURE (For 3rd Semester CSE & IT) Contributors : 1. Er. Subhanga Kishore Das, Sr. Lect CSE 2. Mrs. Pranati Pattanaik, Lect CSE 3. Mrs. Swetalina Das, Lect CA 4. Mrs Manisha Rath, Lect CA 5. Er. Dillip Kumar Mishra, Lect 6. Ms. Supriti Mohapatra, Lect 7. Ms Soma Paikaray, Lect Copy Right DTE&T,Odisha Page 1 Data Structure (Syllabus) Semester & Branch: 3rd sem CSE/IT Teachers Assessment : 10 Marks Theory: 4 Periods per Week Class Test : 20 Marks Total Periods: 60 Periods per Semester End Semester Exam : 70 Marks Examination: 3 Hours TOTAL MARKS : 100 Marks Objective : The effectiveness of implementation of any application in computer mainly depends on the that how effectively its information can be stored in the computer. For this purpose various -structures are used. This paper will expose the students to various fundamentals structures arrays, stacks, queues, trees etc. It will also expose the students to some fundamental, I/0 manipulation techniques like sorting, searching etc 1.0 INTRODUCTION: 04 1.1 Explain Data, Information, data types 1.2 Define data structure & Explain different operations 1.3 Explain Abstract data types 1.4 Discuss Algorithm & its complexity 1.5 Explain Time, space tradeoff 2.0 STRING PROCESSING 03 2.1 Explain Basic Terminology, Storing Strings 2.2 State Character Data Type, 2.3 Discuss String Operations 3.0 ARRAYS 07 3.1 Give Introduction about array, 3.2 Discuss Linear arrays, representation of linear array In memory 3.3 Explain traversing linear arrays, inserting & deleting elements 3.4 Discuss multidimensional arrays, representation of two dimensional arrays in memory (row major order & column major order), and pointers 3.5 Explain sparse matrices.
    [Show full text]
  • Precise Analysis of String Expressions
    Precise Analysis of String Expressions Aske Simon Christensen, Anders Møller?, and Michael I. Schwartzbach BRICS??, Department of Computer Science University of Aarhus, Denmark aske,amoeller,mis @brics.dk f g Abstract. We perform static analysis of Java programs to answer a simple question: which values may occur as results of string expressions? The answers are summarized for each expression by a regular language that is guaranteed to contain all possible values. We present several ap- plications of this analysis, including statically checking the syntax of dynamically generated expressions, such as SQL queries. Our analysis constructs flow graphs from class files and generates a context-free gram- mar with a nonterminal for each string expression. The language of this grammar is then widened into a regular language through a variant of an algorithm previously used for speech recognition. The collection of resulting regular languages is compactly represented as a special kind of multi-level automaton from which individual answers may be extracted. If a program error is detected, examples of invalid strings are automat- ically produced. We present extensive benchmarks demonstrating that the analysis is efficient and produces results of useful precision. 1 Introduction To detect errors and perform optimizations in Java programs, it is useful to know which values that may occur as results of string expressions. The exact answer is of course undecidable, so we must settle for a conservative approxima- tion. The answers we provide are summarized for each expression by a regular language that is guaranteed to contain all its possible values. Thus we use an upper approximation, which is what most client analyses will find useful.
    [Show full text]
  • Regular Languages
    id+id*id / (id+id)*id - ( id + +id ) +*** * (+)id + Formal languages Strings Languages Grammars 2 Marco Maggini Language Processing Technologies Symbols, Alphabet and strings 3 Marco Maggini Language Processing Technologies String operations 4 • Marco Maggini Language Processing Technologies Languages 5 • Marco Maggini Language Processing Technologies Operations on languages 6 Marco Maggini Language Processing Technologies Concatenation and closure 7 Marco Maggini Language Processing Technologies Closure and linguistic universe 8 Marco Maggini Language Processing Technologies Examples 9 • Marco Maggini Language Processing Technologies Grammars 10 Marco Maggini Language Processing Technologies Production rules L(G) = {anbncn | n ≥ 1} T = {a,b,c} N = {A,B,C} production rules 11 Marco Maggini Language Processing Technologies Derivation 12 Marco Maggini Language Processing Technologies The language of G • Given a grammar G the generated language is the set of strings, made up of terminal symbols, that can be derived in 0 or more steps from the start symbol LG = { x ∈ T* | S ⇒* x} Example: grammar for the arithmetic expressions T = {num,+,*,(,)} start symbol N = {E} S = E E ⇒ E * E ⇒ ( E ) * E ⇒ ( E + E ) * E ⇒ ( num + E ) * E ⇒ (num+num)* E ⇒ (num + num) * num language phrase 13 Marco Maggini Language Processing Technologies Classes of generative grammars 14 Marco Maggini Language Processing Technologies Regular languages • Regular languages can be described by different formal models, that are equivalent ▫ Finite State Automata (FSA) ▫ Regular
    [Show full text]
  • The Cedar Programming Environment: a Midterm Report and Examination
    The Cedar Programming Environment: A Midterm Report and Examination Warren Teitelman The Cedar Programming Environment: A Midterm Report and Examination Warren Teitelman t CSL-83-11 June 1984 [P83-00012] © Copyright 1984 Xerox Corporation. All rights reserved. CR Categories and Subject Descriptors: D.2_6 [Software Engineering]: Programming environments. Additional Keywords and Phrases: integrated programming environment, experimental programming, display oriented user interface, strongly typed programming language environment, personal computing. t The author's present address is: Sun Microsystems, Inc., 2550 Garcia Avenue, Mountain View, Ca. 94043. The work described here was performed while employed by Xerox Corporation. XEROX Xerox Corporation Palo Alto Research Center 3333 Coyote Hill Road Palo Alto, California 94304 1 Abstract: This collection of papers comprises a report on Cedar, a state-of-the-art programming system. Cedar combines in a single integrated environment: high-quality graphics, a sophisticated editor and document preparation facility, and a variety of tools for the programmer to use in the construction and debugging of his programs. The Cedar Programming Language is a strongly-typed, compiler-oriented language of the Pascal family. What is especially interesting about the Ce~ar project is that it is one of the few examples where an interactive, experimental programming environment has been built for this kind of language. In the past, such environments have been confined to dynamically typed languages like Lisp and Smalltalk. The first paper, "The Roots of Cedar," describes the conditions in 1978 in the Xerox Palo Alto Research Center's Computer Science Laboratory that led us to embark on the Cedar project and helped to define its objectives and goals.
    [Show full text]
  • Formal Languages
    Formal Languages Discrete Mathematical Structures Formal Languages 1 Strings Alphabet: a finite set of symbols – Normally characters of some character set – E.g., ASCII, Unicode – Σ is used to represent an alphabet String: a finite sequence of symbols from some alphabet – If s is a string, then s is its length ¡ ¡ – The empty string is symbolized by ¢ Discrete Mathematical Structures Formal Languages 2 String Operations Concatenation x = hi, y = bye ¡ xy = hibye s ¢ = s = ¢ s ¤ £ ¨© ¢ ¥ § i 0 i s £ ¥ i 1 ¨© s s § i 0 ¦ Discrete Mathematical Structures Formal Languages 3 Parts of a String Prefix Suffix Substring Proper prefix, suffix, or substring Subsequence Discrete Mathematical Structures Formal Languages 4 Language A language is a set of strings over some alphabet ¡ Σ L Examples: – ¢ is a language – ¢ is a language £ ¤ – The set of all legal Java programs – The set of all correct English sentences Discrete Mathematical Structures Formal Languages 5 Operations on Languages Of most concern for lexical analysis Union Concatenation Closure Discrete Mathematical Structures Formal Languages 6 Union The union of languages L and M £ L M s s £ L or s £ M ¢ ¤ ¡ Discrete Mathematical Structures Formal Languages 7 Concatenation The concatenation of languages L and M £ LM st s £ L and t £ M ¢ ¤ ¡ Discrete Mathematical Structures Formal Languages 8 Kleene Closure The Kleene closure of language L ∞ ¡ i L £ L i 0 Zero or more concatenations Discrete Mathematical Structures Formal Languages 9 Positive Closure The positive closure of language L ∞ i L £ L
    [Show full text]
  • Formal Languages
    Formal Languages Wednesday, September 29, 2010 Reading: Sipser pp 13-14, 44-45 Stoughton 2.1, 2.2, end of 2.3, beginning of 3.1 CS235 Languages and Automata Department of Computer Science Wellesley College Alphabets, Strings, and Languages An alphabet is a set of symbols. E.g.: 1 = {0,1}; 2 = {-,0,+} 3 = {a,b, …, y, z}; 4 = {, , a , aa } A string over is a seqqfyfuence of symbols from . The empty string is ofte written (Sipser) or ; Stoughton uses %. * denotes all strings over . E.g.: * • 1 contains , 0, 1, 00, 01, 10, 11, 000, … * • 2 contains , -, 0, +, --, -0, -+, 0-, 00, 0+, +-, +0, ++, ---, … * • 3 contains , a, b, …, aa, ab, …, bar, baz, foo, wellesley, … * • 4 contains , , , a , aa, …, a , …, a aa , aa a ,… A lnlanguage over (Stoug hto nns’s -lnlanguage ) is any sbstsubset of *. I.e., it’s a (possibly countably infinite) set of strings over . E.g.: •L1 over 1 is all sequences of 1s and all sequences of 10s. •L2 over 2 is all strings with equal numbers of -, 0, and +. •L3 over 3 is all lowercase words in the OED. •L4 over 4 is {, , a aa }. Formal Languages 11-2 1 Languages over Finite Alphabets are Countable A language = a set of strings over an alphabet = a subset of *. Suppose is finite. Then * (and any subset thereof) is countable. Why? Can enumerate all strings in lexicographic (dictionary) order! • 1 = {0,1} 1 * = {, 0, 1 00, 01, 10, 11 000, 001, 010, 011, 100, 101, 110, 111, …} • for 3 = {a,b, …, y, z}, can enumerate all elements of 3* in lexicographic order -- we’ll eventually get to any given element.
    [Show full text]
  • String Solving with Word Equations and Transducers: Towards a Logic for Analysing Mutation XSS (Full Version)
    String Solving with Word Equations and Transducers: Towards a Logic for Analysing Mutation XSS (Full Version) Anthony W. Lin Pablo Barceló Yale-NUS College, Singapore Center for Semantic Web Research [email protected] & Dept. of Comp. Science, Univ. of Chile, Chile [email protected] Abstract to name a few (e.g. see [3] for others). Certain applications, how- We study the fundamental issue of decidability of satisfiability ever, require more expressive constraint languages. The framework over string logics with concatenations and finite-state transducers of satisfiability modulo theories (SMT) builds on top of the basic as atomic operations. Although restricting to one type of opera- constraint satisfaction problem by interpreting atomic propositions tions yields decidability, little is known about the decidability of as a quantifier-free formula in a certain “background” logical the- their combined theory, which is especially relevant when analysing ory, e.g., linear arithmetic. Today fast SMT-solvers supporting a security vulnerabilities of dynamic web pages in a more realis- multitude of background theories are available including Boolector, tic browser model. On the one hand, word equations (string logic CVC4, Yices, and Z3, to name a few (e.g. see [4] for others). The with concatenations) cannot precisely capture sanitisation func- backbones of fast SMT-solvers are usually a highly efficient SAT- tions (e.g. htmlescape) and implicit browser transductions (e.g. in- solver (to handle disjunctions) and an effective method of dealing nerHTML mutations). On the other hand, transducers suffer from with satisfiability of formulas in the background theory. the reverse problem of being able to model sanitisation functions In the past seven years or so there have been a lot of works and browser transductions, but not string concatenations.
    [Show full text]
  • Smalltalk Idioms
    Smalltalk Idioms Farewell and a wood pile Kent Beck IT’S THE OBJECTS, STUPID If we parsed the string “@years”, the resulting picture S me awhile to see the obvious. Some- would look like Figure 6. When the BinaryFunction un- times even longer than that. Three or four times in the wraps its children, the right function will be in place. last month I’ve been confronted by problems I had a As I said, several times in the last month I’ve faced hard time solving. In each case, the answer became clear baffling problems that became easy when I asked myself when I asked myself the simple question, “How can I the question, “How could I make an object to solve this make an object to solve this problem for me?” You think problem for me?” Sometimes it was a method that just I’d have figured it out by now: got a problem? make an didn’t want to be simplified, so I created an object just for object for it. that method. Sometimes it was a question of adding Here’s an example: I had to write an editor for a tree features to an object for a particular purpose without clut- structure. There were several ways of viewing and editing tering the object (as in the editing example). I recommend the tree. On the left was a hierarchical list. On the top right that the next time you run into a problem that just doesn’t was a text editor on the currently selected node of the tree.
    [Show full text]
  • A Generalization of Square-Free Strings a GENERALIZATION of SQUARE-FREE STRINGS
    A Generalization of Square-free Strings A GENERALIZATION OF SQUARE-FREE STRINGS BY NEERJA MHASKAR, B.Tech., M.S. a thesis submitted to the department of Computing & Software and the school of graduate studies of mcmaster university in partial fulfillment of the requirements for the degree of Doctor of Philosophy c Copyright by Neerja Mhaskar, August, 2016 All Rights Reserved Doctor of Philosophy (2016) McMaster University (Dept. of Computing & Software) Hamilton, Ontario, Canada TITLE: A Generalization of Square-free Strings AUTHOR: Neerja Mhaskar B.Tech., (Mechanical Engineering) Jawaharlal Nehru Technological University Hyderabad, India M.S., (Engineering Science) Louisiana State University, Baton Rouge, USA SUPERVISOR: Professor Michael Soltys CO-SUPERVISOR: Professor Ryszard Janicki NUMBER OF PAGES: xiii, 116 ii To my family Abstract Our research is in the general area of String Algorithms and Combinatorics on Words. Specifically, we study a generalization of square-free strings, shuffle properties of strings, and formalizing the reasoning about finite strings. The existence of infinitely long square-free strings (strings with no adjacent re- peating word blocks) over a three (or more) letter finite set (referred to as Alphabet) is a well-established result. A natural generalization of this problem is that only sub- sets of the alphabet with predefined cardinality are available, while selecting symbols of the square-free string. This problem has been studied by several authors, and the lowest possible bound on the cardinality of the subset given is four. The problem remains open for subset size three and we investigate this question. We show that square-free strings exist in several specialized cases of the problem and propose ap- proaches to solve the problem, ranging from patterns in strings to Proof Complexity.
    [Show full text]
  • Effective STL
    Effective STL Author: Scott Meyers E-version is made by: Strangecat@epubcn Thanks is given to j1foo@epubcn, who has helped to revise this e-book. Content Containers........................................................................................1 Item 1. Choose your containers with care........................................................... 1 Item 2. Beware the illusion of container-independent code................................ 4 Item 3. Make copying cheap and correct for objects in containers..................... 9 Item 4. Call empty instead of checking size() against zero. ............................. 11 Item 5. Prefer range member functions to their single-element counterparts... 12 Item 6. Be alert for C++'s most vexing parse................................................... 20 Item 7. When using containers of newed pointers, remember to delete the pointers before the container is destroyed. ........................................................... 22 Item 8. Never create containers of auto_ptrs. ................................................... 27 Item 9. Choose carefully among erasing options.............................................. 29 Item 10. Be aware of allocator conventions and restrictions. ......................... 34 Item 11. Understand the legitimate uses of custom allocators........................ 40 Item 12. Have realistic expectations about the thread safety of STL containers. 43 vector and string............................................................................48 Item 13. Prefer vector
    [Show full text]
  • The Ocaml System Release 4.02
    The OCaml system release 4.02 Documentation and user's manual Xavier Leroy, Damien Doligez, Alain Frisch, Jacques Garrigue, Didier R´emy and J´er^omeVouillon August 29, 2014 Copyright © 2014 Institut National de Recherche en Informatique et en Automatique 2 Contents I An introduction to OCaml 11 1 The core language 13 1.1 Basics . 13 1.2 Data types . 14 1.3 Functions as values . 15 1.4 Records and variants . 16 1.5 Imperative features . 18 1.6 Exceptions . 20 1.7 Symbolic processing of expressions . 21 1.8 Pretty-printing and parsing . 22 1.9 Standalone OCaml programs . 23 2 The module system 25 2.1 Structures . 25 2.2 Signatures . 26 2.3 Functors . 27 2.4 Functors and type abstraction . 29 2.5 Modules and separate compilation . 31 3 Objects in OCaml 33 3.1 Classes and objects . 33 3.2 Immediate objects . 36 3.3 Reference to self . 37 3.4 Initializers . 38 3.5 Virtual methods . 38 3.6 Private methods . 40 3.7 Class interfaces . 42 3.8 Inheritance . 43 3.9 Multiple inheritance . 44 3.10 Parameterized classes . 44 3.11 Polymorphic methods . 47 3.12 Using coercions . 50 3.13 Functional objects . 54 3.14 Cloning objects . 55 3.15 Recursive classes . 58 1 2 3.16 Binary methods . 58 3.17 Friends . 60 4 Labels and variants 63 4.1 Labels . 63 4.2 Polymorphic variants . 69 5 Advanced examples with classes and modules 73 5.1 Extended example: bank accounts . 73 5.2 Simple modules as classes .
    [Show full text]