tagua-vm / parser

Safe, fast and memory efficient PHP parser (lexical and syntactic analysers, and the Abstract Syntax Tree)
http://tagua.io/
119 stars 15 forks source link

Literal: Avoid string copies by default #107

Closed Hywan closed 7 years ago

Hywan commented 7 years ago

A string is no longer copied by default. A single quoted string is copied if and only if it contains an escaped character, because the resulting string must not contain the escaping character, and this is not possible to concatenate slices, so the new string is copied inside a Vec<u8>. However, if no character is escaped, or if it is a nowdoc string, there is no longer any copy.

Behind the scene, we use a clone-on-write smart pointer: std::borrow::Cow.

A benchmark needs to validate this work though, but I am confident it will improve the global performance, let's see.

Interesting commits are:

cc @jubianchi @Geal

Hywan commented 7 years ago

Bench results.

Before:

test rules::literals::tests::bench_simple  ... bench:       2,939 ns/iter (+/- 1,086)

After:

test rules::literals::tests::bench_simple  ... bench:       2,792 ns/iter (+/- 548)

tested with a string of size 5k. This is a boost of 5%.

For smaller strings of size 12 bytes, the boost is of 20%. Well, the benchmarks are less relevant in this case. I have done 10 runs, and the boost is constantly about 20%.

For escaped strings, so with a copy, strangely these is a constant boost of 1-2%. I don't know why, but I take it.

coveralls commented 7 years ago

Coverage Status

Coverage decreased (-0.07%) to 93.433% when pulling bc89efb2bee11fe14480a747ce43f3653c88f1e7 on Hywan:string_cow into 0de8ace0e34df8811ee130783fab0338e1aab3f8 on tagua-vm:master.

coveralls commented 7 years ago

Coverage Status

Coverage decreased (-0.07%) to 93.433% when pulling bc89efb2bee11fe14480a747ce43f3653c88f1e7 on Hywan:string_cow into 0de8ace0e34df8811ee130783fab0338e1aab3f8 on tagua-vm:master.

coveralls commented 7 years ago

Coverage Status

Coverage decreased (-0.07%) to 93.433% when pulling bc89efb2bee11fe14480a747ce43f3653c88f1e7 on Hywan:string_cow into 0de8ace0e34df8811ee130783fab0338e1aab3f8 on tagua-vm:master.

coveralls commented 7 years ago

Coverage Status

Coverage decreased (-0.07%) to 93.433% when pulling bc89efb2bee11fe14480a747ce43f3653c88f1e7 on Hywan:string_cow into 0de8ace0e34df8811ee130783fab0338e1aab3f8 on tagua-vm:master.

coveralls commented 7 years ago

Coverage Status

Coverage decreased (-0.07%) to 93.433% when pulling bc89efb2bee11fe14480a747ce43f3653c88f1e7 on Hywan:string_cow into 0de8ace0e34df8811ee130783fab0338e1aab3f8 on tagua-vm:master.