xml_example.odin 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package xml_example
  2. import "core:encoding/xml"
  3. import "core:mem"
  4. import "core:fmt"
  5. import "core:time"
  6. import "core:strings"
  7. import "core:hash"
  8. N :: 1
  9. example :: proc() {
  10. using fmt
  11. docs: [N]^xml.Document
  12. errs: [N]xml.Error
  13. times: [N]time.Duration
  14. defer for round in 0..<N {
  15. xml.destroy(docs[round])
  16. }
  17. DOC :: #load("../../../../tests/core/assets/XML/utf8.xml")
  18. input := DOC
  19. for round in 0..<N {
  20. start := time.tick_now()
  21. docs[round], errs[round] = xml.parse(input, xml.Options{
  22. flags={.Ignore_Unsupported},
  23. expected_doctype = "",
  24. })
  25. end := time.tick_now()
  26. times[round] = time.tick_diff(start, end)
  27. }
  28. fastest := max(time.Duration)
  29. slowest := time.Duration(0)
  30. total := time.Duration(0)
  31. for round in 0..<N {
  32. fastest = min(fastest, times[round])
  33. slowest = max(slowest, times[round])
  34. total += times[round]
  35. }
  36. fastest_ms := time.duration_milliseconds(fastest)
  37. slowest_ms := time.duration_milliseconds(slowest)
  38. average_ms := time.duration_milliseconds(time.Duration(f64(total) / f64(N)))
  39. fastest_speed := (f64(1000.0) / fastest_ms) * f64(len(DOC)) / 1_024.0 / 1_024.0
  40. slowest_speed := (f64(1000.0) / slowest_ms) * f64(len(DOC)) / 1_024.0 / 1_024.0
  41. average_speed := (f64(1000.0) / average_ms) * f64(len(DOC)) / 1_024.0 / 1_024.0
  42. fmt.printf("N = %v\n", N)
  43. fmt.printf("[Fastest]: %v bytes in %.2f ms (%.2f MiB/s).\n", len(input), fastest_ms, fastest_speed)
  44. fmt.printf("[Slowest]: %v bytes in %.2f ms (%.2f MiB/s).\n", len(input), slowest_ms, slowest_speed)
  45. fmt.printf("[Average]: %v bytes in %.2f ms (%.2f MiB/s).\n", len(input), average_ms, average_speed)
  46. if errs[0] != .None {
  47. printf("Load/Parse error: %v\n", errs[0])
  48. if errs[0] == .File_Error {
  49. println("\"unicode.xml\" not found. Did you run \"tests\\download_assets.py\"?")
  50. }
  51. return
  52. }
  53. charlist, charlist_ok := xml.find_child_by_ident(docs[0], 0, "charlist")
  54. if !charlist_ok {
  55. eprintln("Could not locate top-level `<charlist>` tag.")
  56. return
  57. }
  58. printf("Found `<charlist>` with %v children, %v elements total\n", len(docs[0].elements[charlist].value), docs[0].element_count)
  59. crc32 := doc_hash(docs[0], false)
  60. printf("[%v] CRC32: 0x%08x\n", "🎉" if crc32 == 0x420dbac5 else "🤬", crc32)
  61. for round in 0..<N {
  62. defer xml.destroy(docs[round])
  63. }
  64. }
  65. doc_hash :: proc(doc: ^xml.Document, print := false) -> (crc32: u32) {
  66. buf: strings.Builder
  67. defer strings.builder_destroy(&buf)
  68. w := strings.to_writer(&buf)
  69. xml.print(w, doc)
  70. tree := strings.to_string(buf)
  71. if print { fmt.println(tree) }
  72. return hash.crc32(transmute([]u8)tree)
  73. }
  74. main :: proc() {
  75. using fmt
  76. track: mem.Tracking_Allocator
  77. mem.tracking_allocator_init(&track, context.allocator)
  78. context.allocator = mem.tracking_allocator(&track)
  79. example()
  80. if len(track.allocation_map) > 0 {
  81. println()
  82. for _, v in track.allocation_map {
  83. printf("%v Leaked %v bytes.\n", v.location, v.size)
  84. }
  85. }
  86. println("Done and cleaned up!")
  87. }