xml: 10-12% faster benchmark through cleanup and minor refactoring.

db4
John Benediktsson 2012-07-12 19:06:37 -07:00
parent 3c54f12789
commit c9da03fe6d
6 changed files with 36 additions and 27 deletions

View File

@ -2,7 +2,8 @@
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel sequences sequences.private assocs arrays USING: kernel sequences sequences.private assocs arrays
delegate.protocols delegate vectors accessors multiline delegate.protocols delegate vectors accessors multiline
macros words quotations combinators slots fry strings ; macros words quotations combinators slots fry strings
combinators.short-circuit ;
IN: xml.data IN: xml.data
TUPLE: interpolated var ; TUPLE: interpolated var ;
@ -18,9 +19,11 @@ C: <name> name
2dup and [ = ] [ 2drop t ] if ; 2dup and [ = ] [ 2drop t ] if ;
: names-match? ( name1 name2 -- ? ) : names-match? ( name1 name2 -- ? )
[ [ space>> ] bi@ ?= ] {
[ [ url>> ] bi@ ?= ] [ [ space>> ] bi@ ?= ]
[ [ main>> ] bi@ ?= ] 2tri and and ; [ [ url>> ] bi@ ?= ]
[ [ main>> ] bi@ ?= ]
} 2&& ;
: <simple-name> ( string -- name ) : <simple-name> ( string -- name )
"" swap f <name> ; "" swap f <name> ;

View File

@ -18,29 +18,26 @@ IN: xml.elements
: interpolate-quote ( -- interpolated ) : interpolate-quote ( -- interpolated )
[ quoteless-attr ] take-interpolated ; [ quoteless-attr ] take-interpolated ;
: parse-attr ( -- )
parse-name pass-blank "=" expect pass-blank
get-char CHAR: < eq?
[ "<-" expect interpolate-quote ]
[ t parse-quote* ] if 2array , ;
: start-tag ( -- name ? ) : start-tag ( -- name ? )
#! Outputs the name and whether this is a closing tag #! Outputs the name and whether this is a closing tag
get-char CHAR: / eq? dup [ next ] when get-char CHAR: / eq? dup [ next ] when
parse-name swap ; parse-name swap ;
: (middle-tag) ( -- )
pass-blank version-1.0? get-char name-start?
[ parse-attr (middle-tag) ] when ;
: assure-no-duplicates ( attrs-alist -- attrs-alist ) : assure-no-duplicates ( attrs-alist -- attrs-alist )
H{ } clone 2dup '[ swap _ push-at ] assoc-each H{ } clone 2dup '[ swap _ push-at ] assoc-each
[ nip length 2 >= ] { } assoc-filter-as [ nip length 2 >= ] { } assoc-filter-as
[ first first2 duplicate-attr ] unless-empty ; [ first first2 duplicate-attr ] unless-empty ;
: parse-attr ( -- array )
parse-name pass-blank "=" expect pass-blank
get-char CHAR: < eq?
[ "<-" expect interpolate-quote ]
[ t parse-quote* ] if 2array ;
: middle-tag ( -- attrs-alist ) : middle-tag ( -- attrs-alist )
! f make will make a vector if it has any elements ! f produce-as will make a vector if it has any elements
[ (middle-tag) ] f make pass-blank [ pass-blank version-1.0? get-char name-start? ]
[ parse-attr ] f produce-as pass-blank
dup length 1 > [ assure-no-duplicates ] when ; dup length 1 > [ assure-no-duplicates ] when ;
: end-tag ( name attrs-alist -- tag ) : end-tag ( name attrs-alist -- tag )

View File

@ -2,7 +2,7 @@
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel namespaces accessors xml.tokenize xml.data assocs USING: kernel namespaces accessors xml.tokenize xml.data assocs
xml.errors xml.char-classes combinators.short-circuit splitting xml.errors xml.char-classes combinators.short-circuit splitting
fry xml.state sequences combinators ascii ; fry xml.state sequences combinators ascii math ;
IN: xml.name IN: xml.name
! XML namespace processing: ns = namespace ! XML namespace processing: ns = namespace
@ -53,11 +53,19 @@ SYMBOL: ns-stack
} 2&& } 2&&
] if-empty ; ] if-empty ;
: maybe-name ( space main -- name/f )
2dup {
[ drop valid-name? ]
[ nip valid-name? ]
} 2&& [ f <name> ] [ 2drop f ] if ;
: prefixed-name ( str -- name/f ) : prefixed-name ( str -- name/f )
":" split dup length 2 = [ CHAR: : over index [
[ [ valid-name? ] all? ] CHAR: : 2over 1 + swap index-from
[ first2 f <name> ] bi and [ 2drop f ]
] [ drop f ] if ; [ [ head ] [ 1 + tail ] 2bi maybe-name ]
if
] [ drop f ] if* ;
: interpret-name ( str -- name ) : interpret-name ( str -- name )
dup prefixed-name [ ] [ dup prefixed-name [ ] [

View File

@ -3,16 +3,17 @@
USING: namespaces xml.state kernel sequences accessors USING: namespaces xml.state kernel sequences accessors
xml.char-classes xml.errors math io sbufs fry strings ascii xml.char-classes xml.errors math io sbufs fry strings ascii
xml.entities assocs splitting math.parser xml.entities assocs splitting math.parser
locals combinators arrays hints ; locals combinators combinators.short-circuit arrays hints ;
IN: xml.tokenize IN: xml.tokenize
! * Basic utility words ! * Basic utility words
: assure-good-char ( spot ch -- ) : assure-good-char ( spot ch -- )
[ [
over over {
[ version-1.0?>> over text? not ] [ version-1.0?>> over text? not ]
[ check>> ] bi and [ check>> ]
} 1&&
[ [
[ [ 1 + ] change-column drop ] dip [ [ 1 + ] change-column drop ] dip
disallowed-char disallowed-char

View File

@ -34,7 +34,7 @@ SYMBOL: indentation
: ?filter-children ( children -- no-whitespace ) : ?filter-children ( children -- no-whitespace )
xml-pprint? get [ xml-pprint? get [
[ dup string? [ [ blank? ] trim ] when ] map [ dup string? [ [ blank? ] trim ] when ] map
[ [ empty? ] [ string? ] bi and not ] filter [ "" = not ] filter
] when ; ] when ;
PRIVATE> PRIVATE>

View File

@ -89,7 +89,7 @@ M: closer process
[ drop default-prolog ] unless ; [ drop default-prolog ] unless ;
: cut-prolog ( seq -- newseq ) : cut-prolog ( seq -- newseq )
[ [ prolog? not ] [ "" = not ] bi and ] filter ; [ { [ prolog? not ] [ "" = not ] } 1&& ] filter ;
: make-xml-doc ( seq -- xml-doc ) : make-xml-doc ( seq -- xml-doc )
[ get-prolog ] keep [ get-prolog ] keep