summaryrefslogtreecommitdiff
blob: 06ad0165ec76fd782bc35e4c27816ce029328332 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
From 83699774fa669abfbc5c5c3dc9265308246bd4f6 Mon Sep 17 00:00:00 2001
From: Michael Meeks <michael.meeks@novell.com>
Date: Fri, 1 May 2009 15:23:23 +0100
Subject: [PATCH] Patch originally committed only to the ChangeLog & tests ...
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

        Bug 572508 – gmarkup speedup ...

        * glib/gmarkup.c: Various optimizations: do less allocations by
        keeping a pool of GStrings, do in-place unescaping, avoid redundant
        utf-8 validation.
---
 glib/gmarkup.c | 1129 +++++++++++++++++++++++---------------------------------
 1 files changed, 455 insertions(+), 674 deletions(-)

diff --git a/glib/gmarkup.c b/glib/gmarkup.c
index b1f4f46..35118ad 100644
--- a/glib/gmarkup.c
+++ b/glib/gmarkup.c
@@ -82,11 +82,15 @@ struct _GMarkupParseContext
    * the callback for it.
    */
   GString *partial_chunk;
+  GSList *spare_chunks;
 
   GMarkupParseState state;
   GSList *tag_stack;
-  gchar **attr_names;
-  gchar **attr_values;
+  GSList *tag_stack_gstr;
+  GSList *spare_list_nodes;
+
+  GString **attr_names;
+  GString **attr_values;
   gint cur_attr;
   gint alloc_attrs;
 
@@ -94,8 +98,6 @@ struct _GMarkupParseContext
   gssize       current_text_len;      
   const gchar *current_text_end;
 
-  GString *leftover_char_portion;
-
   /* used to save the start of the last interesting thingy */
   const gchar *start;
 
@@ -112,6 +114,39 @@ struct _GMarkupParseContext
   gpointer held_user_data;
 };
 
+/*
+ * Helpers to reduce our allocation overhead, we have
+ * a well defined allocation lifecycle.
+ */
+static GSList *
+get_list_node (GMarkupParseContext *context, gpointer data)
+{
+  GSList *node;
+  if (context->spare_list_nodes != NULL)
+    {
+      node = context->spare_list_nodes;
+      context->spare_list_nodes = g_slist_remove_link (context->spare_list_nodes, node);
+    }
+  else
+    node = g_slist_alloc();
+  node->data = data;
+  return node;
+}
+
+static void
+free_list_node (GMarkupParseContext *context, GSList *node)
+{
+  node->data = NULL;
+  context->spare_list_nodes = g_slist_concat (node, context->spare_list_nodes);
+}
+
+static inline void
+string_blank (GString *string)
+{
+  string->str[0] = '\0';
+  string->len = 0;
+}
+
 /**
  * g_markup_parse_context_new:
  * @parser: a #GMarkupParser
@@ -148,9 +183,12 @@ g_markup_parse_context_new (const GMarkupParser *parser,
   context->char_number = 1;
 
   context->partial_chunk = NULL;
+  context->spare_chunks = NULL;
+  context->spare_list_nodes = NULL;
 
   context->state = STATE_START;
   context->tag_stack = NULL;
+  context->tag_stack_gstr = NULL;
   context->attr_names = NULL;
   context->attr_values = NULL;
   context->cur_attr = -1;
@@ -159,7 +197,6 @@ g_markup_parse_context_new (const GMarkupParser *parser,
   context->current_text = NULL;
   context->current_text_len = -1;
   context->current_text_end = NULL;
-  context->leftover_char_portion = NULL;
 
   context->start = NULL;
   context->iter = NULL;
@@ -179,6 +216,14 @@ g_markup_parse_context_new (const GMarkupParser *parser,
   return context;
 }
 
+static void
+string_full_free (gpointer ptr, gpointer user_data)
+{
+  g_string_free (ptr, TRUE);
+}
+
+static void clear_attributes (GMarkupParseContext *context);
+
 /**
  * g_markup_parse_context_free:
  * @context: a #GMarkupParseContext
@@ -198,18 +243,21 @@ g_markup_parse_context_free (GMarkupParseContext *context)
   if (context->dnotify)
     (* context->dnotify) (context->user_data);
 
-  g_strfreev (context->attr_names);
-  g_strfreev (context->attr_values);
+  clear_attributes (context);
+  g_free (context->attr_names);
+  g_free (context->attr_values);
 
-  g_slist_foreach (context->tag_stack, (GFunc)g_free, NULL);
+  g_slist_foreach (context->tag_stack_gstr, string_full_free, NULL);
+  g_slist_free (context->tag_stack_gstr);
   g_slist_free (context->tag_stack);
 
+  g_slist_foreach (context->spare_chunks, string_full_free, NULL);
+  g_slist_free (context->spare_chunks);
+  g_slist_free (context->spare_list_nodes);
+
   if (context->partial_chunk)
     g_string_free (context->partial_chunk, TRUE);
 
-  if (context->leftover_char_portion)
-    g_string_free (context->leftover_char_portion, TRUE);
-
   g_free (context);
 }
 
@@ -301,43 +349,101 @@ propagate_error (GMarkupParseContext  *context,
   g_propagate_error (dest, src);
 }
 
-/* To make these faster, we first use the ascii-only tests, then check
- * for the usual non-alnum name-end chars, and only then call the
- * expensive unicode stuff. Nobody uses non-ascii in XML tag/attribute
- * names, so this is a reasonable hack that virtually always avoids
- * the guniprop call.
- */
 #define IS_COMMON_NAME_END_CHAR(c) \
   ((c) == '=' || (c) == '/' || (c) == '>' || (c) == ' ')
 
 static gboolean
