QuerySubExprEliminator.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //-----------------------------------------------------------------------------
  4. namespace System.ServiceModel.Dispatcher
  5. {
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Xml.XPath;
  11. internal class SubExpr
  12. {
  13. internal int var;
  14. internal int refCount; // opcodes
  15. internal bool useSpecial;
  16. Opcode ops;
  17. SubExpr parent;
  18. protected List<SubExpr> children;
  19. internal SubExpr(SubExpr parent, Opcode ops, int var)
  20. {
  21. this.children = new List<SubExpr>(2);
  22. this.var = var;
  23. this.parent = parent;
  24. this.useSpecial = false;
  25. if (parent != null)
  26. {
  27. this.ops = new InternalSubExprOpcode(parent);
  28. this.ops.Attach(ops);
  29. this.useSpecial = parent is SubExprHeader && ((SelectOpcode)ops).Criteria.Axis.Type == QueryAxisType.Child;
  30. }
  31. else
  32. {
  33. this.ops = ops;
  34. }
  35. }
  36. internal Opcode FirstOp
  37. {
  38. get
  39. {
  40. if (this.parent == null)
  41. {
  42. return this.ops;
  43. }
  44. else
  45. {
  46. return this.ops.Next;
  47. }
  48. }
  49. }
  50. internal int Variable
  51. {
  52. get
  53. {
  54. return this.var;
  55. }
  56. }
  57. internal SubExprOpcode Add(Opcode opseq, SubExprEliminator elim)
  58. {
  59. Opcode start = this.FirstOp;
  60. Opcode ops = opseq;
  61. while (start != null && ops != null && start.Equals(ops))
  62. {
  63. start = start.Next;
  64. ops = ops.Next;
  65. }
  66. if (ops == null)
  67. {
  68. if (start == null)
  69. {
  70. return new SubExprOpcode(this);
  71. }
  72. else
  73. {
  74. SubExpr e = this.BranchAt(start, elim);
  75. return new SubExprOpcode(e);
  76. }
  77. }
  78. else
  79. {
  80. if (start == null)
  81. {
  82. ops.DetachFromParent();
  83. for (int i = 0; i < this.children.Count; ++i)
  84. {
  85. if (this.children[i].FirstOp.Equals(ops))
  86. {
  87. return this.children[i].Add(ops, elim);
  88. }
  89. }
  90. SubExpr e = new SubExpr(this, ops, elim.NewVarID());
  91. this.AddChild(e);
  92. return new SubExprOpcode(e);
  93. }
  94. else
  95. {
  96. SubExpr e = this.BranchAt(start, elim);
  97. ops.DetachFromParent();
  98. SubExpr ee = new SubExpr(e, ops, elim.NewVarID());
  99. e.AddChild(ee);
  100. return new SubExprOpcode(ee);
  101. }
  102. }
  103. }
  104. internal virtual void AddChild(SubExpr expr)
  105. {
  106. this.children.Add(expr);
  107. }
  108. SubExpr BranchAt(Opcode op, SubExprEliminator elim)
  109. {
  110. Opcode firstOp = this.FirstOp;
  111. if (this.parent != null)
  112. {
  113. this.parent.RemoveChild(this);
  114. }
  115. else
  116. {
  117. elim.Exprs.Remove(this);
  118. }
  119. firstOp.DetachFromParent();
  120. op.DetachFromParent();
  121. SubExpr e = new SubExpr(this.parent, firstOp, elim.NewVarID());
  122. if (this.parent != null)
  123. {
  124. this.parent.AddChild(e);
  125. }
  126. else
  127. {
  128. elim.Exprs.Add(e);
  129. }
  130. e.AddChild(this);
  131. this.parent = e;
  132. this.ops = new InternalSubExprOpcode(e);
  133. this.ops.Attach(op);
  134. return e;
  135. }
  136. internal void CleanUp(SubExprEliminator elim)
  137. {
  138. if (this.refCount == 0)
  139. {
  140. if (this.children.Count == 0)
  141. {
  142. if (this.parent == null)
  143. {
  144. elim.Exprs.Remove(this);
  145. }
  146. else
  147. {
  148. this.parent.RemoveChild(this);
  149. this.parent.CleanUp(elim);
  150. }
  151. }
  152. else if (this.children.Count == 1)
  153. {
  154. SubExpr child = this.children[0];
  155. Opcode op = child.FirstOp;
  156. op.DetachFromParent();
  157. Opcode op2 = this.ops;
  158. while (op2.Next != null)
  159. {
  160. op2 = op2.Next;
  161. }
  162. op2.Attach(op);
  163. child.ops = this.ops;
  164. if (this.parent == null)
  165. {
  166. elim.Exprs.Remove(this);
  167. elim.Exprs.Add(child);
  168. child.parent = null;
  169. }
  170. else
  171. {
  172. this.parent.RemoveChild(this);
  173. this.parent.AddChild(child);
  174. child.parent = this.parent;
  175. }
  176. }
  177. }
  178. }
  179. internal void DecRef(SubExprEliminator elim)
  180. {
  181. this.refCount--;
  182. CleanUp(elim);
  183. }
  184. internal void Eval(ProcessingContext context)
  185. {
  186. int count = 0, marker = context.Processor.CounterMarker;
  187. Opcode op = this.ops;
  188. if (this.useSpecial)
  189. {
  190. op.EvalSpecial(context);
  191. context.LoadVariable(this.var);
  192. //context.Processor.CounterMarker = marker;
  193. return;
  194. }
  195. while (op != null)
  196. {
  197. op = op.Eval(context);
  198. }
  199. count = context.Processor.ElapsedCount(marker);
  200. //context.Processor.CounterMarker = marker;
  201. context.SaveVariable(this.var, count);
  202. }
  203. internal virtual void EvalSpecial(ProcessingContext context)
  204. {
  205. this.Eval(context);
  206. }
  207. internal void IncRef()
  208. {
  209. this.refCount++;
  210. }
  211. internal virtual void RemoveChild(SubExpr expr)
  212. {
  213. this.children.Remove(expr);
  214. }
  215. internal void Renumber(SubExprEliminator elim)
  216. {
  217. this.var = elim.NewVarID();
  218. for (int i = 0; i < this.children.Count; ++i)
  219. {
  220. this.children[i].Renumber(elim);
  221. }
  222. }
  223. internal void Trim()
  224. {
  225. this.children.Capacity = this.children.Count;
  226. this.ops.Trim();
  227. for (int i = 0; i < this.children.Count; ++i)
  228. {
  229. this.children[i].Trim();
  230. }
  231. }
  232. #if DEBUG_FILTER
  233. internal void Write(TextWriter outStream)
  234. {
  235. outStream.WriteLine("=======================");
  236. outStream.WriteLine("= SubExpr #" + this.var.ToString() + " (" + this.refCount.ToString() + ")");
  237. outStream.WriteLine("=======================");
  238. for(Opcode o = this.ops; o != null; o = o.Next)
  239. {
  240. outStream.WriteLine(o.ToString());
  241. }
  242. outStream.WriteLine("");
  243. for(int i = 0; i < this.children.Count; ++i)
  244. {
  245. this.children[i].Write(outStream);
  246. }
  247. }
  248. #endif
  249. }
  250. internal class SubExprHeader : SubExpr
  251. {
  252. // WS, [....], Can probably combine these
  253. // WS, [....], Make this data structure less ugly (if possible)
  254. Dictionary<string, Dictionary<string, List<SubExpr>>> nameLookup;
  255. Dictionary<SubExpr, MyInt> indexLookup;
  256. internal SubExprHeader(Opcode ops, int var)
  257. : base(null, ops, var)
  258. {
  259. this.nameLookup = new Dictionary<string, Dictionary<string, List<SubExpr>>>();
  260. this.indexLookup = new Dictionary<SubExpr, MyInt>();
  261. this.IncRef(); // Prevent cleanup
  262. }
  263. internal override void AddChild(SubExpr expr)
  264. {
  265. base.AddChild(expr);
  266. RebuildIndex();
  267. if (expr.useSpecial)
  268. {
  269. NodeQName qname = ((SelectOpcode)(expr.FirstOp)).Criteria.QName;
  270. string ns = qname.Namespace;
  271. Dictionary<string, List<SubExpr>> nextLookup;
  272. if (!this.nameLookup.TryGetValue(ns, out nextLookup))
  273. {
  274. nextLookup = new Dictionary<string, List<SubExpr>>();
  275. this.nameLookup.Add(ns, nextLookup);
  276. }
  277. string name = qname.Name;
  278. List<SubExpr> exprs = new List<SubExpr>();
  279. if (!nextLookup.TryGetValue(name, out exprs))
  280. {
  281. exprs = new List<SubExpr>();
  282. nextLookup.Add(name, exprs);
  283. }
  284. exprs.Add(expr);
  285. }
  286. }
  287. internal override void EvalSpecial(ProcessingContext context)
  288. {
  289. int marker = context.Processor.CounterMarker;
  290. if (!context.LoadVariable(this.var))
  291. {
  292. XPathMessageContext.HeaderFun.InvokeInternal(context, 0);
  293. context.SaveVariable(this.var, context.Processor.ElapsedCount(marker));
  294. }
  295. // WS, [....], see if we can put this array in the processor to save
  296. // an allocation. Perhaps we can use the variables slot we're going to fill
  297. NodeSequence[] childSequences = new NodeSequence[this.children.Count];
  298. NodeSequence seq = context.Sequences[context.TopSequenceArg.basePtr].Sequence;
  299. for (int i = 0; i < this.children.Count; ++i)
  300. {
  301. childSequences[i] = context.CreateSequence();
  302. childSequences[i].StartNodeset();
  303. }
  304. // Perform the index
  305. SeekableXPathNavigator nav = seq[0].GetNavigator();
  306. if (nav.MoveToFirstChild())
  307. {
  308. do
  309. {
  310. if (nav.NodeType == XPathNodeType.Element)
  311. {
  312. List<SubExpr> lst;
  313. string name = nav.LocalName;
  314. string ns = nav.NamespaceURI;
  315. Dictionary<string, List<SubExpr>> nextLookup;
  316. if (this.nameLookup.TryGetValue(ns, out nextLookup))
  317. {
  318. if (nextLookup.TryGetValue(name, out lst))
  319. {
  320. for (int i = 0; i < lst.Count; ++i)
  321. {
  322. childSequences[this.indexLookup[lst[i]].i].Add(nav);
  323. }
  324. }
  325. if (nextLookup.TryGetValue(QueryDataModel.Wildcard, out lst))
  326. {
  327. for (int i = 0; i < lst.Count; ++i)
  328. {
  329. childSequences[this.indexLookup[lst[i]].i].Add(nav);
  330. }
  331. }
  332. }
  333. if (this.nameLookup.TryGetValue(QueryDataModel.Wildcard, out nextLookup))
  334. {
  335. if (nextLookup.TryGetValue(QueryDataModel.Wildcard, out lst))
  336. {
  337. for (int i = 0; i < lst.Count; ++i)
  338. {
  339. childSequences[this.indexLookup[lst[i]].i].Add(nav);
  340. }
  341. }
  342. }
  343. }
  344. } while (nav.MoveToNext());
  345. }
  346. int secondMarker = context.Processor.CounterMarker;
  347. for (int i = 0; i < this.children.Count; ++i)
  348. {
  349. if (this.children[i].useSpecial)
  350. {
  351. childSequences[i].StopNodeset();
  352. context.Processor.CounterMarker = secondMarker;
  353. context.PushSequenceFrame();
  354. context.PushSequence(childSequences[i]);
  355. Opcode op = this.children[i].FirstOp.Next;
  356. while (op != null)
  357. {
  358. op = op.Eval(context);
  359. }
  360. context.SaveVariable(this.children[i].var, context.Processor.ElapsedCount(marker));
  361. context.PopSequenceFrame();
  362. }
  363. else
  364. {
  365. context.ReleaseSequence(childSequences[i]);
  366. //context.SetVariable(this.children[i].Variable, null, 0);
  367. }
  368. }
  369. context.Processor.CounterMarker = marker;
  370. }
  371. internal void RebuildIndex()
  372. {
  373. this.indexLookup.Clear();
  374. for (int i = 0; i < this.children.Count; ++i)
  375. {
  376. this.indexLookup.Add(this.children[i], new MyInt(i));
  377. }
  378. }
  379. internal override void RemoveChild(SubExpr expr)
  380. {
  381. base.RemoveChild(expr);
  382. RebuildIndex();
  383. if (expr.useSpecial)
  384. {
  385. NodeQName qname = ((SelectOpcode)(expr.FirstOp)).Criteria.QName;
  386. string ns = qname.Namespace;
  387. Dictionary<string, List<SubExpr>> nextLookup;
  388. if (this.nameLookup.TryGetValue(ns, out nextLookup))
  389. {
  390. string name = qname.Name;
  391. List<SubExpr> exprs;
  392. if (nextLookup.TryGetValue(name, out exprs))
  393. {
  394. exprs.Remove(expr);
  395. if (exprs.Count == 0)
  396. {
  397. nextLookup.Remove(name);
  398. }
  399. }
  400. if (nextLookup.Count == 0)
  401. {
  402. this.nameLookup.Remove(ns);
  403. }
  404. }
  405. }
  406. }
  407. internal class MyInt
  408. {
  409. internal int i;
  410. internal MyInt(int i)
  411. {
  412. this.i = i;
  413. }
  414. }
  415. }
  416. internal class SubExprEliminator
  417. {
  418. List<SubExpr> exprList;
  419. int nextVar;
  420. Dictionary<object, List<SubExpr>> removalMapping;
  421. internal SubExprEliminator()
  422. {
  423. this.removalMapping = new Dictionary<object, List<SubExpr>>();
  424. this.exprList = new List<SubExpr>();
  425. Opcode op = new XPathMessageFunctionCallOpcode(XPathMessageContext.HeaderFun, 0);
  426. SubExprHeader header = new SubExprHeader(op, 0);
  427. this.exprList.Add(header);
  428. this.nextVar = 1;
  429. }
  430. internal List<SubExpr> Exprs
  431. {
  432. get
  433. {
  434. return this.exprList;
  435. }
  436. }
  437. internal int VariableCount
  438. {
  439. get
  440. {
  441. return this.nextVar;
  442. }
  443. }
  444. internal Opcode Add(object item, Opcode ops)
  445. {
  446. List<SubExpr> exprs = new List<SubExpr>();
  447. this.removalMapping.Add(item, exprs);
  448. while (ops.Next != null)
  449. {
  450. ops = ops.Next;
  451. }
  452. Opcode res = ops;
  453. while (ops != null)
  454. {
  455. if (IsExprStarter(ops))
  456. {
  457. Opcode start = ops;
  458. Opcode p = ops.Prev;
  459. ops.DetachFromParent();
  460. ops = ops.Next;
  461. while (ops.ID == OpcodeID.Select)
  462. {
  463. ops = ops.Next;
  464. }
  465. ops.DetachFromParent();
  466. SubExpr e = null;
  467. for (int i = 0; i < this.exprList.Count; ++i)
  468. {
  469. if (this.exprList[i].FirstOp.Equals(start))
  470. {
  471. e = this.exprList[i];
  472. break;
  473. }
  474. }
  475. SubExprOpcode o;
  476. if (e == null)
  477. {
  478. e = new SubExpr(null, start, NewVarID());
  479. this.exprList.Add(e);
  480. o = new SubExprOpcode(e);
  481. }
  482. else
  483. {
  484. o = e.Add(start, this);
  485. }
  486. o.Expr.IncRef();
  487. exprs.Add(o.Expr);
  488. o.Attach(ops);
  489. ops = o;
  490. if (p != null)
  491. {
  492. p.Attach(ops);
  493. }
  494. }
  495. res = ops;
  496. ops = ops.Prev;
  497. }
  498. return res;
  499. }
  500. internal static bool IsExprStarter(Opcode op)
  501. {
  502. if (op.ID == OpcodeID.SelectRoot)
  503. {
  504. return true;
  505. }
  506. if (op.ID == OpcodeID.XsltInternalFunction)
  507. {
  508. XPathMessageFunctionCallOpcode fop = (XPathMessageFunctionCallOpcode)op;
  509. if (fop.ReturnType == XPathResultType.NodeSet && fop.ArgCount == 0)
  510. {
  511. return true;
  512. }
  513. }
  514. return false;
  515. }
  516. internal int NewVarID()
  517. {
  518. return nextVar++;
  519. }
  520. internal void Remove(object item)
  521. {
  522. List<SubExpr> exprs;
  523. if (this.removalMapping.TryGetValue(item, out exprs))
  524. {
  525. for (int i = 0; i < exprs.Count; ++i)
  526. {
  527. exprs[i].DecRef(this);
  528. }
  529. this.removalMapping.Remove(item);
  530. Renumber();
  531. }
  532. }
  533. void Renumber()
  534. {
  535. this.nextVar = 0;
  536. for (int i = 0; i < this.exprList.Count; ++i)
  537. {
  538. this.exprList[i].Renumber(this);
  539. }
  540. }
  541. internal void Trim()
  542. {
  543. this.exprList.Capacity = this.exprList.Count;
  544. for (int i = 0; i < this.exprList.Count; ++i)
  545. {
  546. this.exprList[i].Trim();
  547. }
  548. }
  549. #if DEBUG_FILTER
  550. internal void Write(TextWriter outStream)
  551. {
  552. for(int i = 0; i < this.exprList.Count; ++i)
  553. {
  554. this.exprList[i].Write(outStream);
  555. }
  556. }
  557. #endif
  558. }
  559. internal class SubExprOpcode : Opcode
  560. {
  561. protected SubExpr expr;
  562. internal SubExprOpcode(SubExpr expr)
  563. : base(OpcodeID.SubExpr)
  564. {
  565. this.expr = expr;
  566. }
  567. internal SubExpr Expr
  568. {
  569. get
  570. {
  571. return expr;
  572. }
  573. }
  574. internal override bool Equals(Opcode op)
  575. {
  576. if (base.Equals(op))
  577. {
  578. SubExprOpcode sop = op as SubExprOpcode;
  579. if (sop != null)
  580. {
  581. return this.expr == sop.expr;
  582. }
  583. }
  584. return false;
  585. }
  586. internal override Opcode Eval(ProcessingContext context)
  587. {
  588. if (!context.LoadVariable(this.expr.Variable))
  589. {
  590. context.PushSequenceFrame();
  591. NodeSequence seq = context.CreateSequence();
  592. seq.Add(context.Processor.ContextNode);
  593. context.PushSequence(seq);
  594. int marker = context.Processor.CounterMarker;
  595. try
  596. {
  597. this.expr.Eval(context);
  598. }
  599. catch (XPathNavigatorException e)
  600. {
  601. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(e.Process(this));
  602. }
  603. catch (NavigatorInvalidBodyAccessException e)
  604. {
  605. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(e.Process(this));
  606. }
  607. context.Processor.CounterMarker = marker;
  608. context.PopSequenceFrame();
  609. context.PopSequenceFrame();
  610. context.LoadVariable(this.expr.Variable);
  611. }
  612. return this.next;
  613. }
  614. internal override Opcode EvalSpecial(ProcessingContext context)
  615. {
  616. try
  617. {
  618. this.expr.EvalSpecial(context);
  619. }
  620. catch (XPathNavigatorException e)
  621. {
  622. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(e.Process(this));
  623. }
  624. catch (NavigatorInvalidBodyAccessException e)
  625. {
  626. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(e.Process(this));
  627. }
  628. return this.next;
  629. }
  630. #if DEBUG_FILTER
  631. public override string ToString()
  632. {
  633. return string.Format("{0} #{1}", base.ToString(), this.expr.Variable.ToString());
  634. }
  635. #endif
  636. }
  637. internal class InternalSubExprOpcode : SubExprOpcode
  638. {
  639. internal InternalSubExprOpcode(SubExpr expr)
  640. : base(expr)
  641. {
  642. }
  643. internal override Opcode Eval(ProcessingContext context)
  644. {
  645. if (!context.LoadVariable(this.expr.Variable))
  646. {
  647. this.expr.Eval(context);
  648. }
  649. return this.next;
  650. }
  651. internal override Opcode EvalSpecial(ProcessingContext context)
  652. {
  653. this.expr.EvalSpecial(context);
  654. return this.next;
  655. }
  656. }
  657. }