Comments (5)
DelayedFormat
is a temporary object and should not be stored. A recommended way to store the items statically is via a static array of Item<'static>
:
pub fn to_rfc2822(&self) -> String {
const ITEMS: &'static [Item<'static>] = &[Item::Fixed(Fixed::RFC2822)];
self.format_with_items(ITEMS.iter().cloned()).to_string()
}
(Incidentally, this is the actual implementation of DateTime::to_rfc2822
.)
from chrono.
Yes, I see that in the docs. Your example is for static strings, however I need to precompile format and store at runtime. Since we can't store buffer and compiled DelayedFormat
in the same struct:
struct Foo {
pattern: String, // <---------- The 'a is here or the whole struct, no matter, it isn't possible to compile in safe land.
format: DelayedFormat<'a>, // 'a
}
The only way is to implement it manually.
For example:
// No lifetime, can put it in the box of trait with no `+'a`.
trait Bar {}
struct Foo {
pattern: DelayedFormatBuf,
}
impl Foo {
fn new(patern: String) -> Foo {
Foo {
pattern: DelayedFormatBuf::compile(&pattern),
}
}
}
impl Bar for Foo {}
It is useful when you need to keep traits in the Box without lifetime specifier.
from chrono.
@3Hren Oh, that makes sense. It would be solvable by make Item::{Literal, Space}
accept a Cow<'a, str>
instead of &'a str
. I still doubt the usefulness of it, though, since the parsing of format string is rarely the slowest part of formatting (pluggable Item
is mainly provided for allowing different types of format strings). Do you have a significant performance degradation from not having owned Item
s?
from chrono.
Well, the following benchmarks shows that there isn't such difference as I thought:
#![feature(test)]
extern crate test;
extern crate chrono;
use std::io::Write;
use test::Bencher;
use chrono::UTC;
#[bench]
fn interpreter(b: &mut Bencher) {
let now = UTC::now();
let mut buf = Vec::with_capacity(128);
let pattern = "%Y-%m-%d %H:%M:%S.%.6f".to_owned();
b.iter(|| {
write!(&mut buf, "{}", now.format(&pattern)).unwrap();
buf.clear();
});
}
#[bench]
fn compiler(b: &mut Bencher) {
let now = UTC::now();
let mut buf = Vec::with_capacity(128);
let pattern = "%Y-%m-%d %H:%M:%S.%.6f".to_owned();
let format = now.format(&pattern);
b.iter(|| {
write!(&mut buf, "{}", format).unwrap();
buf.clear();
});
}
test compiler ... bench: 843 ns/iter (+/- 58)
test interpreter ... bench: 986 ns/iter (+/- 338)
This is strange, because my manual implementation for C++ gives:
datetime.strftime 918 ns/iter
datetime.manual[real] 66 ns/iter
I need to investigate this behavior more... You're right, making this struct owned doesn't gives significant performance boost. You are free to close this issue, thanks!
from chrono.
Ah, I see here that you reparse the given pattern each time iteration occurs, which seems unnecessary, because the specified pattern is immutable and parsing can be performed only once to generate tokens.
Something like this:
pattern(&'a str) -> &[Token<'a>].
enum Token<'a> {
Literal(&str),
YearYYYYPlaceholder,
YearYYPlaceholder,
...
FractionalSecondsPlaceholder(usize),
}
from chrono.
Related Issues (20)
- Missing panic condition on doc HOT 1
- Why doesn't time support multiplication, division, mod? HOT 1
- Unexpected panics due to unwrap HOT 1
- Restart 0.5 branch? HOT 6
- input is not enough for unique date and time HOT 2
- Offer options to configure how to create rfc 3339 string HOT 6
- Split `clock` feature into `clock-now` and `clock-tz` HOT 6
- CI failures in MSRV jobs failing to pick up ahash version
- Operations on NaiveDate are not commutative. HOT 10
- Add `subsec` methods to `Duration` HOT 1
- Time formatting on en_US locale with '%X' not expected HOT 1
- FrameworkNotFound error when building for Apple using maturin HOT 1
- format paniced HOT 11
- panic with_timezone Jan 1, 1601 00:00 UTC HOT 32
- Add methods for float multiplication/division HOT 1
- panic with Local Timezone 2023/11/29 02:00:00 checked_sub_months HOT 2
- rfc HOT 1
- This API seems very complicated HOT 2
- Times before Unix epoch
- Truncating a timestamp close to EPOCH fails HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chrono.