Skip to content

Commit 8dd9182

Browse files
authored
Adding Syntax Highlighting
using `.. code-block:: swift` instead of `::` we can turn code into more prettier and legible form!
1 parent 81f49a8 commit 8dd9182

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

Diff for: docs/OptimizationTips.rst

+18-18
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Classes use dynamic dispatch for methods and property accesses by default. Thus
8181
in the following code snippet, ``a.aProperty``, ``a.doSomething()`` and
8282
``a.doSomethingElse()`` will all be invoked via dynamic dispatch:
8383

84-
::
84+
.. code-block:: swift
8585
8686
class A {
8787
var aProperty: [Int]
@@ -120,7 +120,7 @@ compiler can emit direct function calls instead of indirect calls. For instance
120120
in the following ``C.array1`` and ``D.array1`` will be accessed directly
121121
[#]_. In contrast, ``D.array2`` will be called via a vtable:
122122

123-
::
123+
.. code-block:: swift
124124
125125
final class C {
126126
// No declarations in class 'C' can be overridden.
@@ -155,7 +155,7 @@ and field accesses accordingly. For instance in the following,
155155
``e.doSomething()`` and ``f.myPrivateVar``, will be able to be accessed directly
156156
assuming ``E``, ``F`` do not have any overriding declarations in the same file:
157157

158-
::
158+
.. code-block:: swift
159159
160160
private class E {
161161
func doSomething() { ... }
@@ -210,7 +210,7 @@ counting if they contain, recursively, a reference type. By using value types
210210
without reference types, one can avoid additional retain, release traffic inside
211211
Array.
212212

213-
::
213+
.. code-block:: swift
214214
215215
// Don't use a class here.
216216
struct PhonebookEntry {
@@ -231,7 +231,7 @@ Advice: Use ContiguousArray with reference types when NSArray bridging is unnece
231231
If you need an array of reference types and the array does not need to be
232232
bridged to NSArray, use ContiguousArray instead of Array:
233233

234-
::
234+
.. code-block:: swift
235235
236236
class C { ... }
237237
var a: ContiguousArray<C> = [C(...), C(...), ..., C(...)]
@@ -248,7 +248,7 @@ container is mutated. For instance in the following, no copying will occur when
248248
``c`` is assigned to ``d``, but when ``d`` undergoes structural mutation by
249249
appending ``2``, ``d`` will be copied and then ``2`` will be appended to ``d``:
250250

251-
::
251+
.. code-block:: swift
252252
253253
var c: [Int] = [ ... ]
254254
var d = c // No copy will occur here.
@@ -260,7 +260,7 @@ object-reassignment in functions. In Swift, all parameters are passed in at +1,
260260
i.e. the parameters are retained before a callsite, and then are released at the
261261
end of the callee. This means that if one writes a function like the following:
262262

263-
::
263+
.. code-block:: swift
264264
265265
func append_one(_ a: [Int]) -> [Int] {
266266
var a = a
@@ -275,7 +275,7 @@ end of the callee. This means that if one writes a function like the following:
275275
has no uses after ``append_one`` due to the assignment. This can be avoided
276276
through the usage of ``inout`` parameters:
277277

278-
::
278+
.. code-block:: swift
279279
280280
func append_one_in_place(a: inout [Int]) {
281281
a.append(1)
@@ -298,7 +298,7 @@ Advice: Use wrapping integer arithmetic when you can prove that overflow cannot
298298
In performance-critical code you can use wrapping arithmetic to avoid overflow
299299
checks if you know it is safe.
300300

301-
::
301+
.. code-block:: swift
302302
303303
a: [Int]
304304
b: [Int]
@@ -326,7 +326,7 @@ behavior between ``MySwiftFunc<Int>`` and ``MySwiftFunc<String>`` are accounted
326326
for by passing a different table of function pointers and the size abstraction
327327
provided by the box. An example of generics:
328328

329-
::
329+
.. code-block:: swift
330330
331331
class MySwiftFunc<T> { ... }
332332
@@ -341,7 +341,7 @@ generic function specialized to the specific type. This process, called
341341
*specialization*, enables the removal of the overhead associated with
342342
generics. Some more examples of generics:
343343

344-
::
344+
.. code-block:: swift
345345
346346
class MyStack<T> {
347347
func push(_ element: T) { ... }
@@ -394,7 +394,7 @@ represented as values, so this example is somewhat realistic.
394394
.. See Protocol-Oriented-Programming:
395395
.. https://developer.apple.com/videos/play/wwdc2015-408/
396396
397-
::
397+
.. code-block:: swift
398398
399399
protocol P {}
400400
struct Node: P {
@@ -429,7 +429,7 @@ wrapping it in an array. This simple change has a major impact on the
429429
performance of our tree data structure, and the cost of passing the array as an
430430
argument drops from being O(n), depending on the size of the tree to O(1).
431431

432-
::
432+
.. code-block:: swift
433433
434434
struct Tree: P {
435435
var node: [P?]
@@ -461,7 +461,7 @@ construct such a data structure:
461461
.. More details in this blog post by Mike Ash:
462462
.. https://www.mikeash.com/pyblog/friday-qa-2015-04-17-lets-build-swiftarray.html
463463
464-
::
464+
.. code-block:: swift
465465
466466
final class Ref<T> {
467467
var val: T
@@ -498,7 +498,7 @@ the reference Swift will increment the reference count of the ``next`` object
498498
and decrement the reference count of the previous object. These reference
499499
count operations are expensive and unavoidable when using Swift classes.
500500

501-
::
501+
.. code-block:: swift
502502
503503
final class Node {
504504
var next: Node?
@@ -523,7 +523,7 @@ instance held by the ``Unmanaged`` struct instance for the duration of the use
523523
of ``Unmanaged`` (see `Unmanaged.swift`_ for more details) that keeps the instance
524524
alive.
525525

526-
::
526+
.. code-block:: swift
527527
528528
// The call to ``withExtendedLifetime(Head)`` makes sure that the lifetime of
529529
// Head is guaranteed to extend over the region of code that uses Unmanaged
@@ -567,7 +567,7 @@ to retain or release non-trivial structures, which can be expensive.
567567
If it makes sense to limit the adoption of protocols to classes then mark
568568
protocols as class-only protocols to get better runtime performance.
569569

570-
::
570+
.. code-block:: swift
571571
572572
protocol Pingable: AnyObject { func ping() -> Int }
573573
@@ -582,7 +582,7 @@ considerations. Remember that any time one creates a binding for a
582582
closure, one is forcing the compiler to emit an escaping closure,
583583
e.x.:
584584
585-
::
585+
.. code-block:: swift
586586
587587
let f: () -> () = { ... } // Escaping closure
588588
// Contrasted with:

0 commit comments

Comments
 (0)