Skip to content

Commit

Permalink
refactor(core): 💡 optimize QueryId::is_same by not creating a String …
Browse files Browse the repository at this point in the history
…using format for every comparison
  • Loading branch information
tashcan authored and M-Adoo committed Dec 22, 2024
1 parent e338610 commit ffde37a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
20 changes: 12 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ Please only add new entries below the [Unreleased](#unreleased---releasedate) he
- **painter**: Fixed text line height does not work correctly. (#674 @M-Adoo)
- **painter**: Fixed issue with text not being drawn at the middle baseline by default. (#674 @M-Adoo)

### Changed

- **core**: Optimize QueryId::is_same by not creating a String using format for every comparison (#678 @tashcan)

## [0.4.0-alpha.19] - 2024-12-18

### Features
Expand Down Expand Up @@ -317,7 +321,7 @@ Please only add new entries below the [Unreleased](#unreleased---releasedate) he

- **core**: Lazy build the widget tree. (#612, @M-Adoo)
- **core**: Simplify the implementation of parent composition with child widgets. (#612, @M-Adoo)

Merge `SingleWithChild`, `MultiWithChild`, and `ComposeWithChild` into a single trait called WithChild.

### Breaking
Expand Down Expand Up @@ -391,18 +395,18 @@ Check out our Wordle game demo, now running smoothly in your browser\!

- **ribir**: Introduced `AppRunGuard` to allow app and window configuration prior to app startup. (\#565, @M-Adoo)
Previously, to configure the app and window before startup, `App::run` couldn't be used:

``` rust
unsafe {
AppCtx::set_app_theme(material::purple::light());
}

App::new_window(root, None).set_title("Counter");
App::exec();
```

Now, with AppRunGuard, you can use `App::run` and chain the configuration methods:

``` rust
App::run(root)
.with_app_theme(material::purple::light())
Expand Down Expand Up @@ -486,14 +490,14 @@ While these are public APIs, they are typically not required for direct use in u
### Features

- Support the overlay (@wjian23).

This enhancement simplifies the creation of overlay widgets. It streamlines the addition of any widget to an overlay and offers a more user-friendly API for overlay management

- **macros**: Generates documentation for the builder methods of members in `#[derive(Declare)]`, thus improving IDE support.(\#538 @M-Adoo)

- **core**: All built-in widget abilities are now exported on `FatObj`. (\#535 @M-Adoo)
You can directly use `FatObj` to configure built-in widget abilities such as `on_click`, `on_key_down`, etc.

``` rust
let _ = FatObj::new(Void)
.margin(EdgeInsets::all(1.0))
Expand All @@ -502,7 +506,7 @@ While these are public APIs, they are typically not required for direct use in u

- **macros**: `#[derive(Decalre)]` now generates a `FatObj<State<T>>` instead of `State<T>`, and supports initialization of all built-in widgets on its ObjBuilder. (\#535 @M-Adoo)
All pipes used to initialize the field will be unsubscribed when the FatObj is disposed.

``` rust
let row = Row::builder()
.margin(...)
Expand Down
31 changes: 20 additions & 11 deletions core/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,21 +362,30 @@ pub struct QueryId {

#[derive(Debug, PartialEq, Eq)]
struct TypeInfo {
name: String,
layout: std::alloc::Layout,
name: &'static str,
pkg_version: &'static str,
layout: &'static std::alloc::Layout,
}

struct TypeInfoOf<T> {
_phantom: std::marker::PhantomData<T>,
}

impl<T> TypeInfoOf<T> {
const LAYOUT: std::alloc::Layout = std::alloc::Layout::new::<T>();

fn type_info() -> TypeInfo {
TypeInfo {
name: std::any::type_name::<T>(),
pkg_version: env!("CARGO_PKG_VERSION"),
layout: &Self::LAYOUT,
}
}
}

impl QueryId {
pub fn of<T: 'static>() -> Self {
QueryId {
type_id: TypeId::of::<T>(),
info: || {
let layout = std::alloc::Layout::new::<T>();
let name =
format!("{}+{}", std::any::type_name::<T>(), env!("CARGO_PKG_VERSION")).to_string();
TypeInfo { name, layout }
},
}
QueryId { type_id: TypeId::of::<T>(), info: || TypeInfoOf::<T>::type_info() }
}

pub fn is_same(&self, other: &QueryId) -> bool {
Expand Down

0 comments on commit ffde37a

Please sign in to comment.