Pseudo-pointer taggedpointer in OBJC

Source: Internet
Author: User

If you've seen me before. Two OBJC function hub Msgsend and your impression of NSString is this, I believe that has seen its figure many times, in the end it is what role, I today to uncover the mystery.
I call it a pseudo-pointer, because it is like a ghost, without a physical (instance), but can behave like a OBJC object instance. Can you guess the result of running the following code?

//Non-arcIDUnknown = (ID)0x12345678; NSLog (@"%@", unknown); unknown= (ID)0xa000000000032312; NSLog (@"%@", unknown); NSString* S12 = [NSString stringwithutf8string:" A"]; NSLog (@"%@", S12); [S12 Isequaltostring:unknown]; NSLog (@"%d", * (int64_t*) &S12 = = * (int64_t*) &unknown);

If I write a random address 0x12345678, and then use this address to make OBJC calls, [(ID) 0x12345678 retain]. I believe everyone's first reaction is that I'm accessing a non-understood address, pointing to the pointer to the address and calling it a wild pointer, I'm working on a "wild pointer".

If I write a random address like this 0xa000000000032312, and make OBJC call to this address [(ID) 0xa000000000032312 retain]. I believe everyone's reaction is the same as the above example, although some of them have seen our previous article know that this is a taggedpointer, but I am still working on an untrusted address, when accessing the contents of the 0xa000000000032312 memory address will be an error.

But the result is that the operation of the 0xa000000000032312 address is legitimate, and the NSLog (@ "%@", (ID) 0xa000000000032312) also prints 12.

Everyone is familiar with the build process of the instance, such as [[NSString alloc] init] and [NSString string]. You must allocate space for the image instance and initialize it. The address 0xa000000000032312, however, was just a random fabrication, and there was no space allocated at that address at all.
I'll call you again. [NSString stringwithutf8string: ' 12 '], which returns a pointer to a type that is a nstaggedpointerstring instance. The address pointed to is 0xa000000000032312. It was not until then that the instance of 0xa000000000032312 was built, but a visit to the address was an illegal address.

So Taggedpointer is just a pseudo pointer, and it's the truth in the value of the address that the pointer points to itself.
0xa000000000032312 This value is like a compressed pair like an instance, up to 4 bits is the clue of ISA, referring to the Disassembly Msgsend article, the lowest 8 bits is info information, the middle 9th place 48 bits is the content of the image. With this phantom mirror to see 0xa000000000032312, everything is clear, {isa=0xa,coutent= ' \x31\x32 ', len=2}. This is a case called eightbitsencoding in NSString.

Why have Taggedpointer, let's look at performance.
The "@" AB "stringappendbystring:@" C "and [NSString stringwithutf8string:" abc ") are viewed with the profile separately for the loop 100M;
In the previous example you can find [@ "AB" stringappendbystring:@ "C"] return __nscfstring, [nsstring stringwithutf8string: "ABC"] Returns nstaggedpointerstring.
What are the performance differences between them? Due to the limited performance of my machine, profile is almost running, so there is no graph to intercept, only to state the situation. First [@ "AB" stringappendbystring:@ "C"] return to __nscfstring situation, cfstring constantly in the slow distribution, until the allocation of 1M, memory consumption 50M, the machine in the profile run, I made a decisive stop. On the other hand [NSString stringwithutf8string: "abc"] returns Nstaggedpointerstring's profile, the cfstring number does not grow, and memory naturally does not consume. OK I will reduce the number of cycles to 1M times, do not do profile, direct running comparison, nstaggedpointerstring the situation is significantly faster than several times, because there is no instance of building (, for the return instance, the middle of the process as well as other temporary instances).

Besides nstaggedpointerstring and other taggedpointer, do you know what 0XB0000000000000C2 is, and try it quickly.

Finally, thank you for watching again.

Pseudo-pointer taggedpointer in OBJC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.