如何编写最小化页面错误的代码? [英] How to write code which minimizes page faults?

查看:89
本文介绍了如何编写最小化页面错误的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!


先决条件:

-------------------

1)考虑一下我要写一个非常大的程序。说500 K

行。

2)此代码的一部分将由50个结构组成,例如,不多于
比最多1K数据字节。

3)这些结构将被各个地方的所有其他500K行使用。

4)Linux ,SUN Solaris


设计决定:

-------------------

- 向结构添加函数,以便它们处理自己的数据。种类

C ++ OOP。

- 只需使结构携带数据,并编写宏来处理每个结构的

数据,然后插入那些宏在适当的地方
$ 500 $ b代码,无论它们在哪里使用。


这个问题没有涉及什么是好的编程术语在B中使用
宏或OOP和数据封装!!!!!所以请不要发表任何评论

,除非它使你对以下问题的回答更多

清楚。


问题:

--------------------

- 我担心在结构中添加代码/功能会导致更糟糕的情况

locality,即这会导致分页增加。是这样的吗?


- 在需要的地方插入500K行的代码,值得花费多少?b $ b?或者我夸大了

页面错误的风险和后果?


提前谢谢

/ Sune

Hi!

Pre-requisites:
-------------------
1) Consider I''m about to write a quite large program. Say 500 K
lines.
2) Part of this code will consist of 50 structs with, say, no more
than at most 1K bytes of data.
3) These structs are to be used by all of the other 500K lines in
various places.
4) Linux, SUN Solaris

Design decisions:
-------------------
- Add functions to the structs so they handle their own data. Kind of
C++ OOP.
- Just make the structs carry data, and write macros to handle the
data of each struct, and insert those macros in appropriate places
in the 500 K lines of code, wherever they are used.

This question does not deal in what is good programming in terms of
macros or OOP and data encapsulation in C!!!!! So please no remarks
on this unless it makes your answers to the questions below more
clear.

Question:
--------------------
- I''m afraid that adding code/functions to structs results in worse
locality, i.e. this will result in increased paging. Is that so?

- Is inserting code in the 500K lines wherever it''s needed, worth
the effort? or am I over exaggerating the risk and consequences of
page faults?

Thanks in advance
/Sune

推荐答案

使用面向对象的编程应该使它保持相关的代码和数据非常接近。这将改善代码和数据的参考地点




除此之外,你不应该担心这个问题。不要b / b
损害代码的可读性和可维护性以实现更高的

地区的参考。


-

EventStudio 2.5 - http://www.EventHelix.com/EventStudio

从纯文本输入生成PDF和Word EMF中的序列图

Use of object oriented programming should result it keeping related
code and data in close proximity. This would improve the locality
of reference for code as well as data.

Other than that, you should not worry about this issue. Do not
compromise readability and maintainability of code to achieve higher
locality of reference.

--
EventStudio 2.5 - http://www.EventHelix.com/EventStudio
Generate Sequence Diagrams in PDF and Word EMF from plain text input


嗨!


你说的是有道理的,谢谢分享。


如果任何人有任何需要添加的话,请你做。我将回到这个消息

开启和关闭1-2周。


BRs

/ Olle

Hi!

What you say makes sense, thanks for sharing.

IF ANYBODY HAS ANYTHING TO ADD, PLEASE DO. I''LL GO BACK TO THIS MESSAGE
ON AND OFF FOR 1-2 WEEKS.

BRs
/Olle


Sune写道:
嗨!

先决条件:
--- ----------------
1)考虑一下我要写一个非常大的程序。说500 K
行。
2)这部分代码将由50个结构组成,比如说,不超过最多1K字节的数据。
3)这些结构所有其他500K线路将在各个地方使用。
4)Linux,SUN Solaris

