File: NSKeyValueCoding%2BCaching.h

package info (click to toggle)
gnustep-base 1.31.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,580 kB
  • sloc: objc: 239,446; ansic: 36,519; cpp: 122; sh: 112; makefile: 100; xml: 32
file content (55 lines) | stat: -rw-r--r-- 2,385 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/** Key-Value Coding Safe Caching Support
   Copyright (C) 2024 Free Software Foundation, Inc.

   Written by:  Hugo Melder <hugo@algoriddim.com>
   Created: August 2024

   This file is part of the GNUstep Base Library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
*/

/**
 * It turns out that valueForKey: is a very expensive operation, and a major
 * bottleneck for Key-Value Observing and other operations such as sorting
 * an array by key.
 *
 * The accessor search patterns for Key-Value observing are discussed in the
 * Apple Key-Value Coding Programming Guide. The return value may be
 * encapuslated into an NSNumber or NSValue object, depending on the Objective-C
 * type encoding of the return value. This means that once valueForKey: found an
 * existing accessor, the Objective-C type encoding of the accessor is
 * retrieved. We then go through a huge switch case to determine the right way
 * to invoke the IMP and potentially encapsulate the return type. The resulting
 * object is then returned.
 * The algorithm for setValue:ForKey: is similar.
 *
 * We can speed this up by caching the IMP of the accessor in a hash table.
 * However, without proper versioning, this quickly becomes very dangerous.
 * The user might exchange implementations, or add new ones expecting the
 * search pattern invariant to still hold. If we clamp onto an IMP, this
 * invariant no longer holds.
 *
 * We will make use of libobjc2's safe caching to avoid this.
 *
 * Note that the caching is opaque. You will only need to redirect all
 * valueForKey: calls to the function below.
 */

#import "Foundation/NSString.h"
#import "GSPrivate.h"

id
valueForKeyWithCaching(id obj, NSString *aKey) GS_ATTRIB_PRIVATE;