# Disabling ARC for custom literals

**URL:** <https://forums.swift.org/t/disabling-arc-for-custom-literals/40787>\
**Category:** Using Swift\
**Tags:** arc\
**Created:** [September 30, 2020, 8:15pm UTC](https://forums.swift.org/t/disabling-arc-for-custom-literals/40787 "2020-09-30T20:15:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![evanireland](https://avatars.discourse-cdn.com/v4/letter/e/839c29/32.png) [@evanireland](https://forums.swift.org/u/evanireland)\
**Post date:** [September 30, 2020, 8:15pm UTC](https://forums.swift.org/t/disabling-arc-for-custom-literals/40787/1 "2020-09-30T20:15:30Z")

</div>

Is there a way to disable ARC for "custom" literals? For example:

class B {  
let value: Bool  
private init(\_ b: Bool) { value = b }  
public static let TRUE = B(true)  
public static let FALSE = B(false)  
}

If the objects referenced by TRUE and FALSE could be marked as permanent, then ARC operations could avoid the overheads of atomic reference counting.

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [September 30, 2020, 8:25pm UTC](https://forums.swift.org/t/disabling-arc-for-custom-literals/40787/2 "2020-09-30T20:25:56Z")

</div>

There is such a mechanism, currently used only by the empty `Array`, `Dictionary`, and `Set` singletons, but it's not exposed in a useful way right now.

---

<div class="post-metadata">

**Author:** ![evanireland](https://avatars.discourse-cdn.com/v4/letter/e/839c29/32.png) [@evanireland](https://forums.swift.org/u/evanireland)\
**Post date:** [September 30, 2020, 8:27pm UTC](https://forums.swift.org/t/disabling-arc-for-custom-literals/40787/3 "2020-09-30T20:27:08Z")

</div>

OK so does it make sense to make an evolution proposal?

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [September 30, 2020, 8:29pm UTC](https://forums.swift.org/t/disabling-arc-for-custom-literals/40787/4 "2020-09-30T20:29:15Z")

</div>

I think it'd be reasonable to write up a pitch for an attribute or something. I don't know if it's a common enough / important enough need that it would be accepted, but it can't hurt to try if you've got the time and energy.

One caveat is that it won't work on anything that inherits from NSObject (more specifically anything that doesn't use Swift native refcounting).

---

<div class="post-metadata">

**Author:** ![evanireland](https://avatars.discourse-cdn.com/v4/letter/e/839c29/32.png) [@evanireland](https://forums.swift.org/u/evanireland)\
**Post date:** [October 6, 2020, 4:06am UTC](https://forums.swift.org/t/disabling-arc-for-custom-literals/40787/5 "2020-10-06T04:06:07Z")

</div>

Thinking further, I wonder if this could be done without any language changes, simply by noting:

class SomeClass {  
static let myValue: MyType = ...  
}

If MyType is a reference type, in the absence of "class GC" (easy, since we don't do GC), then myValue is clearly an immortal object. If the ARC runtime already supports marking certain objects as not requiring thread-safe reference counting (having some kind of "immortal flag"), then such marking could surely be easily applied to static reference-typed definitions using 'let'.

I see immortality is also discussed in [Swift Performance - #39 by anandabits](https://forums.swift.org/t/swift-performance/28776/39), but it doesn't look to have gone any further.

---

<div class="post-metadata">

**Author:** ![ahti](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ahti/32/604_2.png) [@ahti](https://forums.swift.org/u/ahti)\
**Post date:** [October 6, 2020, 11:43pm UTC](https://forums.swift.org/t/disabling-arc-for-custom-literals/40787/6 "2020-10-06T23:43:29Z")

</div>

Fwiw, if you wanna play around and see what performance gains you might find, this seems to do the trick (no guarantees though, I just dug through the stdlib code and took the first best thing I found):

```swift
@_silgen_name("_swift_stdlib_immortalize")
func _swift_stdlib_immortalize(_ p: UnsafeMutableRawPointer)

func immortalize(_ o: AnyObject) {
	withExtendedLifetime(o) { // not sure this is required
		_swift_stdlib_immortalize(Unmanaged.passUnretained(o).toOpaque())
	}
}

```

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [October 7, 2020, 3:17am UTC](https://forums.swift.org/t/disabling-arc-for-custom-literals/40787/7 "2020-10-07T03:17:09Z")

</div>

Yup that looks right

---

<div class="post-metadata">

**Author:** ![evanireland](https://avatars.discourse-cdn.com/v4/letter/e/839c29/32.png) [@evanireland](https://forums.swift.org/u/evanireland)\
**Post date:** [October 7, 2020, 11:07pm UTC](https://forums.swift.org/t/disabling-arc-for-custom-literals/40787/8 "2020-10-07T23:07:28Z")

</div>

Thanks very much, I'll check this out and report back later.

---

<div class="post-metadata">

**Author:** ![evanireland](https://avatars.discourse-cdn.com/v4/letter/e/839c29/32.png) [@evanireland](https://forums.swift.org/u/evanireland)\
**Post date:** [October 8, 2020, 2:34am UTC](https://forums.swift.org/t/disabling-arc-for-custom-literals/40787/9 "2020-10-08T02:34:16Z")

</div>

I gave this technique a try out, for a case actually with non-let-defined values, but rather an in-memory metadata structure that was underneath an ORM. (Rationale: the metadata is constant for the duration of the application process).

Result: 25% reduction in time taken to initialize objects (which needs to refer to the underlying metadata).

Will look at the let-constant application of the technique later.
