ObjFW  Check-in [295193f0b9]

Overview
Comment:Optimize -[readLine] and -[readTillDelimiter:] a little.

They should be as fast as they were before -[tryReadLine] etc. were
introduced now.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 295193f0b99a819cff960c7112fc91207e6ffd91e0a5d35ad251d5491bb7e4b4
User & Date: js on 2011-09-16 12:40:23
Other Links: manifest | tags
Context
2011-09-16
12:46
Work around glibc being crap - again. check-in: 45fee921e2 user: js tags: trunk
12:40
Optimize -[readLine] and -[readTillDelimiter:] a little. check-in: 295193f0b9 user: js tags: trunk
11:02
Fix compilation with -fblocks on QNX. check-in: af54c117c6 user: js tags: trunk
Changes

Modified src/OFStream.m from [1971d4e07a] to [c69f9cbafe].

503
504
505
506
507
508
509
510
511
512
513
514
515

516
517
518

519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538

539
540
541
542
543
544
545
503
504
505
506
507
508
509






510



511













512
513
514
515
516
517

518
519
520
521
522
523
524
525







-
-
-
-
-
-
+
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-






-
+







	} @finally {
		[self freeMemory: buffer];
	}

	return ret;
}

- (OFString*)readLine
{
	return [self readLineWithEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)readLineWithEncoding: (of_string_encoding_t)encoding
- (OFString*)_tryReadLineWithEncoding: (of_string_encoding_t)encoding
{
	OFString *line = nil;

			   checkCache: (BOOL)checkCache
	while ((line = [self tryReadLineWithEncoding: encoding]) == nil)
		if ([self isAtEndOfStream])
			return nil;

	return line;
}

- (OFString*)tryReadLine
{
	return [self tryReadLineWithEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)tryReadLineWithEncoding: (of_string_encoding_t)encoding
{
	size_t i, bufferLength, retLength;
	char *retCString, *buffer, *newCache;
	OFString *ret;

	/* Look if there's a line or \0 in our cache */
	if (cache != NULL) {
	if (checkCache && cache != NULL) {
		for (i = 0; i < cacheLength; i++) {
			if (OF_UNLIKELY(cache[i] == '\n' ||
			    cache[i] == '\0')) {
				retLength = i;

				if (i > 0 && cache[i - 1] == '\r')
					retLength--;
664
665
666
667
668
669
670
671

672
673
674

675
676
677
678

679
680

681



682
683



684
685
686
687

688
689
690

691
692
693

694
695






696
697



698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714

715
716
717
718
719
720
721
644
645
646
647
648
649
650

651
652


653
654
655


656
657

658
659
660
661
662


663
664
665
666
667
668

669
670
671

672
673


674
675
676
677
678
679
680
681
682


683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701

702
703
704
705
706
707
708
709







-
+

-
-
+


-
-
+

-
+

+
+
+
-
-
+
+
+



-
+


-
+

-
-
+


+
+
+
+
+
+
-
-
+
+
+
















-
+







	} @finally {
		[self freeMemory: buffer];
	}

	return nil;
}

- (OFString*)readTillDelimiter: (OFString*)delimiter
- (OFString*)readLine
{
	return [self readTillDelimiter: delimiter
			  withEncoding: OF_STRING_ENCODING_UTF_8];
	return [self readLineWithEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)readTillDelimiter: (OFString*)delimiter
		  withEncoding: (of_string_encoding_t)encoding
- (OFString*)readLineWithEncoding: (of_string_encoding_t)encoding
{
	OFString *ret = nil;
	OFString *line = nil;

	if ((line = [self _tryReadLineWithEncoding: encoding
					checkCache: YES]) != nil)
		return line;
	while ((ret = [self tryReadTillDelimiter: delimiter
				    withEncoding: encoding]) == nil)

	while ((line = [self _tryReadLineWithEncoding: encoding
					   checkCache: NO]) == nil)
		if ([self isAtEndOfStream])
			return nil;

	return ret;
	return line;
}

- (OFString*)tryReadTillDelimiter: (OFString*)delimiter
- (OFString*)tryReadLine
{
	return [self tryReadTillDelimiter: delimiter
			     withEncoding: OF_STRING_ENCODING_UTF_8];
	return [self tryReadLineWithEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)tryReadLineWithEncoding: (of_string_encoding_t)encoding
{
	return [self _tryReadLineWithEncoding: encoding
				   checkCache: YES];
}

- (OFString*)tryReadTillDelimiter: (OFString*)delimiter
		     withEncoding: (of_string_encoding_t)encoding
- (OFString*)_tryReadTillDelimiter: (OFString*)delimiter
		      withEncoding: (of_string_encoding_t)encoding
			checkCache: (BOOL)checkCache
{
	const char *delimiterUTF8String;
	size_t i, j, delimiterLength, bufferLength, retLength;
	char *retCString, *buffer, *newCache;
	OFString *ret;

	/* FIXME: Convert delimiter to specified charset */
	delimiterUTF8String = [delimiter UTF8String];
	delimiterLength = [delimiter UTF8StringLength];
	j = 0;

	if (delimiterLength == 0)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	/* Look if there's something in our cache */
	if (cache != NULL) {
	if (checkCache && cache != NULL) {
		for (i = 0; i < cacheLength; i++) {
			if (cache[i] != delimiterUTF8String[j++])
				j = 0;

			if (j == delimiterLength || cache[i] == '\0') {
				if (cache[i] == '\0')
					delimiterLength = 1;
826
827
828
829
830
831
832








































833
834
835
836
837
838
839
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







	} @finally {
		[self freeMemory: buffer];
	}

	return nil;
}


- (OFString*)readTillDelimiter: (OFString*)delimiter
{
	return [self readTillDelimiter: delimiter
			  withEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)readTillDelimiter: (OFString*)delimiter
		  withEncoding: (of_string_encoding_t)encoding
{
	OFString *ret = nil;

	if ((ret = [self _tryReadTillDelimiter: delimiter
				  withEncoding: encoding
				    checkCache: YES]) != nil)
		return ret;

	while ((ret = [self _tryReadTillDelimiter: delimiter
				     withEncoding: encoding
				       checkCache: NO]) == nil)
		if ([self isAtEndOfStream])
			return nil;

	return ret;
}

- (OFString*)tryReadTillDelimiter: (OFString*)delimiter
{
	return [self tryReadTillDelimiter: delimiter
			     withEncoding: OF_STRING_ENCODING_UTF_8];
}

- (OFString*)tryReadTillDelimiter: (OFString*)delimiter
		     withEncoding: (of_string_encoding_t)encoding
{
	return [self _tryReadTillDelimiter: delimiter
			      withEncoding: encoding
				checkCache: YES];
}

- (BOOL)buffersWrites
{
	return buffersWrites;
}

- (void)setBuffersWrites: (BOOL)enable
{