Skip to content

Commit f7fb15a

Browse files
authored
⚡️ perf: avoid many clone for comment (#337)
1 parent 762cb7d commit f7fb15a

File tree

6 files changed

+11
-188
lines changed

6 files changed

+11
-188
lines changed

packages/biliass/rust/src/lib.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,9 @@ fn biliass_pyo3(m: &Bound<'_, PyModule>) -> PyResult<()> {
2424
m.add_class::<python::PyDanmakuElem>()?;
2525
m.add_class::<python::PyComment>()?;
2626
m.add_class::<python::PyCommentPosition>()?;
27-
m.add_class::<python::PyRows>()?;
2827
m.add_function(wrap_pyfunction!(python::py_read_comments_from_xml, m)?)?;
2928
m.add_function(wrap_pyfunction!(python::py_read_comments_from_protobuf, m)?)?;
3029
m.add_function(wrap_pyfunction!(python::py_parse_special_comment, m)?)?;
31-
m.add_function(wrap_pyfunction!(python::py_write_head, m)?)?;
32-
m.add_function(wrap_pyfunction!(python::py_write_normal_comment, m)?)?;
33-
m.add_function(wrap_pyfunction!(
34-
python::py_write_comment_with_animation,
35-
m
36-
)?)?;
37-
m.add_function(wrap_pyfunction!(python::py_write_special_comment, m)?)?;
3830
m.add_function(wrap_pyfunction!(python::py_xml_to_ass, m)?)?;
3931
m.add_function(wrap_pyfunction!(python::py_protobuf_to_ass, m)?)?;
4032
Ok(())

packages/biliass/rust/src/python/mod.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@ mod comment;
22
mod convert;
33
mod proto;
44
mod reader;
5-
mod writer;
65

76
pub use comment::{PyComment, PyCommentPosition};
87
pub use convert::{py_protobuf_to_ass, py_xml_to_ass};
98
pub use proto::{PyDanmakuElem, PyDmSegMobileReply};
109
pub use reader::{
1110
py_parse_special_comment, py_read_comments_from_protobuf, py_read_comments_from_xml,
1211
};
13-
pub use writer::{
14-
py_write_comment_with_animation, py_write_head, py_write_normal_comment,
15-
py_write_special_comment, PyRows,
16-
};

packages/biliass/rust/src/python/writer.rs

Lines changed: 0 additions & 122 deletions
This file was deleted.

packages/biliass/rust/src/writer/ass.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ pub fn write_comment(
9999
}
100100

101101
#[allow(clippy::too_many_arguments)]
102-
pub fn write_normal_comment(
103-
rows: &mut rows::Rows,
104-
comment: &Comment,
102+
pub fn write_normal_comment<'a>(
103+
rows: &mut rows::Rows<'a>,
104+
comment: &'a Comment,
105105
width: u32,
106106
height: u32,
107107
bottom_reserved: u32,

packages/biliass/rust/src/writer/rows.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use crate::comment::{Comment, CommentPosition};
22

3-
pub type Rows = Vec<Vec<Option<Comment>>>;
3+
pub type Rows<'a> = Vec<Vec<Option<&'a Comment>>>;
44

5-
// TODO(SigureMo): Remove clone in the future
6-
7-
pub fn init_rows(num_types: usize, capacity: usize) -> Rows {
5+
pub fn init_rows<'a>(num_types: usize, capacity: usize) -> Rows<'a> {
86
let mut rows: Rows = Vec::new();
97
for _ in 0..num_types {
108
let mut type_rows = Vec::with_capacity(capacity);
@@ -35,8 +33,8 @@ pub fn test_free_rows(
3533
let mut current_row = row;
3634
while current_row < rowmax && (res as f32) < comment.height {
3735
if target_row != rows[comment_pos_id][current_row] {
38-
target_row = rows[comment_pos_id][current_row].clone();
39-
if let Some(target_row) = target_row.clone() {
36+
target_row = rows[comment_pos_id][current_row];
37+
if let Some(target_row) = target_row {
4038
if target_row.timeline + duration_still > comment.timeline {
4139
break;
4240
}
@@ -51,8 +49,8 @@ pub fn test_free_rows(
5149
let mut current_row = row;
5250
while current_row < rowmax && (res as f32) < comment.height {
5351
if target_row != rows[comment_pos_id][current_row] {
54-
target_row = rows[comment_pos_id][current_row].clone();
55-
if let Some(target_row) = target_row.clone() {
52+
target_row = rows[comment_pos_id][current_row];
53+
if let Some(target_row) = target_row {
5654
if target_row.timeline > threshold_time
5755
|| target_row.timeline
5856
+ target_row.width as f64 * duration_marquee
@@ -92,12 +90,12 @@ pub fn find_alternative_row(
9290
res
9391
}
9492

95-
pub fn mark_comment_row(rows: &mut Rows, comment: &Comment, row: usize) {
93+
pub fn mark_comment_row<'a>(rows: &mut Rows<'a>, comment: &'a Comment, row: usize) {
9694
let comment_pos_id = comment.pos.clone() as usize;
9795
for i in row..(row + comment.height.ceil() as usize) {
9896
if i >= rows[comment_pos_id].len() {
9997
break;
10098
}
101-
rows[comment_pos_id][i] = Some(comment.clone());
99+
rows[comment_pos_id][i] = Some(comment);
102100
}
103101
}

packages/biliass/src/biliass/_core.pyi

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from collections.abc import Callable
21
from typing import ClassVar
32

43
class DanmakuElem:
@@ -58,48 +57,9 @@ class Comment:
5857

5958
def read_comments_from_xml(text: str, fontsize: float) -> list[Comment]: ...
6059
def read_comments_from_protobuf(data: bytes, fontsize: float) -> list[Comment]: ...
61-
62-
class Rows:
63-
def __init__(self, num_types: int, capacity: int) -> None: ...
64-
65-
def write_head(width: int, height: int, fontface: str, fontsize: float, alpha: float, styleid: str) -> str: ...
66-
def write_normal_comment(
67-
rows: Rows,
68-
comment: Comment,
69-
width: int,
70-
height: int,
71-
bottom_reserved: int,
72-
fontsize: float,
73-
duration_marquee: float,
74-
duration_still: float,
75-
styleid: str,
76-
reduced: bool,
77-
) -> str: ...
78-
def write_comment_with_animation(
79-
comment: Comment,
80-
width: int,
81-
height: int,
82-
rotate_y: float,
83-
rotate_z: float,
84-
from_x: float,
85-
from_y: float,
86-
to_x: float,
87-
to_y: float,
88-
from_alpha: int,
89-
to_alpha: int,
90-
text: str,
91-
delay: float,
92-
lifetime: float,
93-
duration: float,
94-
fontface: str,
95-
is_border: bool,
96-
styleid: str,
97-
zoom_factor: tuple[float, float, float],
98-
) -> str: ...
9960
def parse_special_comment(
10061
content: str, zoom_factor: tuple[float, float, float]
10162
) -> tuple[tuple[int, int, float, float, float, float], int, int, str, int, float, int, str, bool]: ...
102-
def write_special_comment(comment: Comment, width: int, height: int, styleid: str) -> str: ...
10363
def xml_to_ass(
10464
inputs: list[str],
10565
stage_width: int,

0 commit comments

Comments
 (0)