Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Thread safety #25

Merged
merged 3 commits into from

3 participants

@volodg

The pull request contains the fix to use safely the TouchXML with multithreading.

@schwa
Owner

I like 095e0c1 but the other two commits are possibly unnecessary. The enumeration commit definitely is unneeded (the block based enumeration isn't magically any better than a for in loop).

If you could redo this pull request with just the thread safety changes I'd be happier to accept it.

@schwa
Owner

Ah - actually I can see the value of the block enum version now…

@dodikk

The only advantage of "for" loop is backward compatibility with older iOS versions.

However, with the help of our project below you can use block enumeration API even on iOS 3.
https://github.com/EmbeddedSources/BlockRuntime

According to our benchmarks, block version of NSDictionary enumeration is 3X faster than "for" loop
items count : 10 000 000
for -- 8.390 sec
enumerate -- 3.615 sec

Not sure if it's O(N) but it definitely performs better.

@dodikk

P.S. What else prevents you from accepting the patch?

@schwa schwa merged commit 7e98b15 into TouchCode:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 14, 2012
  1. @volodg

    added context to libxml2 parser

    volodg authored
  2. @volodg

    fixed header style

    volodg authored
  3. @volodg
This page is out of date. Refresh to see the latest.
View
4 Source/CXMLDocument.h
@@ -31,6 +31,8 @@
#import "CXMLNode.h"
+#include <tree.h>
+
enum {
CXMLDocumentTidyHTML = 1 << 9, // Based on NSXMLDocumentTidyHTML
CXMLDocumentTidyXML = 1 << 10, // Based on NSXMLDocumentTidyXML
@@ -40,6 +42,8 @@ enum {
@interface CXMLDocument : CXMLNode {
NSMutableSet *nodePool;
+@public
+ xmlParserCtxtPtr xmlCtxt;
}
- (id)initWithData:(NSData *)inData options:(NSUInteger)inOptions error:(NSError **)outError;
View
16 Source/CXMLDocument.m
@@ -105,7 +105,9 @@ - (id)initWithData:(NSData *)inData encoding:(NSStringEncoding)encoding options:
if ((self = [super init]) != NULL)
{
NSError *theError = NULL;
-
+
+ self->xmlCtxt = xmlNewParserCtxt();
+
#if TOUCHXMLUSETIDY
if (inOptions & CXMLDocumentTidyHTML)
{
@@ -125,7 +127,7 @@ - (id)initWithData:(NSData *)inData encoding:(NSStringEncoding)encoding options:
CFStringEncoding cfenc = CFStringConvertNSStringEncodingToEncoding(encoding);
CFStringRef cfencstr = CFStringConvertEncodingToIANACharSetName(cfenc);
const char *enc = CFStringGetCStringPtr(cfencstr, 0);
- theDoc = xmlReadMemory([inData bytes], [inData length], NULL, enc, XML_PARSE_RECOVER | XML_PARSE_NOWARNING);
+ theDoc = xmlCtxtReadMemory( self->xmlCtxt, [inData bytes], [inData length], NULL, enc, XML_PARSE_RECOVER | XML_PARSE_NOWARNING);
}
if (theDoc != NULL && xmlDocGetRootElement(theDoc) != NULL)
@@ -135,14 +137,14 @@ - (id)initWithData:(NSData *)inData encoding:(NSStringEncoding)encoding options:
}
else
{
- xmlErrorPtr theLastErrorPtr = xmlGetLastError();
+ xmlErrorPtr theLastErrorPtr = xmlCtxtGetLastError( self->xmlCtxt );
NSString* message = [NSString stringWithUTF8String:
(theLastErrorPtr ? theLastErrorPtr->message : "Unknown error")];
NSDictionary *theUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
message, NSLocalizedDescriptionKey, NULL];
theError = [NSError errorWithDomain:@"CXMLErrorDomain" code:1 userInfo:theUserInfo];
-
- xmlResetLastError();
+
+ xmlCtxtResetLastError( self->xmlCtxt );
}
}
@@ -200,6 +202,10 @@ - (void)dealloc
xmlUnlinkNode(_node);
xmlFreeDoc((xmlDocPtr)_node);
_node = NULL;
+ //
+ xmlFreeParserCtxt( self->xmlCtxt );
+ self->xmlCtxt = NULL;
+ //
[super dealloc];
}
View
8 Source/CXMLNode_XPathExtensions.m
@@ -51,10 +51,10 @@ - (NSArray *)nodesForXPath:(NSString *)xpath namespaceMappings:(NSDictionary *)i
xmlXPathContextPtr theXPathContext = xmlXPathNewContext(_node->doc);
theXPathContext->node = _node;
-for (NSString *thePrefix in inNamespaceMappings)
- {
- xmlXPathRegisterNs(theXPathContext, (xmlChar *)[thePrefix UTF8String], (xmlChar *)[[inNamespaceMappings objectForKey:thePrefix] UTF8String]);
- }
+ [ inNamespaceMappings enumerateKeysAndObjectsUsingBlock: ^( id thePrefix, id ns_uri, BOOL *stop_ )
+ {
+ xmlXPathRegisterNs(theXPathContext, (xmlChar *)[thePrefix UTF8String], (xmlChar *)[ns_uri UTF8String]);
+ } ];
// TODO considering putting xmlChar <-> UTF8 into a NSString category
xmlXPathObjectPtr theXPathObject = xmlXPathEvalExpression((const xmlChar *)[xpath UTF8String], theXPathContext);
View
2  Source/Creation/CXMLDocument_CreationExtensions.h
@@ -29,7 +29,7 @@
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of toxicsoftware.com.
-#import "CXMLDocument.h"
+#import <CXMLDocument.h>
@interface CXMLDocument (CXMLDocument_CreationExtensions)
Something went wrong with that request. Please try again.