-is_name_start_char (const gchar *p)
+slow_name_validate (GMarkupParseContext *context, const char *name, GError **error)
 {
-  if (g_ascii_isalpha (*p) ||
-      (!IS_COMMON_NAME_END_CHAR (*p) &&
-       (*p == '_' || 
-	*p == ':' ||
-	g_unichar_isalpha (g_utf8_get_char (p)))))
-    return TRUE;
-  else
-    return FALSE;
+  const char *p = name;
+
+  if (!g_utf8_validate (name, strlen (name), NULL))
+    {
+      set_error (context, error, G_MARKUP_ERROR_BAD_UTF8,
+		 _("Invalid UTF-8 encoded text in name - not valid '%s'"), name);
+      return FALSE;
+    }
+
+  if (!(g_ascii_isalpha (*p) ||
+	(!IS_COMMON_NAME_END_CHAR (*p) &&
+	 (*p == '_' || 
+	  *p == ':' ||
+	  g_unichar_isalpha (g_utf8_get_char (p))))))
+    {
+      set_error (context, error, G_MARKUP_ERROR_PARSE,
+		 _("'%s' is not a valid name "), name);
+      return FALSE;
+    }
+
+  for (p = g_utf8_next_char (name); *p != '\0'; p = g_utf8_next_char (p))
+    {
+      /* is_name_char */
+      if (!(g_ascii_isalnum (*p) ||
+	    (!IS_COMMON_NAME_END_CHAR (*p) &&
+	     (*p == '.' || 
+	      *p == '-' ||
+	      *p == '_' ||
+	      *p == ':' ||
+	      g_unichar_isalpha (g_utf8_get_char (p))))))
+	{
+	  set_error (context, error, G_MARKUP_ERROR_PARSE,
+		     _("'%s' is not a valid name: '%c' "), name, *p);
+	  return FALSE;
+	}
+    }
+  return TRUE;
 }
 
+/* 
+ * Use me for elements, attributes etc.
+ */
 static gboolean
-is_name_char (const gchar *p)
+name_validate (GMarkupParseContext *context, const char *name, GError **error)
 {
-  if (g_ascii_isalnum (*p) ||
-      (!IS_COMMON_NAME_END_CHAR (*p) &&
-       (*p == '.' || 
-	*p == '-' ||
-	*p == '_' ||
-	*p == ':' ||
-	g_unichar_isalpha (g_utf8_get_char (p)))))
-    return TRUE;
-  else
-    return FALSE;
+  char mask;
+  const char *p;
+
+  /* name start char */
+  p = name;
+  if (G_UNLIKELY (IS_COMMON_NAME_END_CHAR (*p) ||
+		  !(g_ascii_isalpha (*p) || *p == '_' || *p == ':')))
+    goto slow_validate;
+  
+  for (mask = *p++; *p != '\0'; p++)
+    {
+      mask |= *p;
+
+      /* is_name_char */
+      if (G_UNLIKELY (!(g_ascii_isalnum (*p) ||
+			(!IS_COMMON_NAME_END_CHAR (*p) &&
+			 (*p == '.' || 
+			  *p == '-' ||
+			  *p == '_' ||
+			  *p == ':')))))
+	goto slow_validate;
+    }
+
+  if (mask & 0x80) /* un-common / non-ascii */
+    goto slow_validate;
+
+  return TRUE;
+
+ slow_validate:
+  return slow_name_validate (context, name, error);
 }
 
