12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879(* The constants, in thousandths of a byte, are least-squares fits of what gzip
-9 saves on the cascade output of the 504 SatCSS sheets, ref.css and
tw_all.css when one token is deleted: a new token, 2.42 + 0.061 per byte; one
run of declarations printed in that order earlier, 1.32 + 0.005 per byte; one
such run of selector branches, 1.47 + 0.029 per byte. A run's cost hardly
grows with its length, as an LZ77 match's does not. Brotli -q 11 agrees in
direction: about 2.9 for a new token, 1.7 for a declaration run and 3.2 for a
selector run. *)letper_byte=1000letnovel=2420letnovel_byte=61letdeclaration_match=1320letdeclaration_match_byte=5letselector_match=1470letselector_match_byte=29typetoken=BranchofSelector.t|DeclarationofDeclaration.declaration(* A token's identity is its minified text. *)moduleSeen=Hashtbl.Make(structtypet=tokenletequalab=match(a,b)with|Brancha,Branchb->Selector.equalab|Declarationa,Declarationb->Declaration.same_minifiedab|Branch_,Declaration_|Declaration_,Branch_->falselethash=function|Branchselector->Selector.hashselector|Declarationdeclaration->Declaration.hashdeclarationend)(* Where each token was last printed, by its rank among the tokens read. *)typewalk={seen:intSeen.t;mutablenext:int;mutablecost:int}(* The tokens of one selector list or one body, printed together. A token
printed before starts a match, unless the token before it was printed before
too, right ahead of where this one was: then the match goes on. [previous] is
the rank the token before was last printed at, when it was printed before. *)letgroupwalk~match_cost~match_bytetokens=ignore(List.fold_left(funprevious(token,size)->letbytes=size+1inletrank=walk.nextinwalk.next<-rank+1;letearlier=Seen.find_optwalk.seentokeninSeen.replacewalk.seentokenrank;matchearlierwith|Someearlier->letstart=matchpreviouswith|Somepreviouswhenearlier=previous+1->0|Some_|None->match_costinwalk.cost<-walk.cost+start+(match_byte*bytes);Someearlier|None->walk.cost<-walk.cost+novel+(novel_byte*bytes);None)Nonetokens)letrulesprint(rules:Stylesheet.rulelist)=letwalk={seen=Seen.create16;next=0;cost=0}inList.iter(fun(rule:Stylesheet.rule)->groupwalk~match_cost:selector_match~match_byte:selector_match_byte(List.map(funbranch->(Branchbranch,Size.selectorbranch))(Edge.selectorsrule.selector));groupwalk~match_cost:declaration_match~match_byte:declaration_match_byte(List.map(fundeclaration->(Declarationdeclaration,Size.declprintdeclaration))rule.declarations))rules;walk.cost