Skip to content

Commit 0d4041b

Browse files
author
bors-servo
authored
Auto merge of #1037 - nical:arc-clone-syntax, r=glennw
Favor the function over the method syntax when cloning a reference counted pointer. ```data.clone()``` reads like performance hazard, due to the vague definition of Clone, which can be either very expensive if data is an uniquely owned large resource or very cheap if it is a reference counted pointer to such a resource. This has already confused people reading the code and even code reviews several times in webrender, so I would like to work around this unfortunate ambiguity. I first tried to address this at the [standard library level](rust-lang/rfcs#1954) but the decision of the rust team appears to be that we should use the function syntax instead of the method syntax to address the problem. See also the [clippy issue](https://github.com/Manishearth/rust-clippy/issues/1645) about providing a lint for this. I decided to take a systematic approach and use the function call syntax for all of the outstanding reference counted pointer clones that I came across (I would not be surprised that I missed some of them). Rather than try to separate the obvious from the ambiguous ones. I would rather make this a default policy than have to debate about where the line is in each code review. The three commits are independent and can be reviewed separately (although they are small and simple enough to look at as one diff if you prefer). <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/webrender/1037) <!-- Reviewable:end -->
2 parents 7463ae5 + 45a2fa8 commit 0d4041b

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

webrender/src/device.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -676,10 +676,10 @@ impl<T> GpuProfiler<T> {
676676
GpuProfiler {
677677
next_frame: 0,
678678
frames: [
679-
GpuFrameProfile::new(gl.clone()),
680-
GpuFrameProfile::new(gl.clone()),
681-
GpuFrameProfile::new(gl.clone()),
682-
GpuFrameProfile::new(gl.clone()),
679+
GpuFrameProfile::new(Rc::clone(&gl)),
680+
GpuFrameProfile::new(Rc::clone(&gl)),
681+
GpuFrameProfile::new(Rc::clone(&gl)),
682+
GpuFrameProfile::new(Rc::clone(&gl)),
683683
],
684684
}
685685
}
@@ -721,12 +721,12 @@ impl GpuMarker {
721721
gl::GlType::Gl => {
722722
gl.push_group_marker_ext(message);
723723
GpuMarker{
724-
gl: gl.clone(),
724+
gl: Rc::clone(&gl),
725725
}
726726
}
727727
gl::GlType::Gles => {
728728
GpuMarker{
729-
gl: gl.clone(),
729+
gl: Rc::clone(&gl),
730730
}
731731
}
732732
}
@@ -1108,7 +1108,7 @@ impl Device {
11081108
};
11091109

11101110
let texture = Texture {
1111-
gl: self.gl.clone(),
1111+
gl: Rc::clone(&self.gl),
11121112
id: id,
11131113
width: 0,
11141114
height: 0,
@@ -1451,7 +1451,7 @@ impl Device {
14511451
}
14521452

14531453
let program = Program {
1454-
gl: self.gl.clone(),
1454+
gl: Rc::clone(&self.gl),
14551455
name: base_filename.to_owned(),
14561456
id: pid,
14571457
u_transform: -1,
@@ -1795,7 +1795,7 @@ impl Device {
17951795
ibo_id.bind(self.gl()); // force it to be a part of VAO
17961796

17971797
let vao = VAO {
1798-
gl: self.gl.clone(),
1798+
gl: Rc::clone(&self.gl),
17991799
id: vao_id,
18001800
ibo_id: ibo_id,
18011801
main_vbo_id: main_vbo_id,

webrender/src/render_backend.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ impl RenderBackend {
301301
if let Some(ref wrapper) = self.webrender_context_handle {
302302
let dispatcher: Option<Box<GLContextDispatcher>> = if cfg!(target_os = "windows") {
303303
Some(Box::new(WebRenderGLDispatcher {
304-
dispatcher: self.main_thread_dispatcher.clone()
304+
dispatcher: Arc::clone(&self.main_thread_dispatcher)
305305
}))
306306
} else {
307307
None

webrender/src/renderer.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ impl Renderer {
589589

590590
let file_watch_handler = FileWatcher {
591591
result_tx: result_tx.clone(),
592-
notifier: notifier.clone(),
592+
notifier: Arc::clone(&notifier),
593593
};
594594

595595
let mut device = Device::new(gl,
@@ -857,11 +857,11 @@ impl Renderer {
857857
device.end_frame();
858858

859859
let main_thread_dispatcher = Arc::new(Mutex::new(None));
860-
let backend_notifier = notifier.clone();
861-
let backend_main_thread_dispatcher = main_thread_dispatcher.clone();
860+
let backend_notifier = Arc::clone(&notifier);
861+
let backend_main_thread_dispatcher = Arc::clone(&main_thread_dispatcher);
862862

863863
let vr_compositor = Arc::new(Mutex::new(None));
864-
let backend_vr_compositor = vr_compositor.clone();
864+
let backend_vr_compositor = Arc::clone(&vr_compositor);
865865

866866
// We need a reference to the webrender context from the render backend in order to share
867867
// texture ids

webrender/src/resource_cache.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ impl ResourceCache {
406406
if !same_epoch && self.blob_image_requests.insert(request) {
407407
renderer.request_blob_image(
408408
key,
409-
data.clone(),
409+
Arc::clone(&data),
410410
&BlobImageDescriptor {
411411
width: template.descriptor.width,
412412
height: template.descriptor.height,
@@ -846,7 +846,7 @@ fn spawn_glyph_cache_thread(workers: Arc<Mutex<ThreadPool>>) -> (Sender<GlyphCac
846846

847847
let barrier = Arc::new(Barrier::new(worker_count));
848848
for i in 0..worker_count {
849-
let barrier = barrier.clone();
849+
let barrier = Arc::clone(&barrier);
850850
workers.lock().unwrap().execute(move || {
851851
register_thread_with_profiler(format!("Glyph Worker {}", i));
852852
barrier.wait();
@@ -882,7 +882,7 @@ fn spawn_glyph_cache_thread(workers: Arc<Mutex<ThreadPool>>) -> (Sender<GlyphCac
882882
// added to each worker thread.
883883
let barrier = Arc::new(Barrier::new(worker_count));
884884
for _ in 0..worker_count {
885-
let barrier = barrier.clone();
885+
let barrier = Arc::clone(&barrier);
886886
let font_template = font_template.clone();
887887
workers.lock().unwrap().execute(move || {
888888
FONT_CONTEXT.with(|font_context| {
@@ -908,7 +908,7 @@ fn spawn_glyph_cache_thread(workers: Arc<Mutex<ThreadPool>>) -> (Sender<GlyphCac
908908
// Delete a font from the font context in each worker thread.
909909
let barrier = Arc::new(Barrier::new(worker_count));
910910
for _ in 0..worker_count {
911-
let barrier = barrier.clone();
911+
let barrier = Arc::clone(&barrier);
912912
workers.lock().unwrap().execute(move || {
913913
FONT_CONTEXT.with(|font_context| {
914914
let mut font_context = font_context.borrow_mut();

0 commit comments

Comments
 (0)