+static gboolean
+text_validate (GMarkupParseContext *context, const char *p, int len, GError **error)
+{
+  if (!g_utf8_validate (p, len, NULL))
+    {
+      set_error (context, error, G_MARKUP_ERROR_BAD_UTF8,
+		 _("Invalid UTF-8 encoded text in name - not valid '%s'"), p);
+      return FALSE;
+    }
+  else
+    return TRUE;
+}
 
 static gchar*
 char_str (gunichar c,
@@ -360,7 +466,6 @@ static void
 set_unescape_error (GMarkupParseContext *context,
                     GError             **error,
                     const gchar         *remaining_text,
-                    const gchar         *remaining_text_end,
                     GMarkupError         code,
                     const gchar         *format,
                     ...)
@@ -373,7 +478,7 @@ set_unescape_error (GMarkupParseContext *context,
 
   remaining_newlines = 0;
   p = remaining_text;
-  while (p != remaining_text_end)
+  while (*p != '\0')
     {
       if (*p == '\n')
         ++remaining_newlines;
@@ -397,414 +502,199 @@ set_unescape_error (GMarkupParseContext *context,
   g_propagate_error (error, tmp_error);
 }
 
-typedef enum
-{
-  USTATE_INSIDE_TEXT,
-  USTATE_AFTER_AMPERSAND,
-  USTATE_INSIDE_ENTITY_NAME,
-  USTATE_AFTER_CHARREF_HASH
-} UnescapeState;
-
-typedef struct
-{
-  GMarkupParseContext *context;
-  GString *str;
-  UnescapeState state;
-  const gchar *text;
-  const gchar *text_end;
-  const gchar *entity_start;
-} UnescapeContext;
-
-static const gchar*
-unescape_text_state_inside_text (UnescapeContext *ucontext,
-                                 const gchar     *p,
-                                 GError         **error)
+/*
+ * re-write the GString in-place, unescaping anything that escaped.
+ * most XML does not contain entities, or escaping.
+ */
+static gboolean
+unescape_gstring_inplace (GMarkupParseContext *context,
+			  GString             *string,
+			  gboolean            *is_ascii,
+			  GError             **error)
 {
-  const gchar *start;
+  char mask, *to;
+  int line_num = 1;
+  const char *from;
   gboolean normalize_attribute;
 
-  if (ucontext->context->state == STATE_INSIDE_ATTRIBUTE_VALUE_SQ ||
-      ucontext->context->state == STATE_INSIDE_ATTRIBUTE_VALUE_DQ)
+  *is_ascii = FALSE;
+
+  /* are we unescaping an attribute or not ? */
+  if (context->state == STATE_INSIDE_ATTRIBUTE_VALUE_SQ ||
+      context->state == STATE_INSIDE_ATTRIBUTE_VALUE_DQ)
     normalize_attribute = TRUE;
   else
     normalize_attribute = FALSE;
 
-  start = p;
-  
-  while (p != ucontext->text_end)
-    {
-      if (*p == '&')
-        {
-          break;
-        }
-      else if (normalize_attribute && (*p == '\t' || *p == '\n'))
-        {
-          g_string_append_len (ucontext->str, start, p - start);
-          g_string_append_c (ucontext->str, ' ');
-          p = g_utf8_next_char (p);
-          start = p;
-        }
-      else if (*p == '\r')
-        {
-          g_string_append_len (ucontext->str, start, p - start);
-          g_string_append_c (ucontext->str, normalize_attribute ? ' ' : '\n');
-          p = g_utf8_next_char (p);
-          if (p != ucontext->text_end && *p == '\n')
-            p = g_utf8_next_char (p);
-          start = p;
-        }
-      else
-        p = g_utf8_next_char (p);
-    }
-  
-  if (p != start)
-    g_string_append_len (ucontext->str, start, p - start);
-  
-  if (p != ucontext->text_end && *p == '&')
-    {
-      p = g_utf8_next_char (p);
-      ucontext->state = USTATE_AFTER_AMPERSAND;
-    }
-
-  return p;
-}
-
-static const gchar*
-unescape_text_state_after_ampersand (UnescapeContext *ucontext,
-                                     const gchar     *p,
-                                     GError         **error)
-{
-  ucontext->entity_start = NULL;
-  
-  if (*p == '#')
-    {
-      p = g_utf8_next_char (p);
-
-      ucontext->entity_start = p;
-      ucontext->state = USTATE_AFTER_CHARREF_HASH;
-    }
-  else if (!is_name_start_char (p))
-    {
-      if (*p == ';')
-        {
-          set_unescape_error (ucontext->context, error,
-                              p, ucontext->text_end,
-                              G_MARKUP_ERROR_PARSE,
-                              _("Empty entity '&;' seen; valid "
-                                "entities are: &amp; &quot; &lt; &gt; &apos;"));
-        }
-      else
-        {
-          gchar buf[8];
-
-          set_unescape_error (ucontext->context, error,
-                              p, ucontext->text_end,
-                              G_MARKUP_ERROR_PARSE,
-                              _("Character '%s' is not valid at "
-                                "the start of an entity name; "
-                                "the & character begins an entity; "
-                                "if this ampersand isn't supposed "
-                                "to be an entity, escape it as "
-                                "&amp;"),
-                              utf8_str (p, buf));
-        }
-    }
-  else
-    {
-      ucontext->entity_start = p;
-      ucontext->state = USTATE_INSIDE_ENTITY_NAME;
-    }
-
-  return p;
-}
-
-static const gchar*
-unescape_text_state_inside_entity_name (UnescapeContext *ucontext,
-                                        const gchar     *p,
-                                        GError         **error)
-{
-  while (p != ucontext->text_end)
-    {
-      if (*p == ';')
-        break;
-      else if (!is_name_char (p))
-        {
-          gchar ubuf[8];
-
-          set_unescape_error (ucontext->context, error,
-                              p, ucontext->text_end,
-                              G_MARKUP_ERROR_PARSE,
-                              _("Character '%s' is not valid "
-                                "inside an entity name"),
-                              utf8_str (p, ubuf));
-          break;
-        }
-
-      p = g_utf8_next_char (p);
-    }
-
-  if (ucontext->context->state != STATE_ERROR)
-    {
-      if (p != ucontext->text_end)
-        {
-	  gint len = p - ucontext->entity_start;
-
-          /* move to after semicolon */
-          p = g_utf8_next_char (p);
-          ucontext->state = USTATE_INSIDE_TEXT;
-
-          if (strncmp (ucontext->entity_start, "lt", len) == 0)
-            g_string_append_c (ucontext->str, '<');
-          else if (strncmp (ucontext->entity_start, "gt", len) == 0)
-            g_string_append_c (ucontext->str, '>');
-          else if (strncmp (ucontext->entity_start, "amp", len) == 0)
-            g_string_append_c (ucontext->str, '&');
-          else if (strncmp (ucontext->entity_start, "quot", len) == 0)
-            g_string_append_c (ucontext->str, '"');
-          else if (strncmp (ucontext->entity_start, "apos", len) == 0)
-            g_string_append_c (ucontext->str, '\'');
-          else
-            {
-	      gchar *name;
-
-	      name = g_strndup (ucontext->entity_start, len);
-              set_unescape_error (ucontext->context, error,
-                                  p, ucontext->text_end,
-                                  G_MARKUP_ERROR_PARSE,
-                                  _("Entity name '%s' is not known"),
-                                  name);
-	      g_free (name);
-            }
-        }
-      else
-        {
-          set_unescape_error (ucontext->context, error,
-                              /* give line number of the & */
-                              ucontext->entity_start, ucontext->text_end,
-                              G_MARKUP_ERROR_PARSE,
-                              _("Entity did not end with a semicolon; "
-                                "most likely you used an ampersand "
-                                "character without intending to start "
-                                "an entity - escape ampersand as &amp;"));
-        }
-    }
-#undef MAX_ENT_LEN
-
-  return p;
-}
-
-static const gchar*
-unescape_text_state_after_charref_hash (UnescapeContext *ucontext,
-                                        const gchar     *p,
-                                        GError         **error)
-{
-  gboolean is_hex = FALSE;
-  const char *start;
-
-  start = ucontext->entity_start;
-
-  if (*p == 'x')
-    {
-      is_hex = TRUE;
-      p = g_utf8_next_char (p);
-      start = p;
-    }
-
-  while (p != ucontext->text_end && *p != ';')
-    p = g_utf8_next_char (p);
-
-  if (p != ucontext->text_end)
+  /*
+   * Meeks' theorum: unescaping can only shrink text.
+   * for &lt; etc. this is obvious, for &#xffff; more
+   * thought is required, but this is patently so.
+   */
+  mask = 0;
+  for (from = to = string->str; *from != '\0'; from++, to++) 
     {
-      g_assert (*p == ';');
+      *to = *from;
+
+      mask |= *to;
+      if (*to == '\n')
+	line_num++;
+      if (normalize_attribute && (*to == '\t' || *to == '\n'))
+	*to = ' ';
+      if (*to == '\r')
+	{
+	  *to = normalize_attribute ? ' ' : '\n';
+	  if (from[1] == '\n')
+	    from++;
+	}
+      if (*from == '&')
+	{
+	  from++;
+	  if (*from == '#')
+	    {
+	      gboolean is_hex = FALSE;
+	      gulong l;
+	      gchar *end = NULL;
 
-      /* digit is between start and p */
+	      from++;
 
-      if (start != p)
-        {
-          gulong l;
-          gchar *end = NULL;
-                    
-          errno = 0;
-          if (is_hex)
-            l = strtoul (start, &end, 16);
-          else
-            l = strtoul (start, &end, 10);
+	      if (*from == 'x')
+		{
+		  is_hex = TRUE;
+		  from++;
+		}
 
-          if (end != p || errno != 0)
-            {
-              set_unescape_error (ucontext->context, error,
-                                  start, ucontext->text_end,
-                                  G_MARKUP_ERROR_PARSE,
-                                  _("Failed to parse '%-.*s', which "
-                                    "should have been a digit "
-                                    "inside a character reference "
-                                    "(&#234; for example) - perhaps "
-                                    "the digit is too large"),
-                                  p - start, start);
-            }
-          else
-            {
-              /* characters XML 1.1 permits */
-              if ((0 < l && l <= 0xD7FF) ||
-                  (0xE000 <= l && l <= 0xFFFD) ||
-                  (0x10000 <= l && l <= 0x10FFFF))
-                {
-                  gchar buf[8];
-                  g_string_append (ucontext->str, char_str (l, buf));
-                }
-              else
-                {
-                  set_unescape_error (ucontext->context, error,
-                                      start, ucontext->text_end,
-                                      G_MARKUP_ERROR_PARSE,
-                                      _("Character reference '%-.*s' does not "
-					"encode a permitted character"),
-                                      p - start, start);
-                }
+	      /* digit is between start and p */
+	      errno = 0;
+	      if (is_hex)
+		l = strtoul (from, &end, 16);
+	      else
+		l = strtoul (from, &end, 10);
+	      
+	      if (end == from || errno != 0)
+		{
+		  set_unescape_error (context, error,
+				      from, G_MARKUP_ERROR_PARSE,
+				      _("Failed to parse '%-.*s', which "
+					"should have been a digit "
+					"inside a character reference "
+					"(&#234; for example) - perhaps "
+					"the digit is too large"),
+				      end - from, from);
+		  return FALSE;
+		}
+	      else if (*end != ';')
+		{
+		  set_unescape_error (context, error,
+				      from, G_MARKUP_ERROR_PARSE,
+				      _("Character reference did not end with a "
+					"semicolon; "
+					"most likely you used an ampersand "
+					"character without intending to start "
+					"an entity - escape ampersand as &amp;"));
+		  return FALSE;
+		}
+	      else
+		{
+		  /* characters XML 1.1 permits */
+		  if ((0 < l && l <= 0xD7FF) ||
+		      (0xE000 <= l && l <= 0xFFFD) ||
+		      (0x10000 <= l && l <= 0x10FFFF))
+		    {
+		      gchar buf[8];
+		      char_str (l, buf);
+		      strcpy (to, buf);
+		      to += strlen (buf) - 1;
+		      from = end;
+		      if (l >= 0x80) /* not ascii */
+			mask |= 0x80;
+		    }
+		  else
+		    {
+		      set_unescape_error (context, error,
+					  from, G_MARKUP_ERROR_PARSE,
+					  _("Character reference '%-.*s' does not "
+					    "encode a permitted character"),
+					  end - from, from);
+		      return FALSE;
+		    }
+		}
             }
 
-          /* Move to next state */
-          p = g_utf8_next_char (p); /* past semicolon */
-          ucontext->state = USTATE_INSIDE_TEXT;
-        }
-      else
-        {
-          set_unescape_error (ucontext->context, error,
-                              start, ucontext->text_end,
-                              G_MARKUP_ERROR_PARSE,
-                              _("Empty character reference; "
-                                "should include a digit such as "
-                                "&#454;"));
-        }
-    }
-  else
-    {
-      set_unescape_error (ucontext->context, error,
-                          start, ucontext->text_end,
-                          G_MARKUP_ERROR_PARSE,
-                          _("Character reference did not end with a "
-                            "semicolon; "
-                            "most likely you used an ampersand "
-                            "character without intending to start "
-                            "an entity - escape ampersand as &amp;"));
+          else if (strncmp (from, "lt;", 3) == 0)
+	    {
+	      *to = '<';
+	      from += 2;
+	    }
+          else if (strncmp (from, "gt;", 3) == 0)
+	    {
+	      *to = '>';
+	      from += 2;
+	    }
+          else if (strncmp (from, "amp;", 4) == 0)
+	    {
+	      *to = '&';
+	      from += 3;
+	    }
+          else if (strncmp (from, "quot;", 5) == 0)
+	    {
+	      *to = '"';
+	      from += 4;
+	    }
+	  else if (strncmp (from, "apos;", 5) == 0)
+	    {
+	      *to = '\'';
+	      from += 4;
+	    }
+	  else
+	    {
+	      if (*from == ';')
+		set_unescape_error (context, error,
+				    from, G_MARKUP_ERROR_PARSE,
+				    _("Empty entity '&;' seen; valid "
+				      "entities are: &amp; &quot; &lt; &gt; &apos;"));
+	      else
+		{
+		  const char *end = strchr (from, ';');
+		  if (end)
+		    set_unescape_error (context, error,
+					from, G_MARKUP_ERROR_PARSE,
+					_("Entity name '%-.*s' is not known"),
+					end-from, from);
+		  else
+		    set_unescape_error (context, error,
+					from, G_MARKUP_ERROR_PARSE,
+					_("Entity did not end with a semicolon; "
+					  "most likely you used an ampersand "
+					  "character without intending to start "
+					  "an entity - escape ampersand as &amp;"));
+		}
+	      return FALSE;
+	    }
+	}
     }
 
-  return p;
-}
+  g_assert (to - string->str <= string->len);
+  if (to - string->str != string->len)
+    g_string_truncate (string, to - string->str);
 
-static gboolean
-unescape_text (GMarkupParseContext *context,
-               const gchar         *text,
-               const gchar         *text_end,
-               GString            **unescaped,
-               GError             **error)
-{
-  UnescapeContext ucontext;
-  const gchar *p;
-
-  ucontext.context = context;
-  ucontext.text = text;
-  ucontext.text_end = text_end;
-  ucontext.entity_start = NULL;
-  
-  ucontext.str = g_string_sized_new (text_end - text);
-
-  ucontext.state = USTATE_INSIDE_TEXT;
-  p = text;
-
-  while (p != text_end && context->state != STATE_ERROR)
-    {
-      g_assert (p < text_end);
-      
-      switch (ucontext.state)
-        {
-        case USTATE_INSIDE_TEXT:
-          {
-            p = unescape_text_state_inside_text (&ucontext,
-                                                 p,
-                                                 error);
-          }
-          break;
-
-        case USTATE_AFTER_AMPERSAND:
-          {
-            p = unescape_text_state_after_ampersand (&ucontext,
-                                                     p,
-                                                     error);
-          }
-          break;
-
-
-        case USTATE_INSIDE_ENTITY_NAME:
-          {
-            p = unescape_text_state_inside_entity_name (&ucontext,
-                                                        p,
-                                                        error);
-          }
-          break;
-
-        case USTATE_AFTER_CHARREF_HASH:
-          {
-            p = unescape_text_state_after_charref_hash (&ucontext,
-                                                        p,
-                                                        error);
-          }
-          break;
+  *is_ascii = !(mask & 0x80);
 
-        default:
-          g_assert_not_reached ();
-          break;
-        }
-    }
-
-  if (context->state != STATE_ERROR) 
-    {
-      switch (ucontext.state) 
-	{
-	case USTATE_INSIDE_TEXT:
-	  break;
-	case USTATE_AFTER_AMPERSAND:
-	case USTATE_INSIDE_ENTITY_NAME:
-	  set_unescape_error (context, error,
-			      NULL, NULL,
-			      G_MARKUP_ERROR_PARSE,
-			      _("Unfinished entity reference"));
-	  break;
-	case USTATE_AFTER_CHARREF_HASH:
-	  set_unescape_error (context, error,
-			      NULL, NULL,
-			      G_MARKUP_ERROR_PARSE,
-			      _("Unfinished character reference"));
-	  break;
-	}
-    }
-
-  if (context->state == STATE_ERROR)
-    {
-      g_string_free (ucontext.str, TRUE);
-      *unescaped = NULL;
-      return FALSE;
-    }
-  else
-    {
-      *unescaped = ucontext.str;
-      return TRUE;
-    }
+  return TRUE;
 }
 
 static inline gboolean
 advance_char (GMarkupParseContext *context)
 {  
-  context->iter = g_utf8_next_char (context->iter);
-  context->char_number += 1;
+  context->iter++;
+  context->char_number++;
 
-  if (context->iter == context->current_text_end)
-    {
+  if (G_UNLIKELY (context->iter == context->current_text_end))
       return FALSE;
-    }
-  else if (*context->iter == '\n')
+
+  else if (G_UNLIKELY (*context->iter == '\n'))
     {
-      context->line_number += 1;
+      context->line_number++;
       context->char_number = 1;
     }
   
@@ -833,37 +723,62 @@ advance_to_name_end (GMarkupParseContext *context)
 {
   do
     {
-      if (!is_name_char (context->iter))
+      if (IS_COMMON_NAME_END_CHAR (*(context->iter)))
         return;
+      if (xml_isspace (*(context->iter)))
+	return;
     }
   while (advance_char (context));
 }
 
 static void
+release_chunk (GMarkupParseContext *context, GString *str)
+{
+  GSList *node;
+  if (!str)
+    return;
+  if (str->allocated_len > 256)
+    { /* large strings are unusual and worth freeing */
+      g_string_free (str, TRUE);
+      return;
+    }
+  string_blank (str);
+  node = get_list_node (context, str);
+  context->spare_chunks = g_slist_concat (node, context->spare_chunks);
+}
+
+static void
 add_to_partial (GMarkupParseContext *context,
                 const gchar         *text_start,
                 const gchar         *text_end)
 {
   if (context->partial_chunk == NULL)
-    context->partial_chunk = g_string_sized_new (text_end - text_start);
+    { /* allocate a new chunk to parse into */
 
-  if (text_start != text_end)
-    g_string_append_len (context->partial_chunk, text_start,
-                         text_end - text_start);
+      if (context->spare_chunks != NULL)
+	{
+	  GSList *node = context->spare_chunks;
+	  context->spare_chunks = g_slist_remove_link (context->spare_chunks, node);
+	  context->partial_chunk = node->data;
+	  free_list_node (context, node);
+	}
+      else
+	context->partial_chunk = g_string_sized_new (MAX (28, text_end - text_start));
+    }
 
-  /* Invariant here that partial_chunk exists */
+  if (text_start != text_end)
+    g_string_insert_len (context->partial_chunk, -1,
+			 text_start, text_end - text_start);
 }
 
-static void
+static inline void
 truncate_partial (GMarkupParseContext *context)
 {
   if (context->partial_chunk != NULL)
-    {
-      context->partial_chunk = g_string_truncate (context->partial_chunk, 0);
-    }
+    string_blank (context->partial_chunk);
 }
 
-static const gchar*
+static inline const gchar*
 current_element (GMarkupParseContext *context)
 {
   return context->tag_stack->data;
@@ -891,6 +806,30 @@ pop_subparser_stack (GMarkupParseContext *context)
 }
 
 static void
+push_partial_as_tag (GMarkupParseContext *context)
+{
+  GString *str = context->partial_chunk;
+  /* sadly, this is exported by gmarkup_get_element_stack as-is */
+  context->tag_stack = g_slist_concat (get_list_node (context, str->str), context->tag_stack);
+  context->tag_stack_gstr = g_slist_concat (get_list_node (context, str), context->tag_stack_gstr);
+  context->partial_chunk = NULL;
+}
+
+static void
+pop_tag (GMarkupParseContext *context)
+{
+  GSList *nodea, *nodeb;
+
+  nodea = context->tag_stack;
+  nodeb = context->tag_stack_gstr;
+  release_chunk (context, nodeb->data);
+  context->tag_stack = g_slist_remove_link (context->tag_stack, nodea);
+  context->tag_stack_gstr = g_slist_remove_link (context->tag_stack_gstr, nodeb);
+  free_list_node (context, nodea);
+  free_list_node (context, nodeb);
+}
+
+static void
 possibly_finish_subparser (GMarkupParseContext *context)
 {
   if (current_element (context) == context->subparser_element)
@@ -916,60 +855,42 @@ static const gchar*
 current_attribute (GMarkupParseContext *context)
 {
   g_assert (context->cur_attr >= 0);
-  return context->attr_names[context->cur_attr];
-}
-
-static void
-find_current_text_end (GMarkupParseContext *context)
-{
-  /* This function must be safe (non-segfaulting) on invalid UTF8.
-   * It assumes the string starts with a character start
-   */
-  const gchar *end = context->current_text + context->current_text_len;
-  const gchar *p;
-  const gchar *next;
-
-  g_assert (context->current_text_len > 0);
-
-  p = g_utf8_find_prev_char (context->current_text, end);
-
-  g_assert (p != NULL); /* since current_text was a char start */
-
-  /* p is now the start of the last character or character portion. */
-  g_assert (p != end);
-  next = g_utf8_next_char (p); /* this only touches *p, nothing beyond */
-
-  if (next == end)
-    {
-      /* whole character */
-      context->current_text_end = end;
-    }
-  else
-    {
-      /* portion */
-      context->leftover_char_portion = g_string_new_len (p, end - p);
-      context->current_text_len -= (end - p);
-      context->current_text_end = p;
-    }
+  return context->attr_names[context->cur_attr]->str;
 }
 
-
 static void
-add_attribute (GMarkupParseContext *context, char *name)
+add_attribute (GMarkupParseContext *context, GString *str)
 {
   if (context->cur_attr + 2 >= context->alloc_attrs)
     {
       context->alloc_attrs += 5; /* silly magic number */
-      context->attr_names = g_realloc (context->attr_names, sizeof(char*)*context->alloc_attrs);
-      context->attr_values = g_realloc (context->attr_values, sizeof(char*)*context->alloc_attrs);
+      context->attr_names = g_realloc (context->attr_names, sizeof(GString*)*context->alloc_attrs);
+      context->attr_values = g_realloc (context->attr_values, sizeof(GString*)*context->alloc_attrs);
     }
   context->cur_attr++;
-  context->attr_names[context->cur_attr] = name;
+  context->attr_names[context->cur_attr] = str;
   context->attr_values[context->cur_attr] = NULL;
   context->attr_names[context->cur_attr+1] = NULL;
   context->attr_values[context->cur_attr+1] = NULL;
 }
 
+static void
+clear_attributes (GMarkupParseContext *context)
+{
+  /* Go ahead and free the attributes. */
+  for (; context->cur_attr >= 0; context->cur_attr--)
+    {
+      int pos = context->cur_attr;
+      release_chunk (context, context->attr_names[pos]);
+      release_chunk (context, context->attr_values[pos]);
+      context->attr_names[pos] = context->attr_values[pos] = NULL;
+    }
+  g_assert (context->cur_attr == -1);
+  g_assert (context->attr_names == NULL ||
+	    context->attr_names[0] == NULL);
+  g_assert (context->attr_values == NULL ||
+	    context->attr_values[0] == NULL);
+}
 /**
  * g_markup_parse_context_parse:
  * @context: a #GMarkupParseContext
@@ -994,8 +915,6 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
                               gssize               text_len,
                               GError             **error)
 {
-  const gchar *first_invalid;
-  
   g_return_val_if_fail (context != NULL, FALSE);
   g_return_val_if_fail (text != NULL, FALSE);
   g_return_val_if_fail (context->state != STATE_ERROR, FALSE);
@@ -1009,130 +928,16 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
   
   context->parsing = TRUE;
   
-  if (context->leftover_char_portion)
-    {
-      const gchar *first_char;
-
-      if ((*text & 0xc0) != 0x80)
-        first_char = text;
-      else
-        first_char = g_utf8_find_next_char (text, text + text_len);
-
-      if (first_char)
-        {
-          /* leftover_char_portion was completed. Parse it. */
-          GString *portion = context->leftover_char_portion;
-          
-          g_string_append_len (context->leftover_char_portion,
-                               text, first_char - text);
-
-          /* hacks to allow recursion */
-          context->parsing = FALSE;
-          context->leftover_char_portion = NULL;
-          
-          if (!g_markup_parse_context_parse (context,
-                                             portion->str, portion->len,
-                                             error))
-            {
-              g_assert (context->state == STATE_ERROR);
-            }
-          
-          g_string_free (portion, TRUE);
-          context->parsing = TRUE;
-
-          /* Skip the fraction of char that was in this text */
-          text_len -= (first_char - text);
-          text = first_char;
-        }
-      else
-        {
-          /* another little chunk of the leftover char; geez
-           * someone is inefficient.
-           */
-          g_string_append_len (context->leftover_char_portion,
-                               text, text_len);
-
-          if (context->leftover_char_portion->len > 7)
-            {
-              /* The leftover char portion is too big to be
-               * a UTF-8 character
-               */
-              set_error_literal (context,
-                                 error,
-                                 G_MARKUP_ERROR_BAD_UTF8,
-                                 _("Invalid UTF-8 encoded text - overlong sequence"));
-            }
-          
-          goto finished;
-        }
-    }
 
   context->current_text = text;
   context->current_text_len = text_len;
+  context->current_text_end = context->current_text + text_len;
   context->iter = context->current_text;
   context->start = context->iter;
 
-  /* Nothing left after finishing the leftover char, or nothing
-   * passed in to begin with.
-   */
   if (context->current_text_len == 0)
     goto finished;
 
-  /* find_current_text_end () assumes the string starts at
-   * a character start, so we need to validate at least
-   * that much. It doesn't assume any following bytes
-   * are valid.
-   */
-  if ((*context->current_text & 0xc0) == 0x80) /* not a char start */
-    {
-      set_error_literal (context,
-                         error,
-                         G_MARKUP_ERROR_BAD_UTF8,
-                         _("Invalid UTF-8 encoded text - not a start char"));
-      goto finished;
-    }
-
-  /* Initialize context->current_text_end, possibly adjusting
-   * current_text_len, and add any leftover char portion
-   */
-  find_current_text_end (context);
-
-  /* Validate UTF8 (must be done after we find the end, since
-   * we could have a trailing incomplete char)
-   */
-  if (!g_utf8_validate (context->current_text,
-			context->current_text_len,
-			&first_invalid))
-    {
-      gint newlines = 0;
-      const gchar *p, *q;
-      gchar *current_text_dup;
-
-      q = p = context->current_text;
-      while (p != first_invalid)
-        {
-          if (*p == '\n')
-            {
-              ++newlines;
-              q = p + 1;
-              context->char_number = 1;
-            }
-          ++p;
-        }
-
-      context->line_number += newlines;
-      context->char_number += g_utf8_strlen (q, first_invalid - q);
-
-      current_text_dup = g_strndup (context->current_text, context->current_text_len);
-      set_error (context,
-                 error,
-                 G_MARKUP_ERROR_BAD_UTF8,
-                 _("Invalid UTF-8 encoded text - not valid '%s'"),
-                 current_text_dup);
-      g_free (current_text_dup);
-      goto finished;
-    }
-
   while (context->iter != context->current_text_end)
     {
       switch (context->state)
@@ -1191,7 +996,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
 
               context->state = STATE_AFTER_CLOSE_TAG_SLASH;
             }
-          else if (is_name_start_char (context->iter))
+          else if (!IS_COMMON_NAME_END_CHAR (*(context->iter)))
             {
               context->state = STATE_INSIDE_OPEN_TAG_NAME;
 
@@ -1247,7 +1052,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
             tmp_error = NULL;
             if (context->parser->end_element)
               (* context->parser->end_element) (context,
-                                                context->tag_stack->data,
+						current_element (context),
                                                 context->user_data,
                                                 &tmp_error);
 
@@ -1279,10 +1084,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
                                current_element (context));
                   }
               }
-
-            g_free (context->tag_stack->data);
-            context->tag_stack = g_slist_delete_link (context->tag_stack,
-                                                      context->tag_stack);
+	    pop_tag (context);
           }
           break;
 
@@ -1309,12 +1111,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
                * if any; push it on the stack; enter next state.
                */
               add_to_partial (context, context->start, context->iter);
-              context->tag_stack =
-                g_slist_prepend (context->tag_stack,
-                                 g_string_free (context->partial_chunk,
-                                                FALSE));
-
-              context->partial_chunk = NULL;
+	      push_partial_as_tag (context);
 
               context->state = STATE_BETWEEN_ATTRIBUTES;
               context->start = NULL;
@@ -1345,7 +1142,10 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
 	      /* The name has ended. Combine it with the partial chunk
 	       * if any; push it on the stack; enter next state.
 	       */
-              add_attribute (context, g_string_free (context->partial_chunk, FALSE));
+	      if (!name_validate (context, context->partial_chunk->str, error))
+		break;
+
+	      add_attribute (context, context->partial_chunk);
 	      
               context->partial_chunk = NULL;
               context->start = NULL;
@@ -1387,11 +1187,10 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
                 }
               else if (*context->iter == '>')
                 {
-
                   advance_char (context);
                   context->state = STATE_AFTER_CLOSE_ANGLE;
                 }
