1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or is a part of
11 # Injects stat-calculating functionalities to Text and its variants
13 # Every allocation is counted for each available type of Text in Core
15 # Cached operations are monitored and statistics of their use are printed
16 # at the end of the execution of a program
19 intrude import core
::text
::ropes
24 # Counts the number of allocations of UnicodeFlatString
25 var uniflatstr_allocations
= 0
27 # Counts the number of allocations of ASCIIFlatString
28 var asciiflatstr_allocations
= 0
30 # Counts the number of allocations of FlatBuffer
31 var flatbuf_allocations
= 0
33 # Counts the number of allocations of Concat
34 var concat_allocations
= 0
36 # Counts the number of calls to property length
37 var length_calls
= new Counter[String]
39 # Counts the number of length calls that missed the cache
40 var length_cache_miss
= new Counter[String]
42 # Counts the number of call to index on a Text
43 var index_call
= new Counter[String]
45 # Count the number of times that an indexed access
46 # on a Concat caused a regeneration of the cache
47 var concat_cache_miss
= 0
49 # Distance between characters when looking for a character in a FlatString
50 var index_len
= new Counter[Int]
52 # Length (bytes) of the FlatString created by lib
53 var str_byte_length
= new Counter[Int]
55 # Counter of the times `byte_length` is called on FlatString
56 var byte_length_call
= new Counter[String]
58 # Counter of the times `bytepos` is called on each type of receiver
59 var position_call
= new Counter[String]
61 # Counter of the times `bytepos` is called on each type of receiver
62 var bytepos_call
= new Counter[String]
64 # Calls to the `first_byte` property of a FlatString
65 var first_byte_call
= 0
67 # Calls to the `last_byte` property of a FlatString
68 var last_byte_call
= 0
70 # Number of strings created with full length created
71 var str_full_created
= 0
73 private fun show_string_stats
do
79 print
"\t-UnicodeFlatString = {uniflatstr_allocations}"
80 print
"\t-ASCIIFlatString = {asciiflatstr_allocations}"
81 print
"\t-FlatBuffer = {flatbuf_allocations}"
82 print
"\t-Concat = {concat_allocations}"
84 print
"Calls to length, by type:"
85 for k
, v
in length_calls
do
87 if k
== "UnicodeFlatString" then printn
" (cache misses {length_cache_miss[k]}, {div(length_cache_miss[k] * 100, v)}%)"
90 print
"Indexed accesses, by type:"
91 for k
, v
in index_call
do
93 if k
== "Concat" then printn
" (cache misses {concat_cache_miss}, {div(concat_cache_miss * 100, v)}%)"
97 print
"Calls to byte_length for each type:"
98 for k
, v
in byte_length_call
do
102 print
"Calls to position for each type:"
103 for k
, v
in position_call
do
107 print
"Calls to bytepos for each type:"
108 for k
, v
in bytepos_call
do
112 print
"Calls to first_byte on FlatString {first_byte_call}"
113 print
"Calls to last_byte on FlatString {last_byte_call}"
115 print
"Length of travel for index distribution:"
116 index_len
.print_content
118 print
"Byte length of the FlatStrings created:"
119 str_byte_length
.print_content
135 sys
.concat_allocations
+= 1
138 redef fun byte_length
do
139 sys
.byte_length_call
.inc
"Concat"
144 sys
.index_call
.inc
"Concat"
145 if flat_last_pos_start
!= -1 then
146 var fsp
= i
- flat_last_pos_start
147 if fsp
>= 0 and fsp
< flat_cache
.length
then return flat_cache
[fsp
]
149 sys
.concat_cache_miss
+= 1
153 if s
isa FlatString then break
156 var llen
= lft
.length
164 flat_last_pos_start
= st
- i
171 redef fun char_to_byte_index
(index
) do
176 # Find best insertion point
177 var delta_begin
= index
178 var delta_end
= (ln
- 1) - index
179 var delta_cache
= (position
- index
).abs
180 var min
= delta_begin
183 if delta_cache
< min
then min
= delta_cache
184 if delta_end
< min
then min
= delta_end
189 if min
== delta_begin
then
192 else if min
== delta_cache
then
196 ns_i
= its
.find_beginning_of_char_at
(last_byte
)
202 ns_i
= its
.char_to_byte_index_cached
(index
, my_i
, ns_i
)
206 sys
.index_len
.inc
((after
- from
).abs
)
215 redef class FlatBuffer
218 sys
.flatbuf_allocations
+= 1
222 sys
.bytepos_call
.inc
"FlatBuffer"
226 redef fun bytepos
=(p
) do
227 sys
.bytepos_call
.inc
"FlatBuffer"
231 redef fun position
do
232 sys
.position_call
.inc
"FlatBuffer"
236 redef fun position
=(p
) do
237 sys
.position_call
.inc
"FlatBuffer"
241 redef fun byte_length
do
242 sys
.byte_length_call
.inc
"FlatBuffer"
247 sys
.length_calls
.inc
"FlatBuffer"
252 sys
.index_call
.inc
"FlatBuffer"
256 redef fun char_to_byte_index
(i
) do
257 sys
.index_call
.inc
"FlatBuffer"
262 redef class FlatString
265 sys
.bytepos_call
.inc
"FlatString"
269 redef fun bytepos
=(p
) do
270 sys
.bytepos_call
.inc
"FlatString"
274 redef fun position
do
275 sys
.position_call
.inc
"FlatString"
279 redef fun position
=(p
) do
280 sys
.position_call
.inc
"FlatString"
284 redef fun byte_length
do
285 sys
.byte_length_call
.inc
"FlatString"
289 redef fun first_byte
do
290 sys
.first_byte_call
+= 1
294 redef fun first_byte
=(v
) do
295 sys
.first_byte_call
+= 1
299 redef fun last_byte
do
300 sys
.last_byte_call
+= 1
304 private var length_cache
: nullable Int = null
307 sys
.length_calls
.inc
"FlatString"
309 if l
!= null then return l
310 sys
.length_cache_miss
.inc
"FlatString"
311 if byte_length
== 0 then return 0
317 st
+= its
.length_of_char_at
(st
)
324 redef fun char_to_byte_index
(i
) do
325 sys
.index_call
.inc
"FlatString"
330 redef class ASCIIFlatString
331 redef init full_data
(items
, byte_length
, from
, length
)
334 sys
.asciiflatstr_allocations
+= 1
335 sys
.str_full_created
+= 1
339 redef class UnicodeFlatString
340 redef init full_data
(items
, byte_length
, from
, length
)
343 sys
.uniflatstr_allocations
+= 1
344 sys
.str_full_created
+= 1