设计决策:
------ -------------
- 向结构添加函数,以便它们处理自己的数据。那种C ++ OOP。
- 只需使结构携带数据,并编写宏来处理每个结构的数据,并将这些宏插入到500中的适当位置
K行代码,无论它们在哪里使用。

这个问题不涉及什么是良好的编程方面的宏或OOP和C中的数据封装!!!!!所以请不要对此发表评论,除非它让你对以下问题的回答更清楚。

问题:
---------- ----------
- 我担心在结构中添加代码/函数会导致更糟糕的局部性,即这会导致分页增加。是这样吗?

- 在需要的地方插入500K行代码,值得努力吗?还是我夸大了页面错误的风险和后果?

提前致谢
/ Sune
Hi!

Pre-requisites:
-------------------
1) Consider I''m about to write a quite large program. Say 500 K
lines.
2) Part of this code will consist of 50 structs with, say, no more
than at most 1K bytes of data.
3) These structs are to be used by all of the other 500K lines in
various places.
4) Linux, SUN Solaris

Design decisions:
-------------------
- Add functions to the structs so they handle their own data. Kind of
C++ OOP.
- Just make the structs carry data, and write macros to handle the
data of each struct, and insert those macros in appropriate places
in the 500 K lines of code, wherever they are used.

This question does not deal in what is good programming in terms of
macros or OOP and data encapsulation in C!!!!! So please no remarks
on this unless it makes your answers to the questions below more
clear.

Question:
--------------------
- I''m afraid that adding code/functions to structs results in worse
locality, i.e. this will result in increased paging. Is that so?

- Is inserting code in the 500K lines wherever it''s needed, worth
the effort? or am I over exaggerating the risk and consequences of
page faults?

Thanks in advance
/Sune




不幸的是,你提供的信息都没有说明你的程序的(潜在的)分页行为是什么 - 假设

那里有任何信息分页。 (50个1K结构是一个微不足道的数量,因此,

可能,是对应于500KLOC源的机器代码量。)


更重要的是在这里,假设有足够的数据,其中分页

成为一个问题,是*模式*,其中访问数据(这个

通常以违反直觉的方式工作,顺便说一句。


也许经典情况如下:

你的记忆量是M whatevers。

你可以在一个循环中访问数据的M + 1个数据 - 也就是说,只需要比内存中的数据多得多。


如果计算页数使用

典型的LRU替换算法生成的故障,你会发现这几乎是一个很糟糕的情况 - 绝对*破坏*性能。


如果在这种情况下,你要拆分你的循环,那么它一次只能迭代
超过一半的内存,p的数量年龄缺陷将是* b $ b *大幅减少;事实上他们几乎会被淘汰! [做

计算是一项非常值得的练习。]


HTH,

--ag


-

Artie Gold - 德克萨斯州奥斯汀
http://it-matters.blogspot.com (新帖子12/5)
http://www.cafepress.com/goldsays

如果你没有什么可隐瞒的,你就不会尝试!



Unfortunately, none of the information you''ve provided says much about
what the (potential) paging behavior of your program will be -- assuming
there''s any paging at all. (50 1K structs is a trivial amount, so,
likely, is the amount of machine code corresponding to a 500KLOC source.)

What is more significant here, assuming there''s enough data where paging
becomes an issue, is the *pattern* in which data are accessed (this
often works in ways that are counterintuitive, BTW).

Perhaps the classic situation is as follows:
The amount of memory you have is M whatevers.
You access M + 1 whatevers of data in a loop -- i.e. just barely more
than will fit in memory.

If you count the number of page faults that would be generated using a
typical LRU replacement algorithm, you''ll see that this is pretty much a
pessimal situation -- which absolutely *clobbers* performance.

If, in this situation, you were to split your loop so it only iterates
over half the memory at a time, the number of page faults would be
*drastically* reduced; in fact they would nearly be eliminated! [Doing
the calculation is a very worthwhile exercise.]

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you''re not trying!"


这篇关于如何编写最小化页面错误的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