-              else if (is_name_start_char (context->iter))
+              else if (!IS_COMMON_NAME_END_CHAR (*(context->iter)))
                 {
                   context->state = STATE_INSIDE_ATTRIBUTE_NAME;
                   /* start of attribute name */
@@ -1419,44 +1218,35 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
               if (context->state == STATE_AFTER_ELISION_SLASH ||
                   context->state == STATE_AFTER_CLOSE_ANGLE)
                 {
-                  const gchar *start_name;
-		  /* Ugly, but the current code expects an empty array instead of NULL */
-		  const gchar *empty = NULL;
-                  const gchar **attr_names =  &empty;
-                  const gchar **attr_values = &empty;
+		  int i;
+		  const gchar *start_name;
+                  const gchar **attr_names;
+                  const gchar **attr_values;
                   GError *tmp_error;
 
-                  /* Call user callback for element start */
-                  start_name = current_element (context);
-
-		  if (context->cur_attr >= 0)
+		  attr_names = g_newa (const gchar *, context->cur_attr + 2);
+		  attr_values = g_newa (const gchar *, context->cur_attr + 2);
+		  for (i = 0; i < context->cur_attr + 1; i++)
 		    {
-		      attr_names = (const gchar**)context->attr_names;
-		      attr_values = (const gchar**)context->attr_values;
+		      attr_names[i] = context->attr_names[i]->str;
+		      attr_values[i] = context->attr_values[i]->str;
 		    }
+		  attr_names[i] = NULL;
+		  attr_values[i] = NULL;
 
+                  /* Call user callback for element start */
                   tmp_error = NULL;
-                  if (context->parser->start_element)
+		  start_name = current_element (context);
+
+                  if (context->parser->start_element &&
+		      name_validate (context, start_name, error))
                     (* context->parser->start_element) (context,
-                                                        start_name,
+							start_name,
                                                         (const gchar **)attr_names,
                                                         (const gchar **)attr_values,
                                                         context->user_data,
                                                         &tmp_error);
-
-                  /* Go ahead and free the attributes. */
-		  for (; context->cur_attr >= 0; context->cur_attr--)
-		    {
-		      int pos = context->cur_attr;
-		      g_free (context->attr_names[pos]);
-		      g_free (context->attr_values[pos]);
-		      context->attr_names[pos] = context->attr_values[pos] = NULL;
-		    }
-                  g_assert (context->cur_attr == -1);
-                  g_assert (context->attr_names == NULL ||
-                            context->attr_names[0] == NULL);
-                  g_assert (context->attr_values == NULL ||
-                            context->attr_values[0] == NULL);
+		  clear_attributes (context);
                   
                   if (tmp_error != NULL)
                     propagate_error (context, error, tmp_error);
@@ -1531,25 +1321,22 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
             }
           else
             {
+	      gboolean is_ascii;
               /* The value has ended at the quote mark. Combine it
                * with the partial chunk if any; set it for the current
                * attribute.
                */
-              GString *unescaped;
-              
               add_to_partial (context, context->start, context->iter);
 
               g_assert (context->cur_attr >= 0);
               
-              if (unescape_text (context,
-                                 context->partial_chunk->str,
-                                 context->partial_chunk->str +
-                                 context->partial_chunk->len,
-                                 &unescaped,
-                                 error))
+              if (unescape_gstring_inplace (context, context->partial_chunk, &is_ascii, error) &&
+		  (is_ascii || text_validate (context, context->partial_chunk->str,
+					      context->partial_chunk->len, error)))
                 {
                   /* success, advance past quote and set state. */
-                  context->attr_values[context->cur_attr] = g_string_free (unescaped, FALSE);
+                  context->attr_values[context->cur_attr] = context->partial_chunk;
+		  context->partial_chunk = NULL;
                   advance_char (context);
                   context->state = STATE_BETWEEN_ATTRIBUTES;
                   context->start = NULL;
@@ -1576,30 +1363,25 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
 
           if (context->iter != context->current_text_end)
             {
-              GString *unescaped = NULL;
+	      gboolean is_ascii;
 
               /* The text has ended at the open angle. Call the text
                * callback.
                */
               
-              if (unescape_text (context,
-                                 context->partial_chunk->str,
-                                 context->partial_chunk->str +
-                                 context->partial_chunk->len,
-                                 &unescaped,
-                                 error))
+              if (unescape_gstring_inplace (context, context->partial_chunk, &is_ascii, error) &&
+		  (is_ascii || text_validate (context, context->partial_chunk->str,
+					      context->partial_chunk->len, error)))
                 {
                   GError *tmp_error = NULL;
 
                   if (context->parser->text)
                     (*context->parser->text) (context,
-                                              unescaped->str,
-                                              unescaped->len,
+					      context->partial_chunk->str,
+					      context->partial_chunk->len,
                                               context->user_data,
                                               &tmp_error);
                   
-                  g_string_free (unescaped, TRUE);
-
                   if (tmp_error == NULL)
                     {
                       /* advance past open angle and set state. */
@@ -1618,7 +1400,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
 
         case STATE_AFTER_CLOSE_TAG_SLASH:
           /* Possible next state: INSIDE_CLOSE_TAG_NAME */
-          if (is_name_start_char (context->iter))
+          if (!IS_COMMON_NAME_END_CHAR (*(context->iter)))
             {
               context->state = STATE_INSIDE_CLOSE_TAG_NAME;
 
@@ -1656,13 +1438,9 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
 	  
 	  if (context->iter != context->current_text_end)
 	    {
-	      gchar *close_name;
+	      GString *close_name;
 
-	      /* The name has ended. Combine it with the partial chunk
-	       * if any; check that it matches stack top and pop
-	       * stack; invoke proper callback; enter next state.
-	       */
-	      close_name = g_string_free (context->partial_chunk, FALSE);
+	      close_name = context->partial_chunk;
 	      context->partial_chunk = NULL;
               
 	      if (*context->iter != '>')
@@ -1676,7 +1454,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
 			       "the close element name '%s'; the allowed "
 			       "character is '>'"),
 			     utf8_str (context->iter, buf),
-			     close_name);
+			     close_name->str);
 		}
 	      else if (context->tag_stack == NULL)
 		{
@@ -1685,16 +1463,16 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
 			     G_MARKUP_ERROR_PARSE,
 			     _("Element '%s' was closed, no element "
 			       "is currently open"),
-			     close_name);
+			     close_name->str);
 		}
-	      else if (strcmp (close_name, current_element (context)) != 0)
+	      else if (strcmp (close_name->str, current_element (context)) != 0)
 		{
 		  set_error (context,
 			     error,
 			     G_MARKUP_ERROR_PARSE,
 			     _("Element '%s' was closed, but the currently "
 			       "open element is '%s'"),
-			     close_name,
+			     close_name->str,
 			     current_element (context));
 		}
 	      else
@@ -1710,22 +1488,18 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
 		  tmp_error = NULL;
 		  if (context->parser->end_element)
 		    (* context->parser->end_element) (context,
-						      close_name,
+						      close_name->str,
 						      context->user_data,
 						      &tmp_error);
 		  
                   ensure_no_outstanding_subparser (context);
-		  
-		  /* Pop the tag stack */
-		  g_free (context->tag_stack->data);
-		  context->tag_stack = g_slist_delete_link (context->tag_stack,
-							    context->tag_stack);
+		  pop_tag (context);
 		  
 		  if (tmp_error)
                     propagate_error (context, error, tmp_error);
                 }
-	      
-              g_free (close_name);
+	      context->partial_chunk = close_name;
+	      truncate_partial (context);
             }
           break;
 	  
@@ -1784,14 +1558,22 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
 	      if (context->flags & G_MARKUP_TREAT_CDATA_AS_TEXT &&
 		  strncmp (context->partial_chunk->str, "<![CDATA[", 9) == 0)
 		{
-		  if (context->parser->text)
+		  if (context->parser->text &&
+		      text_validate (context,
+				     context->partial_chunk->str + 9,
+				     context->partial_chunk->len - 12,
+				     error))
 		    (*context->parser->text) (context,
 					      context->partial_chunk->str + 9,
 					      context->partial_chunk->len - 12,
 					      context->user_data,
 					      &tmp_error);
 		}
-	      else if (context->parser->passthrough)
+	      else if (context->parser->passthrough &&
+		       text_validate (context,
+				      context->partial_chunk->str,
+				      context->partial_chunk->len,
+				      error))
                 (*context->parser->passthrough) (context,
                                                  context->partial_chunk->str,
                                                  context->partial_chunk->len,
@@ -1999,7 +1781,6 @@ G_CONST_RETURN GSList *
 g_markup_parse_context_get_element_stack (GMarkupParseContext *context)
 {
   g_return_val_if_fail (context != NULL, NULL);
-
   return context->tag_stack;
 }
 
-- 
1.6.1.3