[ruby/prism] Significantly faster offset cache for parser
https://github.com/ruby/prism/commit/8cd92eef79
This commit is contained in:
parent
5c2d96df19
commit
cf1cd215c0
@ -124,20 +124,21 @@ module Prism
|
|||||||
# build the parser gem AST.
|
# build the parser gem AST.
|
||||||
#
|
#
|
||||||
# If the bytesize of the source is the same as the length, then we can
|
# If the bytesize of the source is the same as the length, then we can
|
||||||
# just use the offset directly. Otherwise, we build a hash that functions
|
# just use the offset directly. Otherwise, we build an array where the
|
||||||
# as a cache for the conversion.
|
# index is the byte offset and the value is the character offset.
|
||||||
#
|
|
||||||
# This is a good opportunity for some optimizations. If the source file
|
|
||||||
# has any multi-byte characters, this can tank the performance of the
|
|
||||||
# translator. We could make this significantly faster by using a
|
|
||||||
# different data structure for the cache.
|
|
||||||
def build_offset_cache(source)
|
def build_offset_cache(source)
|
||||||
if source.bytesize == source.length
|
if source.bytesize == source.length
|
||||||
-> (offset) { offset }
|
-> (offset) { offset }
|
||||||
else
|
else
|
||||||
Hash.new do |hash, offset|
|
offset_cache = []
|
||||||
hash[offset] = source.byteslice(0, offset).length
|
offset = 0
|
||||||
|
|
||||||
|
source.each_char do |char|
|
||||||
|
char.bytesize.times { offset_cache << offset }
|
||||||
|
offset += 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
offset_cache << offset
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user